因此,我收集了一些二进制(黑白)照片,其高度和宽度从80像素到140像素不等。长宽比不固定。我想在Python中将每张照片嵌入150px×150px较大的照片的中心。
我直到最近才熟悉PIL和OpenCV。而且我使用Python已有很长时间了。因此,我被困住了。任何在Python中都能使用的解决方案都值得赞赏。
答案 0 :(得分:2)
在使用PIL之前,我已经做了非常相似的事情。我假设您正在尝试输出新图像。
from PIL import Image
# load in the top image
top_img = Image.open('1.jpg', 'r')
top_img_w, top_img_h = top_img.size
# load in the bottom image
bottom_img = Image.open('2.jpg', 'r')
# get the size or use 150x150 if it's constant
bottom_img_w, bottom_img_h = bottom_img.size
# offset the top image so it's placed in the middle of the bottom image
offset = ((bottom_img_w - top_img_w) // 2, (bottom_img_h - top_img_h) // 2)
# embed top_img on top of bottom_img
bottom_img.paste(top_img, offset)
output_name = '3.jpg'
bottom_img.save(output_name)