Python PIL ValueError:图像不匹配

问题描述 投票:0回答:1

我正在使用 PIL 并遇到了这个问题。我不知道我做错了什么。这是我的代码:

from PIL import Image

# Load the two images
bg = Image.open('Background.png')
baseman = Image.open('Baseman.png')

# Resize the second image to match the size of the first image
baseman = baseman.resize(bg.size)

# Create a new image with alpha channel
result = Image.new('RGBA', bg.size)

# Mix the two images using alpha compositing
result = Image.alpha_composite(result, bg)
result = Image.alpha_composite(result, baseman)

# Save the result image
result.save('result.png')

我预计项目的输出是 3 个 png 文件的混合。但我遇到了这个错误: ValueError:图像不匹配 我已经查看了类似问题的答案,但没有一个解决它。

python python-imaging-library valueerror
1个回答
1
投票

Image.resize
返回图像的副本。您正在更改列表的元素,但
bg
baseman
bzamin
变量仍然指向旧的、未调整大小的图像。 要么重新分配它们,要么更改以后对
pics[0]
pics[1]
pics[2]
的引用。

© www.soinside.com 2019 - 2024. All rights reserved.