[英]Merge images with Python
我正在嘗試將圖像與python合並,但總的來說,結果圖像總是出現在木板上。
from PIL import Image
images = map(Image.open, ['0.png', '1.png', '2.png', '3.png', '/4.png', '5.png', '6.png', '7.png'])
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save('test.png')
問題是創建迭代器images
map
,迭代器只能使用一次 - 即。 在一for
。 之后,它認為沒有更多的圖像和第二for
不起作用。
並且您使用兩個for
循環中的images
首先(它正常工作):
zip(*(i.size for i in images))
第二(它看到空列表)
for im in images:
您必須使用list()
來創建可以與許多for
循環一起使用的真實列表
images = list(map(...))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.