utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:tumanov_castleoaks 作者: Roamdev 项目源码 文件源码
def variation_watermark(image, **wm_settings):
    """ ????????? ???????? ????? ?? ???????? """
    if image.mode != 'RGBA':
        image = image.convert('RGBA')

    with open(wm_settings['file'], 'rb') as fp:
        watermark = Image.open(fp)
        info = watermark.info
        if watermark.mode not in ('RGBA', 'LA') and not (watermark.mode == 'P' and 'transparency' in info):
            watermark.putalpha(255)

        img_width, img_height = image.size
        wm_width, wm_height = watermark.size

        scale = wm_settings['scale']
        if scale != 1:
            watermark = watermark.resize((int(wm_width * scale), int(wm_height * scale)), Image.ANTIALIAS)
            wm_width, wm_height = watermark.size

        position = wm_settings['position']
        padding = wm_settings['padding']
        if position == 'TL':
            left = padding[0]
            top = padding[1]
        elif position == 'TR':
            left = img_width - wm_width - padding[0]
            top = padding[1]
        elif position == 'BL':
            left = padding[0]
            top = img_height - wm_height - padding[1]
        elif position == 'BR':
            left = img_width - wm_width - padding[0]
            top = img_height - wm_height - padding[1]
        elif position == 'C':
            top = (img_height - wm_height) // 2
            left = (img_width - wm_width) // 2
        else:
            left = top = padding

        opacity = wm_settings['opacity']
        if opacity < 1:
            alpha = watermark.convert('RGBA').split()[3]
            alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
            watermark.putalpha(alpha)
        image.paste(watermark, (left, top), watermark)

    return image
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号