如何将水印放在图像的中心位置?
how i can put the watermark at the center position of the image?
我有 10k 张图片,所以我尝试使用 Pillow 库在所有图片上添加水印,但水印位置总是会发生变化,如下图所示。
我想把水印放在每张图片的中心位置,水印不能太大也不能太小,它应该适合每张图片所以你能告诉我怎么做吗?
这是水印图片:
我正在使用此代码:
from PIL import Image
import glob
def watermark_with_transparency(input_image_path, output_image_path, watermark_image_path, position):
base_image = Image.open(input_image_path) #open base image
watermark = Image.open(watermark_image_path) #open water mark
width, height = base_image.size #getting size of image
transparent = Image.new('RGBA', (width, height), (0,0,0,0))
transparent.paste(base_image, (0,0))
transparent.paste(watermark, position, mask=watermark)
#transparent.show()
transparent.convert('RGB').save(output_image_path)
print 'Image Done..!'
for inputImage in glob.glob('images/*.jpg'):
output = inputImage.replace('images\','')
outputImage = 'watermark images\'+str(output)
watermark_with_transparency(inputImage, outputImage, 'watermark.png', position=(0,0)) #function
我认为您最好的选择是像这样调整水印的大小:
base_image = Image.open(input_image_path) #open base image
watermark = Image.open(watermark_image_path) #open water mark
watermark = watermark.resize(base_image.size)
您将位置传递为 0,0。如果你想让它居中,那么你应该通过将图像的宽度和高度除以 2 并从中减去水印的宽度和高度除以 2 来更新函数内的位置。
X coordinate = width_of_image/2 - width_of_watermark/2
Y coordinate = height_of_image/2 - height_of_watermark/2
这是一个示例代码:
width_of_watermark , height_of_watermark = watermark.size
position = ((width/2-width_of_watermark/2),(height/2-height_of_watermark/2))
我有 10k 张图片,所以我尝试使用 Pillow 库在所有图片上添加水印,但水印位置总是会发生变化,如下图所示。
我想把水印放在每张图片的中心位置,水印不能太大也不能太小,它应该适合每张图片所以你能告诉我怎么做吗?
这是水印图片:
我正在使用此代码:
from PIL import Image
import glob
def watermark_with_transparency(input_image_path, output_image_path, watermark_image_path, position):
base_image = Image.open(input_image_path) #open base image
watermark = Image.open(watermark_image_path) #open water mark
width, height = base_image.size #getting size of image
transparent = Image.new('RGBA', (width, height), (0,0,0,0))
transparent.paste(base_image, (0,0))
transparent.paste(watermark, position, mask=watermark)
#transparent.show()
transparent.convert('RGB').save(output_image_path)
print 'Image Done..!'
for inputImage in glob.glob('images/*.jpg'):
output = inputImage.replace('images\','')
outputImage = 'watermark images\'+str(output)
watermark_with_transparency(inputImage, outputImage, 'watermark.png', position=(0,0)) #function
我认为您最好的选择是像这样调整水印的大小:
base_image = Image.open(input_image_path) #open base image
watermark = Image.open(watermark_image_path) #open water mark
watermark = watermark.resize(base_image.size)
您将位置传递为 0,0。如果你想让它居中,那么你应该通过将图像的宽度和高度除以 2 并从中减去水印的宽度和高度除以 2 来更新函数内的位置。
X coordinate = width_of_image/2 - width_of_watermark/2
Y coordinate = height_of_image/2 - height_of_watermark/2
这是一个示例代码:
width_of_watermark , height_of_watermark = watermark.size
position = ((width/2-width_of_watermark/2),(height/2-height_of_watermark/2))