python - 在 Python 中粘贴图像

标签 python image

我试过这段代码, 将一张图片粘贴到另一张图片上

from PIL import Image
img=Image.open("cat.jpg")
img2=Image.open("cat2.jpg")

area=(10,50,10,20) 
img.paste(img2,area)
img.show()

但是我得到一个错误

Traceback (most recent call last):
  File "C:/Users/****/PycharmProjects/untitled2/s.py", line 6, in <module>
    img.paste(img2,area)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 1361, in paste
    self.im.paste(im, box)
ValueError: images do not match

为什么?

最佳答案

您的代码不起作用,因为您提供的区域大小似乎与图像大小不匹配。您可以使用以下方法获取图像的大小:

(width, height) = img2.size

然后您可以将区域的大小与图像相匹配。

关于python - 在 Python 中粘贴图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477782/

相关文章:

python - 字符串与 int 错误和响应不一致

python - 如何与 Odoo 9 一起运行 Odoo 8?

python - 使用 Python 解析选择性的列和行

android - 如何将图像附件放入 Android 中的 CouchDB?

database - 如何从数据库中获取属性对象

html - 使用响应式设计的图像不显示在 IE 中

css - 在 mozilla 上使用图像源到 base64 时,使用 css 的背景图像不起作用

python - Pip 说安装新版本后安装旧版本

jquery - 更改图标图像的CSS代码

python - 如何找到图像中物体的中心和角度?