【发布时间】:2020-08-23 19:30:59
【问题描述】:
我正在尝试用np.pad 填充洋红色 (255, 0, 255) 颜色的 RGB 图像。但是在使用 RGB 值作为constant_values 时出现错误。例如:
import numpy as np
from scipy.misc import face
import matplotlib.pyplot as plt
def pad_img(img, pad_with):
pad_value = max(img.shape[:-1])
img_padded = np.pad(img,
((0, (pad_value - img.shape[0])), # pad bottom
(0, (pad_value - img.shape[1])), # pad right
(0, 0)), # don't pad channels
mode='constant',
constant_values=pad_with)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(img)
ax2.imshow(img_padded)
plt.show()
这很好用(用白色填充):
img = face()
pad_img(img, pad_with=255)
这不是(用洋红色填充):
img = face()
pad_img(img, pad_with=(255, 0, 255))
投掷:
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,) and requested shape (3,2)
【问题讨论】: