Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

程序使用 ANTIALIAS 属性添加封面水印时报错 #124

Open
Prometheus-INTJ opened this issue Aug 9, 2024 · 0 comments
Open

程序使用 ANTIALIAS 属性添加封面水印时报错 #124

Prometheus-INTJ opened this issue Aug 9, 2024 · 0 comments

Comments

@Prometheus-INTJ
Copy link

Prometheus-INTJ commented Aug 9, 2024

描述:
在为图片添加水印时,遇到了 ANTIALIAS 属性相关的错误。具体表现为当用户尝试使用 ANTIALIAS 来调整水印图片大小时,程序抛出以下错误:

AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

复现步骤

  1. 在代码中使用 Pillow 库的 resize 方法对水印图片进行缩放,并传入 Image.ANTIALIAS 作为参数。
  2. 将调整大小后的水印图片粘贴到目标图片上。
  3. 运行脚本。

预期行为

水印图片应被正确缩放并添加到目标图片上,不应抛出任何错误。

实际行为

在调整水印图片大小时,脚本抛出 AttributeError,提示 ANTIALIAS 属性不存在,导致水印无法正常添加。

环境信息

  • Python 版本:3.x
  • Pillow 版本:10.x

建议的解决方案

经过调查,发现 ANTIALIAS 属性在最新版本的 Pillow 中已被弃用,并被 Image.Resampling.LANCZOS 取代。要解决这个问题,需要将 Image.ANTIALIAS 替换为 Image.Resampling.LANCZOS,并确保导入了合适的模块。以下是修改后的代码:

from PIL import Image

def add_to_pic(self, pic_path, img_pic, size, count, mode):
    mark_pic_path = ''
    if mode == 1:
        mark_pic_path = 'Img/SUB.png'
    elif mode == 2:
        mark_pic_path = 'Img/LEAK.png'
    elif mode == 3:
        mark_pic_path = 'Img/UNCENSORED.png'
    img_subt = Image.open(mark_pic_path)
    scroll_high = int(img_pic.height / size)
    scroll_wide = int(scroll_high * img_subt.width / img_subt.height)
    
    # 修改这一行
    img_subt = img_subt.resize((scroll_wide, scroll_high), Image.Resampling.LANCZOS)
    
    r, g, b, a = img_subt.split()  # 获取颜色通道,保持png的透明性
    # 封面四个角的位置
    pos = [
        {'x': 0, 'y': 0},
        {'x': img_pic.width - scroll_wide, 'y': 0},
        {'x': img_pic.width - scroll_wide, 'y': img_pic.height - scroll_high},
        {'x': 0, 'y': img_pic.height - scroll_high},
    ]
    img_pic.paste(img_subt, (pos[count]['x'], pos[count]['y']), mask=a)
    img_pic.save(pic_path, quality=95)

关键修改点

  • Image.ANTIALIAS 替换为 Image.Resampling.LANCZOS
  • 确保已经导入了 Image 模块,并且 Pillow 版本足够新,支持 Image.Resampling.LANCZOS

如果没有更新 Pillow,可以使用以下命令来更新:

pip install --upgrade Pillow
@Prometheus-INTJ Prometheus-INTJ changed the title 添加水印时使用 ANTIALIAS 属性时报错 程序使用 ANTIALIAS 属性添加封面水印时报错 Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant