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

d3p*_*3pd 5 python numpy image shape scale

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

data = numpy.zeros((1, 15, 3), dtype = numpy.uint8)
Run Code Online (Sandbox Code Playgroud)

然后,我用RGB像素值填充了此数组,生成了一些彩色图像,可以使用以下过程将其保存:

image = Image.fromarray(data)
image.save("image.png")
Run Code Online (Sandbox Code Playgroud)

我如何缩放NumPy数组的大小(不进行插值),以创建600 x 300像素的图像?

Jai*_*Jai 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)
    
    Run Code Online (Sandbox Code Playgroud)
  • 我使用了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)
    
    Run Code Online (Sandbox Code Playgroud)
  • 输出量

    (150, 150)
    (24, 24)
    
    Run Code Online (Sandbox Code Playgroud)
  • 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()
    
    Run Code Online (Sandbox Code Playgroud)

4]使用SK-IMAGE

5]使用SciPy


CT *_*Zhu 3

在 中scikit-image,我们有变换

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述