[英]In Python, how can an image stored as a NumPy array be scaled in size?
我通過以下方式創建了一個 NumPy 數組:
data = numpy.zeros((1, 15, 3), dtype = numpy.uint8)
然后我用 RGB 像素值填充這個數組,產生一個可以使用如下程序保存的小彩色圖像:
image = Image.fromarray(data)
image.save("image.png")
為了創建 600 x 300 像素的圖像,我如何擴大 NumPy 數組的大小(無插值)?
您可以按照評論中的建議使用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()
函數也有插值作為參數,您可以通過它指定如何調整圖像大小插值方法:
3]使用枕頭庫
使用Image.resize()
from PIL import Image sourceimage= 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)
在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))
這是使用 PIL 調整存儲在 numpy 數組中的圖像大小的代碼片段。 在這個例子中, img
是一個二維的 numpy 數組。
from PIL import Image
import numpy as np
nr,nc = img.shape
shrinkFactor = .5
img_pil = Image.fromarray(img)
img_pil = img_pil.resize((round(nc*shrinkFactor),round(nr*shrinkFactor)))
img_resized = np.array(img_pil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.