moyy996 / AVDC

日本电影元数据刮削器,配合kodi,emby,plex等本地媒体管理工具使用。可批量抓取,也可单个抓取。可抓取子目录下视频,多集视频(-cd1/-cd2),带字幕作品(-c., -C.)。批量添加emby演员头像。
GNU General Public License v3.0
2.44k stars 221 forks source link

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

Open Prometheus-INTJ opened 1 month ago

Prometheus-INTJ commented 1 month ago

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

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

复现步骤

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

预期行为

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

实际行为

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

环境信息

建议的解决方案

经过调查,发现 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)

关键修改点

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

pip install --upgrade Pillow