python版本3.10试图在图像中添加水印时会产生错误

发布于 2025-01-28 10:35:09 字数 2023 浏览 2 评论 0原文

注意:所有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版本中使用?

Note: all 3.9 versions run the code perfectly creating the watermarked image.

I created this project to fit a watermark cross that perfectly fits the size of the image, but in this line of code:

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

It generating this error:

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

The complete code is:

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)

The model in the image is ok and the values in my view are correct, what should i modify to work in this new python version?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2025-02-04 10:35:09

尝试:

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

似乎在移至Python 3.9时,type typer更改了,并且值不会自动施放到int中。

Try:

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

Seems that in when moving to Python 3.9, the type inference changed and the values did not automatically cast into int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文