日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

编写python脚本完成图片拼接

發布時間:2025/3/21 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写python脚本完成图片拼接 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???????之前在畢業季的時候,自己手上積攢了一堆照片,然后想一口氣發出來祭奠一下自己的大學生涯,于是只好把一堆圖片拼接起來。想當然地,用美圖秀秀,簡單方便還好看,(劃重點,此處不是給美圖秀秀打軟廣!!),可是,當手頭上想拼接的圖片是七八十張起步時,用美圖秀秀就不現實了。于是乎,大概是因為我是閑人吧,我嘗試著用Python寫了個拼接圖片的程序。當然,因為我懶,所以寫了有兩個月研究生都快開學了才想著整理出來,詳細的程序分析此處就省略了哈哈,畢竟呢,“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')

最后附一張生成圖片吧~

總結

以上是生活随笔為你收集整理的编写python脚本完成图片拼接的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。