python - 如何根据背景图片规范添加水印对角限制高度和宽度而不损失标志质量?

标签 python python-imaging-library watermark

问题摘要:我的预期结果是能够对角地框出水印,并且无论背景图像的宽度和高度如何,它始终符合限制并且尺寸能够保持质量 Logo 。


为了不损失用作水印的图像的质量,我必须根据要粘贴的图像的宽度重新调整其大小,但正如您在图像中看到的那样,我下面将发布代码,当图片高度太小时,水印会超出图片的限制。

如果我需要根据其他图像的宽度调整水印以保持质量,我应该如何调整高度和宽度,以便水印完美地适合其他图像,无论其高度或宽度如何?

from PIL import Image

def watermark_with_transparency(input_image_path,
                                output_image_path,
                                watermark_image_path):
    
    TRANSPARENCY = 20
    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)
    wpercent = (basewidth/float(watermark.size[0]))
    hsize = int((float(watermark.size[1])*float(wpercent)))
    watermark = watermark.resize((basewidth,hsize), Image.ANTIALIAS)
    watermark = watermark.rotate( angle, expand=1 )
    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.show()
    base_image.save(output_image_path)

if __name__ == '__main__':
    watermark_with_transparency(
        'jogos_de_hoje_na_tv.png',
        'watermark_create.png',
        'logo_com_transparencia.png'
        )

当前结果:

enter image description here




我尝试添加这样的宽度:

    basehight = h_img
    hpercent = (basehight/float(watermark.size[0]))
    wsize = int((float(watermark.size[0])*float(hpercent)))

    watermark = watermark.resize((wsize,hsize), Image.ANTIALIAS)

但结果是水印高度很高,而且尺寸调整没有任何质量。

最佳答案

我像这样更改你的代码:

def watermark_with_transparency(input_image_path,
                                output_image_path,
                                watermark_image_path):
    TRANSPARENCY = 20
    angle = 30
    base_image = Image.open(input_image_path)
    base_image.show()
    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.show()
    base_image.save(output_image_path)

这个想法是,当您调整水印大小时,您必须检查哪个因素(高度或宽度)最小并且您想要采用它。否则,在代码的第一个版本中,调整大小没有考虑到新高度可能大于输入图像高度。

关于python - 如何根据背景图片规范添加水印对角限制高度和宽度而不损失标志质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71211852/

相关文章:

python - 将网络摄像头素材从OpenCV获取到PyQt

python - 如何在不将其保存到文件的情况下在 Qt 中播放 numpy 数组?

python-3.x - 使用PIL保存模式错误将png转换为pdf

image-processing - Tesseract image_to_string 为空

使用 PIL(LSB) 进行 Python 图像处理

video - 如何使用ffmpeg在视频中心添加透明水印?

python - 为什么我收到 TypeError : cannot unpack non-iterable builtin_function_or_method object?

python - Lambda Python 依赖包错误运行时.ImportModuleError : Unable to import module 'lambda_function' : No module named 'surveys'

php - 使用 PHP GD 合并两个图像 (.JPG)

php - 将透明文本写入 jpeg