Python 版本 3.10 在尝试向图像添加水印时生成错误

Python version 3.10 generating a error when trying to add a watermark to an image

注意:所有 3.9 版本 运行 完美创建带水印图像的代码。

我创建这个项目是为了适合完美适合图像大小的水印十字,但是在这行代码中:

paste_mask = watermark.split()[3].point(lambda i: i * TRANSPARENCY / 100.)

它生成此错误:

File "c:\Users\Computador\Desktop\Python\Watermark.py", line 29, in watermark_with_transparency
paste_mask = watermark.split()[3].point(lambda i: i * TRANSPARENCY / 100.)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 1723, in point
return self._new(self.im.point(lut, mode))
TypeError: 'float' object cannot be interpreted as an integer

完整代码为:

from PIL import Image

def watermark_with_transparency(input_image_path,
                                output_image_path,
                                watermark_image_path):
    TRANSPARENCY = 10
    angle = 30
    base_image = Image.open(input_image_path)
    w_img, h_img = base_image.size
    basewidth = w_img
    watermark = Image.open(watermark_image_path)
    watermark = watermark.rotate(angle, expand=True)
    wpercent = (basewidth / float(watermark.size[0]))
    hpercent = h_img / float(watermark.size[1])
    if wpercent < hpercent:
        hsize = int((float(watermark.size[1]) * float(wpercent)))
        watermark = watermark.resize((basewidth, hsize), Image.ANTIALIAS)
    else:
        wsize = int((float(watermark.size[0]) * float(hpercent)))
        watermark = watermark.resize((wsize, h_img), Image.ANTIALIAS)
    w_logo, h_logo = watermark.size
    center_y = int(h_img / 2)
    center_x = int(w_img / 2)
    top_y = center_y - int(h_logo / 2)
    left_x = center_x - int(w_logo / 2)
    if watermark.mode != 'RGBA':
        alpha = Image.new('L', (w_img, h_img), 255)
        watermark.putalpha(alpha)
    paste_mask = watermark.split()[3].point(lambda i: i * TRANSPARENCY / 100.)
    base_image.paste(watermark, (left_x, top_y), mask=paste_mask)
    base_image.save(output_image_path)

图片中的模型没问题,我认为的值是正确的,我应该修改什么才能在这个新的 python 版本中工作?

尝试:

paste_mask = watermark.split()[3].point(lambda i: int(i * TRANSPARENCY / 100.))

似乎在移动到 Python 3.9 时,type inference 发生了变化,并且值没有自动转换为 int