在Python中,如何将存储为NumPy数组的图像缩放大小?

问题描述 投票:4回答:2

我用以下方式创建了一个NumPy数组:

data = numpy.zeros((1, 15, 3), dtype = numpy.uint8)

然后我用RGB像素值填充这个数组,产生一个小的彩色图像,可以使用如下过程保存:

image = Image.fromarray(data)
image.save("image.png")

为了创建600 x 300像素的图像,我怎样才能扩大NumPy数组的大小(无插值)?

python image numpy scale shape
2个回答
6
投票

您可以按照评论中的建议使用numpy.kron,也可以使用以下选项

1]使用PILLOW保持宽高比

  • 如果要保持图像的纵横比,则可以使用thumbnail()方法 from PIL import Image def scale_image(input_image_path, output_image_path, width=None, height=None): original_image = Image.open(input_image_path) w, h = original_image.size print('The original image size is {wide} wide x {height} ' 'high'.format(wide=w, height=h)) if width and height: max_size = (width, height) elif width: max_size = (width, h) elif height: max_size = (w, height) else: # No width or height specified raise RuntimeError('Width or height required!') original_image.thumbnail(max_size, Image.ANTIALIAS) original_image.save(output_image_path) scaled_image = Image.open(output_image_path) width, height = scaled_image.size print('The scaled image size is {wide} wide x {height} ' 'high'.format(wide=width, height=height)) if __name__ == '__main__': scale_image(input_image_path='caterpillar.jpg', output_image_path='caterpillar_scaled.jpg', width=800)
  • 我使用了Image.ANTIALIAS标志,它将应用高质量的下采样滤波器,从而产生更好的图像

2]使用OpenCV

  • OpenCV具有cv2.resize()功能 import cv2 image = cv2.imread("image.jpg") # when reading the image the image original size is 150x150 print(image.shape) scaled_image = cv2.resize(image, (24, 24)) # when scaling we scale original image to 24x24 print(scaled_image.shape)
  • 产量 (150, 150) (24, 24)
  • cv2.resize()函数还具有插值作为参数,您可以通过该插值指定要调整图像大小的方式
  • 插值方法: INTER_NEAREST - 最近邻插值 INTER_LINEAR - 双线性插值(默认使用) INTER_AREA - 使用像素区域关系重新采样。它可能是图像抽取的首选方法,因为它可以提供无莫尔条纹的结果。但是当图像被缩放时,它类似于INTER_NEAREST方法。 INTER_CUBIC - 4x4像素邻域上的双三次插值 INTER_LANCZOS4 - 8x8像素邻域的Lanczos插值

3]使用PILLOW库

  • 使用Image.resize() from PIL import Image image = Image.open("image.jpg") # original image of size 150x150 resized_image = sourceimage.resize((24, 24), resample=NEAREST) # resized image of size 24x24 resized_image.show()

4]使用SK-IMAGE库

  • 使用skimage.transform.resize() from skimage import io image = io.imread("image.jpg") print(image.shape) resized_image = skimage.transform.resize(image, (24, 24)) print(resized_image.shape)
  • 产量 (150, 150) (24, 24)

5]使用SciPy

  • 使用scipy.misc.imresize()函数 import numpy as np import scipy.misc image = scipy.misc.imread("image.jpg") print(image.shape) resized_image = scipy.misc.imresize(x, (24, 24)) resized_image print(resized_image.shape)
  • 产量 (150, 150) (24, 24)

2
投票

scikit-image,我们有transform

from skimage import transform as tf
import matplotlib.pyplot as plt
import numpy as np
data = np.random.random((1, 15, 3))*255
data = data.astype(np.uint8)
new_data = tf.resize(data, (600, 300, 3), order=0) # order=0, Nearest-neighbor interpolation
f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(10, 10))
ax1.imshow(data)
ax2.imshow(new_data)
ax3.imshow(tf.resize(data, (600, 300, 3), order=1))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.