Last active
March 26, 2021 12:38
-
-
Save RiddMa/d385e369adf379a7e2d134eb3fb4e40f to your computer and use it in GitHub Desktop.
纵向拼接照片
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import PIL.Image as Image | |
if __name__ == '__main__': | |
img_list = [] | |
path = input('Path for stitching:\n') | |
for fn in os.listdir(path): | |
if fn.endswith('.tif') | fn.endswith('.tiff') | fn.endswith('.png') | fn.endswith('.jpg'): | |
img_list.append(Image.open(path + os.sep + fn)) | |
width = 0 | |
height = 0 | |
for img in img_list: | |
w, h = img.size # 单幅图像尺寸 | |
height += h | |
width = max(width, w) # 取最大的宽度作为拼接图的宽度 | |
result = Image.new(img_list[0].mode, (width, height + 20 * (len(img_list) - 1)), 0x000000) # 创建空白长图 | |
height = 0 | |
for img in img_list: # 拼接图片 | |
w, h = img.size | |
result.paste(img, box=(round(width / 2 - w / 2), height)) # 图片水平居中 | |
height += (h + 20) # 间距20px | |
result.save(path + os.sep + 'stitch.jpg', dpi=[300, 300], quality=95) # 保存图片 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment