Rid*_*kei 6 python-imaging-library python-3.x
我正准备将 2.jpg 的一小部分粘贴到 1.jpg
from PIL import Image
body = Image.open("1.jpg")
head = Image.open("2.jpg")
headbox = (0,0,30,30)
head.crop(headbox).save("head.jpg")
body.paste("head.jpg", (0,0)).save("out.jpg")
Run Code Online (Sandbox Code Playgroud)
然后它抛出一个错误
****************************************, line 8, in <module>
body.paste("head.jpg", (0,0)).save("out.jpg")
File "C:\Users\liton\Anaconda3\lib\site-packages\PIL\Image.py", line 1401, in paste
"cannot determine region size; use 4-item box"
ValueError: cannot determine region size; use 4-item box
Run Code Online (Sandbox Code Playgroud)
我使用 pycharm 和 python 3.7,我没有看到任何语法错误。所以代码怎么了
您应该将图像对象传递给“body.paste”,但是,您只是传递了一个字符串(图像名称)。所以首先你需要使用'Image.open'打开你的图像,然后将它传递给'body.paste'。此外,'body.paste' 不返回任何值,因此您不能直接使用 'save' 方法。以下代码将解决您的问题:
from PIL import Image
body = Image.open("1.jpg")
head = Image.open("2.jpg")
headbox = (0,0,30,30)
head.crop(headbox).save("head.jpg")
head_crop = Image.open("./head.jpg")
body.paste(head_crop, (0,0))
body.save("out.jpg")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5897 次 |
| 最近记录: |