使用 Scipy 進行影象處理(基本影象調整大小)

SciPy 提供基本的影象處理功能。這些功能包括將影象從磁碟讀取到 numpy 陣列,將 numpy 陣列作為影象寫入磁碟以及調整影象大小的功能。

在以下程式碼中,僅使用一個影象。它被著色,調整大小並儲存。原始影象和生成的影象如下所示:

import numpy as np  //scipy is numpy-dependent

from scipy.misc import imread, imsave, imresize   //image resizing functions

# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')
print img.dtype, img.shape  # Prints "uint8 (400, 248, 3)"

# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 3);
# we multiply it by the array [1, 0.95, 0.9] of shape (3,);
# numpy broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]

# Resize the tinted image to be 300 by 300 pixels.
img_tinted = imresize(img_tinted, (300, 300))

# Write the tinted image back to disk
imsave('assets/cat_tinted.jpg', img_tinted)

https://i.stack.imgur.com/K16nx.jpg https://i.stack.imgur.com/RP5UX.jpg

參考