gpt4 book ai didi

python - PIL : pasting an image onto another image, 计算位置

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:04 24 4
gpt4 key购买 nike

我正在尝试将一张图片粘贴到另一张图片上,但无法计算左上角以外的位置。如何计算右上/下角和左下角的位置?

from io import BytesIO
from PIL import Image


def add_watermark():
original_image = Image.open('test1.jpg')
watermark = Image.open('watermark.png')

watermark_width, watermark_height = watermark.size

x, y = original_image.size
margin = 40

# left top
position = ((0 + margin, 0 + margin))

image_with_watermark = Image.new('RGBA', (x, y), (0, 0, 0, 0))
image_with_watermark.paste(original_image, (0, 0))
image_with_watermark.paste(watermark, position, mask=watermark)
image_with_watermark.show()
buffer = BytesIO()
image_with_watermark.save(fp=buffer, format='jpeg')


add_watermark()

最佳答案

对左上角、右上角、左下角和右下角尝试以下操作:

position_tl = (0 + margin, 0 + margin)
position_tr = (x - margin - watermark_width, 0 + margin)
position_bl = (0 + margin, y - margin - watermark_height)
position_br = (x - margin - watermark_width, y - margin - watermark_height)

关于python - PIL : pasting an image onto another image, 计算位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50927972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com