如何根据背景图片规格添加水印对角限制高度和宽度而不损失标志质量?
问题摘要:我的预期结果是能够对角地框住水印,无论背景图像的宽度和高度如何,它始终符合限制和尺寸,以保持徽标的质量。
为了不失去用作水印的图像的质量,我必须根据要粘贴的图像的宽度重新调整其大小,但正如您在我将在下面发布的图像中看到的那样代码中,当图像高度太小时,水印超出了图像的限制。
如果我需要根据其他图像的宽度调整水印以保持质量,我应该如何调整高度和宽度,以便水印完美地适合其他图像,无论其高度或宽度如何?
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)
但结果是水印具有很大的高度,并且在尺寸调整方面没有任何质量。
Question summary: My expected result is to be able to frame the watermark diagonally and regardless of the width and height of the background image, it always fits within the limits and with dimensions that maintain the quality of the logo.
In order not to lose the quality of the image that I use as a watermark, I had to readjust its size from the width of the image where I am going to paste it, but as you can see in the image that I will publish below the code, when the image has a too small height, the watermark exceeds the limits of the image.
If I need to adjust the watermark according to the width of the other image to maintain the quality, how should I go about adjusting both the height and width so that the watermark fits perfectly inside the other image regardless of its height or width?
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'
)
Current result:
I tried add doing it for the width like this:
basehight = h_img
hpercent = (basehight/float(watermark.size[0]))
wsize = int((float(watermark.size[0])*float(hpercent)))
watermark = watermark.resize((wsize,hsize), Image.ANTIALIAS)
But the result was a watermark with a lot of height and without any quality in the size adjustment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这样更改您的代码:
想法是,当您调整水印大小时,您必须检查哪个因素(高度或宽度)是最小的并且您想要采用它。否则,在代码的第一个版本中,调整大小没有考虑到新高度可能大于输入图像高度。
I change your code like this:
The idea is that when your resize your watermark you have to check which factor (height or width) is the smallest and you want to take it. Otherwise, in your first version of the code the resizing did not take into account that the new height could be larger than the input image height.
试试这个#m
try this one #m