编写python脚本完成图片拼接
生活随笔
收集整理的這篇文章主要介紹了
编写python脚本完成图片拼接
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
???????之前在畢業(yè)季的時(shí)候,自己手上積攢了一堆照片,然后想一口氣發(fā)出來祭奠一下自己的大學(xué)生涯,于是只好把一堆圖片拼接起來。想當(dāng)然地,用美圖秀秀,簡單方便還好看,(劃重點(diǎn),此處不是給美圖秀秀打軟廣!!),可是,當(dāng)手頭上想拼接的圖片是七八十張起步時(shí),用美圖秀秀就不現(xiàn)實(shí)了。于是乎,大概是因?yàn)槲沂情e人吧,我嘗試著用Python寫了個(gè)拼接圖片的程序。當(dāng)然,因?yàn)槲覒?#xff0c;所以寫了有兩個(gè)月研究生都快開學(xué)了才想著整理出來,詳細(xì)的程序分析此處就省略了哈哈,畢竟呢,“talk is cheap, show me your code!”
# -*- coding:utf-8 -*- import os from PIL import Image, ExifTags import numpy as np# read image taken by mobile phone, automatically adjust the angle of pictures to the correct location. def read_img(file):img = Image.open(file)try:for orientation in ExifTags.TAGS.keys() : if ExifTags.TAGS[orientation] == 'Orientation': break exif=dict(img._getexif().items())if exif[orientation] == 3 : img=img.rotate(180, expand = True)elif exif[orientation] == 6 : img=img.rotate(270, expand = True)elif exif[orientation] == 8 : img=img.rotate(90, expand = True)except:passreturn img# merge images vertically. def merge_vertical_numpy(arrays, picture_name):baseImg = arrays[0]baseSize = baseImg.sizebaseMat = np.array(baseImg) # transform the dimension to 2dfor img in arrays[1:]:# resize to same widthtmpSize = img.sizeif tmpSize != baseSize:img = img.resize((baseSize[0], round(baseSize[0] / tmpSize[0] * tmpSize[1])), Image.ANTIALIAS)mat = np.array(img)baseMat = np.append(baseMat, mat, axis=0)report_img = Image.fromarray(baseMat)report_img.save(picture_name)# merge images horizontally. def merge_horizontal(files):baseImg = read_img(files[0])baseSize = baseImg.size# for function 'np.array' or 'np.atleast_2d', I think that image quality through 'np.array' is lower than 'np.atleast_2d'.# method 1:baseMat = np.array(baseImg)# method 2:# basemat = np.atleast_2d(baseImg) # transform the dimension to 2dfor file in files[1:]:img = read_img(file)# resize to same widthtmpSize = img.sizeif tmpSize != baseSize:img = img.resize((round(baseSize[1] / tmpSize[1] * tmpSize[0]), baseSize[1]), Image.ANTIALIAS) # Image.ANTIALIAS -- high quality mat = np.array(img)baseMat = np.append(baseMat, mat, axis=1)report_img = Image.fromarray(baseMat)return report_img"""params:dirname: dir which contains pictures waiting to be mergedhorizontal_num: nums of pictures horizontallyvertical_num: nums of pictures verticallypicture_name: name of saved picture """ def merge(dirname, horizontal_num, vertical_num, picture_name):files = [file for file in os.listdir(dirname)]# confirm that horizontal_num * vertical_num == number of total imagesif len(files) != horizontal_num * vertical_num:raise Exception("Invalid horizontal nums or vertical nums!")files_seg = []numpy_seg = []lineCnt = 0for i in range(len(files)):files_seg.append('{}/{}'.format(dirname, files[i]))if (i + 1) % horizontal_num == 0:numpy_seg.append(merge_horizontal(files_seg))lineCnt += 1print('generate image line {}'.format(lineCnt))files_seg = []merge_vertical_numpy(numpy_seg, picture_name)if __name__ == '__main__':merge('./美好', 10, 10, 'merge_wounderfun.jpg')最后附一張生成圖片吧~
總結(jié)
以上是生活随笔為你收集整理的编写python脚本完成图片拼接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统下文件字体乱码的解决方案
- 下一篇: 从一亩三分地转——“有代码的地方,就有江