我试图创建一个完整的图像,并希望粘贴较小的图像到它。这是我的代码的一个例子。
from PIL import Image
imageWidth=5760
imageHeight=2880
image_sheet = Image.new("RGB", (imageWidth, imageHeight), (255, 255, 255))
这将创建一个具有参数中提到的规范的图像对象。现在,我希望在这个image.For示例中粘贴一个大小为512*512的图像,
image=np.zeros((512,512))
image_sheet.paste(image,box=(0,0)) # I am trying to paste image of size 512*512 at upper left location of (0,0) as per the documentation
我知道这个错误:
File "C:\Users\SSHUB\Anaconda3\envs\dl_torch\lib\site-packages\PIL\Image.py", line 1537, in paste
raise ValueError("cannot determine region size; use 4-item box")
ValueError: cannot determine region size; use 4-item box
如果我使用像这个image_sheet.paste(image,box=(0,0,512,512))
这样的4项框,我会得到以下错误:
File "C:\Users\SSHUB\Anaconda3\envs\dl_torch\lib\site-packages\PIL\Image.py", line 1559, in paste
self.im.paste(im, box)
TypeError: color must be int or tuple
我用的是枕头9.0.1。请指导如何解决这个问题。
发布于 2022-10-07 11:36:45
Image.paste()
只接受另一个Image
实例或像素颜色(可以是字符串、整数或元组,这取决于图像的模式)。
您传递的是一个数字数组,这不是图像,也不是有效的像素颜色。首先让它成为一个图像,例如使用Image.fromarray()
image_sheet.paste(Image.fromarray(image))
也许将image_sheet.mode
作为第二个参数传递给Image.fromarray()
。我没有使用box
参数作为Image.paste()
,因为默认情况下是(0, 0)
。
Image.paste()
方法的实现方式,如果您不传递一个图像,它假设您传递的是像素颜色,在这种情况下,您必须指定一个4值框,因此第一个错误消息。当你给它一个4值的盒子时,它才能告诉你你传入的不是像素颜色!
https://stackoverflow.com/questions/73986485
复制相似问题