我正在尝试将一张图像粘贴到另一张图像上,但是无法计算除左上角以外的位置。如何计算右上/下角和左下角的位置?
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()
答案 0 :(得分:2)
尝试对左上,右上,左下和右下进行以下操作:
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)