国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python實(shí)現(xiàn)拼接圖片

瀏覽:55日期:2022-08-01 14:35:09

最近在寫(xiě)一篇卷積神經(jīng)網(wǎng)絡(luò)的論文,有好多實(shí)驗(yàn)結(jié)果需要整理,本來(lái)是用美圖秀秀進(jìn)行圖像的拼接,但是發(fā)現(xiàn)重復(fù)操作太多,而且拼接效果不好,想到用python寫(xiě)個(gè)腳本實(shí)現(xiàn),看一個(gè)簡(jiǎn)單的例子:

python實(shí)現(xiàn)拼接圖片

橫向拼接

首先我需要將同一張圖片的變形拼接為一行,代碼如下:

import osfrom PIL import ImageUNIT_SIZE = 229 # 單個(gè)圖像的大小為229*229TARGET_WIDTH = 6 * UNIT_SIZE # 拼接完后的橫向長(zhǎng)度為6*229path = 'C:/Users/zm/Desktop/FinalResult/Other-Reconstruction/2'images = [] # 先存儲(chǔ)所有的圖像的名稱(chēng)for root, dirs, files in os.walk(path): for f in files : images.append(f)for i in range(len(images)/6): # 6個(gè)圖像為一組 imagefile = [] j = 0 for j in range(6): imagefile.append(Image.open(path+’/’+images[i*6+j])) target = Image.new(’RGB’, (TARGET_WIDTH, UNIT_SIZE)) left = 0 right = UNIT_SIZE for image in imagefile: target.paste(image, (left, 0, right, UNIT_SIZE))# 將image復(fù)制到target的指定位置中 left += UNIT_SIZE # left是左上角的橫坐標(biāo),依次遞增 right += UNIT_SIZE # right是右下的橫坐標(biāo),依次遞增 quality_value = 100 # quality來(lái)指定生成圖片的質(zhì)量,范圍是0~100 target.save(path+’/result/’+os.path.splitext(images[i*6+j])[0]+’.jpg’, quality = quality_value) imagefile = []

縱向拼接

將每種類(lèi)型的圖片拼接為一行六個(gè)的圖片后再將這些圖片縱向拼接在一起,總共3種圖像,那么有3行

import osfrom PIL import ImageUNIT_SIZE = 229 # 圖像的高TARGET_WIDTH = 6 * UNIT_SIZE # 一行有6個(gè)圖像,那么是6*229那么寬path = 'C:/Users/zm/Desktop/FinalResult/Other-Reconstruction/2/result'imagefile = [] for root, dirs, files in os.walk(path): for f in files : imagefile.append(Image.open(path+’/’+f)) target = Image.new(’RGB’, (TARGET_WIDTH, UNIT_SIZE*3)) # 最終拼接的圖像的大小為(229*3) * (229*6)left = 0right = UNIT_SIZEfor image in imagefile: target.paste(image, (0, left, TARGET_WIDTH, right)) left += UNIT_SIZE # 從上往下拼接,左上角的縱坐標(biāo)遞增 right += UNIT_SIZE #左下角的縱坐標(biāo)也遞增  quality_value = 100 target.save(path+’/result.jpg’, quality = quality_value)

同時(shí)橫向縱向拼接

今天需要處理的圖片如下:左邊是圖片所在的文件夾,每個(gè)文件夾的圖片如→_→右邊所示,需要拼接為2*5的圖片。

python實(shí)現(xiàn)拼接圖片

寫(xiě)的腳本如下:

import osfrom PIL import ImageUNIT_SIZE = 229 # the size of image def pinjie(images,num): target = Image.new(’RGB’, (UNIT_SIZE*5, UNIT_SIZE*2)) # result is 2*5 leftone = 0 lefttwo = 0 rightone = UNIT_SIZE righttwo = UNIT_SIZE for i in range(len(images)): if(i%2==0): target.paste(images[i], (leftone, 0, rightone, UNIT_SIZE)) leftone += UNIT_SIZE #第一行左上角右移 rightone += UNIT_SIZE #右下角右移 else: target.paste(images[i], (lefttwo, UNIT_SIZE, righttwo, UNIT_SIZE*2)) lefttwo += UNIT_SIZE #第二行左上角右移 righttwo += UNIT_SIZE #右下角右移 quality_value = 100 target.save(path+dirlist[num]+’.jpg’, quality = quality_value)path = 'C:/Users/laojbdao/Desktop/FinalResult/result4/different_distribution/'dirlist = [] # all dir namefor root, dirs, files in os.walk(path): for dir in dirs : dirlist.append(dir)num = 0for dir in dirlist: images = [] # images in each folder for root, dirs, files in os.walk(path+dir): # traverse each folder print path+dir+’’ for file in files: images.append(Image.open(path+dir+’/’+file)) pinjie(images,num) num +=1 images = []

python實(shí)現(xiàn)拼接圖片

本文已被收錄到專(zhuān)題《python圖片處理操作》 ,歡迎大家點(diǎn)擊學(xué)習(xí)更多精彩內(nèi)容。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 榆社县| 长海县| 深水埗区| 衡阳县| 运城市| 鄂温| 通州区| 介休市| 新乡市| 游戏| 北碚区| 墨玉县| 康保县| 涿鹿县| 宁波市| 漳州市| 博客| 高陵县| 白水县| 韶关市| 广东省| 通山县| 东至县| 凤翔县| 渑池县| 新巴尔虎左旗| 海晏县| 佛冈县| 深泽县| 习水县| 河曲县| 时尚| 扶余县| 东乡族自治县| 资中县| 万盛区| 鲁山县| 正阳县| 平远县| 新昌县| 富川|