无法将调整大小的图像粘贴到枕头

时间:2019-12-22 23:23:29

标签: python python-imaging-library

在枕头模块中粘贴调整大小的图像时出现问题。我要对所有尺寸的图像调整水印,使其宽度为图像宽度的1/3,并保持比例为65:10,在粘贴调整大小的图像后出现此错误:

(480, 360)
(160, 25)
Traceback (most recent call last):
  File "C:/Users/Przemek/PycharmProjects/cwiczenia/venv/project watermark.py", line 15, in <module>
    image.paste(watermarkResized, (0,0))
  File "C:\Users\Przemek\PycharmProjects\cwiczenia\venv\lib\site-packages\PIL\Image.py", line 1504, in paste
    raise ValueError("cannot determine region size; use 4-item box")
ValueError: cannot determine region size; use 4-item box

代码如下:

import os
from PIL import Image
directory = "PATH TO THE DIRECTORY"
watermark = Image.open("PATH TO THE WATERMARK")

for filename in os.listdir(directory):
    image = Image.open("PATH TO THE DIRECTORY"+filename)
    imageWidth, imageHeight = (image.size)
    watermarkResized = watermark.resize(((int(round((imageWidth/3),0)), int(round(((imageWidth/3) /6.5),0))))).copy

    print(image.size)
    print(watermarkResized.size)


    image.paste(watermarkResized, (0,0))
    image.save("PATH TO THE DIRECTORY"+filename+".png")

2 个答案:

答案 0 :(得分:0)

paste需要四个值(x1, y1, x2, y2)

在您的代码中可能是

width, height = watermarkResized.size

image.paste(watermarkResized, (0, 0, width, height))

答案 1 :(得分:0)

我通过简单地从图像对象中删除.copy函数来解决此问题。我现在有另一个问题。如何将具有透明背景图像的.png图像粘贴到图像中?