如何在不损失logo质量的情况下,根据背景图片规格添加水印对角限制高度和宽度?
How add watermark diagonally limiting height and width according to background image specifications without losing logo quality?
问题总结:我的预期结果是能够对角地框住水印,并且无论背景图片的宽度和高度如何,它总是在限制范围内并且与保持徽标质量的尺寸。
为了不损失我用作水印的图像的质量,我不得不根据我要粘贴的图像宽度重新调整它的大小,但正如您在图像中看到的那样我将在代码下方发布,当图像高度太小时,水印超出图像的限制。
如果我需要根据其他图像的宽度调整水印以保持质量,我应该如何调整高度和宽度以使水印完全适合其他图像而不考虑其高度还是宽度?
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'
)
当前结果:
我尝试添加这样的宽度:
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)
这个想法是,当您调整水印大小时,您必须检查哪个因素(高度或宽度)是最小的并且您想要使用它。否则,在您的第一个版本的代码中,调整大小没有考虑到新高度可能大于输入图像高度。
问题总结:我的预期结果是能够对角地框住水印,并且无论背景图片的宽度和高度如何,它总是在限制范围内并且与保持徽标质量的尺寸。
为了不损失我用作水印的图像的质量,我不得不根据我要粘贴的图像宽度重新调整它的大小,但正如您在图像中看到的那样我将在代码下方发布,当图像高度太小时,水印超出图像的限制。
如果我需要根据其他图像的宽度调整水印以保持质量,我应该如何调整高度和宽度以使水印完全适合其他图像而不考虑其高度还是宽度?
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'
)
当前结果:
我尝试添加这样的宽度:
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)
这个想法是,当您调整水印大小时,您必须检查哪个因素(高度或宽度)是最小的并且您想要使用它。否则,在您的第一个版本的代码中,调整大小没有考虑到新高度可能大于输入图像高度。