我正在写一个脚本,从一个文件夹中获取10k图像,并将它们拼贴成100行。每幅图像为32x32像素。这就是我写的剧本:
import glob
from PIL import Image, ImageDraw
#creates an Image based on given pixel size
collage = Image.new("RGBA", (3200,3200), color=(255,255,255,255))
images = glob.glob("*.png")
for image in images:
img = Image.open(image)
x = img.convert("RGBA")
print(x)
#takes each image from the given directory above and merges it into a collage
for i in range(0,3200,32):
for j in range(0,3200,32):
img = images.pop()
collage.paste(img, (i,j))
#displays output of collage
collage.show()
#saves Image to given directory
collage.save(r"C:\Users\17379\Desktop\Nouns DAO\Noun assets\noun"+image)
但是当我运行它时,我会得到这样的错误:
ValueError: cannot determine region size; use 4-item box
我做错了什么?
发布于 2022-09-06 18:13:36
images
是一个文件名列表。你确实在你的第一个循环中加载了它们,但是你永远不会把图像保存在任何地方--你把它们扔掉。因此,在第二个循环中,您要将文件名传递给collage.paste
,而不是图像对象。
也许:
images = glob.glob("*.png")
images = [Image.open(image).convert("RGBA") for image in images]
for i in range(0,3200,32):
for j in range(0,3200,32):
img = images.pop()
collage.paste(img, (i,j))
还请记住,.pop()
是从最后弹出的。如果你想要图像有序,你需要.pop(0)
。
https://stackoverflow.com/questions/73626204
复制相似问题