您可以使用scipy.misc.imread
将图像scipy.misc.imread
为numpy数组。然后使用numpy.zeros((height, width, channels))
创建一个具有所需背景的数组,并将图像粘贴到所需位置:
import numpy as np
import scipy.misc
im = scipy.misc.imread('foo.jpg', mode='RGB')
height, width, channels = im.shape
# make canvas
im_bg = np.zeros((height, width, channels))
im_bg = (im_bg + 1) * 255 # e.g., make it white
# Your work: Compute where it should be
pad_left = ...
pad_top = ...
im_bg[pad_top:pad_top + height,
pad_left:pad_left + width,
:] = im
# im_bg is now the image with the background.
0
我有大量固定大小的图像(例如500 * 500)。我想编写一个python脚本,将其调整为固定大小(例如800 * 800),但将原始图像保留在中心,并用固定颜色(例如黑色)填充多余的区域。
我正在使用PIL。我现在可以使用
resize
功能调整图像的resize
,但这会改变纵横比。有什么办法吗?