【问题标题】:pil composite multiple images giving errorpil合成多个图像给出错误
【发布时间】:2022-01-10 12:02:00
【问题描述】:

如何将所有这些图像组合在一起?请记住,我不知道要合成的图像的名称,文件是由之前的一些代码选择并添加到列表中的。

list=['file1.png', 'file2.png', 'file3.png', 'file4.png']
final = Image.alpha_composite(list[0], list[1], list[2], list[3])
final.save(f'{random.randint(1,10000)}.png')

【问题讨论】:

  • 你的意思是list[0], list[1], ...。方括号代替圆括号。
  • 在这种情况下,您可以使用Image.alpha_composite(*list)
  • 提示:不要为变量使用像list 这样的内置名称。将其命名为 filenames 之类的名称,它的含义也更清楚。
  • 将实际错误(+回溯)添加到您的问题中很有用;不仅仅是代码。
  • @9769953 是的,我的意思是列表[0]。但是它仍然不起作用,因为 alpha_composite() 需要 2 个位置参数,但给出了 4 个。如何将这 4 个图像组合在一起?

标签: python image python-imaging-library


【解决方案1】:

alpha_composite 方法的参数是两个 Image 对象,而不是文件名。

IMO,您不能使用此方法,传递给alpha_composite 的文件的不同顺序将得到不同的结果。也许您可以尝试使用固定 alpha 的方法 blend,例如 0.5

示例代码

from PIL import Image

path = 'path of your holder for image files'

im = [Image.open(path+'/'+file)
    for file in ('10.png', '24.png', '38.png', '52.png')]

im1 = Image.blend(im[0], im[1], 0.5)
im2 = Image.blend(im[2], im[3], 0.5)
image1 = Image.blend(im1, im2, 0.5)
image1.show()

im3 = Image.blend(im[0], im[3], 0.5)
im4 = Image.blend(im[1], im[2], 0.5)
image2 = Image.blend(im3, im4, 0.5)
image2.show()

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2021-12-05
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多