无法在Pi中粘贴调整大小的图像

2024-10-04 05:34:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在枕头模块中粘贴调整大小的图像时遇到问题。我想将水印调整到任何大小的所有图像,宽度为图像宽度的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")

Tags: thetopathin图像imagesize粘贴
2条回答

我只需从image对象中删除.copy函数,就解决了这个问题。我现在有另一个问题。如何将背景透明的.png图像粘贴到图像中?你知道吗

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

在你的代码里可能是

width, height = watermarkResized.size

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

相关问题 更多 >