There python image processing associated libraries a lot, here briefly PIL, cv2, scipy.imageio, matplotlib.image, skimage other commonly used library, which PIL library to use the most convenient, cv2 most powerful library.
PIL:Python Imaging Library
Installation python: pip install Pillow
given here only read, change in shape, image transfer array, an image, and a method of saving an image transfer array.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
# read image
raw_image = Image.open("panda.jpg")
# image resize
image_resize = raw_image.resize((128, 128))
# image to array
image_array = np.array(image_resize)
# array to image
image_output = Image.fromarray(image_array)
# save image
image_output.save("new_panda.jpg")
plt.imshow(raw_image)
plt.axis("off")
plt.show()
cv2: opencv-python
installation Python: PIP-install OpenCV Python
Python openCV in the library, very powerful and can do all kinds of image processing, there being only given reading and saving methods.
import cv2
# read image, return np.array with BGR
raw_image = cv2.imread("panda.jpg")
# BGR to RGB
image_rgb = cv2.cvtColor(raw_image,cv2.COLOR_BGR2RGB)
# image resize
image_resize = cv2.resize(raw_image, (128, 128))
# save image
cv2.imwrite("new_panda.jpg", image_resize)
keras.preprocessing
keras image processing tool, in fact, with the underlying processing PIL, but more to explain.
from keras.preprocessing import image
# read image
raw_image = image.load_img("panda.jpg", target_size=(128, 128))
# image to array
image_array = image.img_to_array(raw_image)
# array to image
image_output = image.array_to_img(image_array)
# save image
image_output.save("new_panda.jpg")
scipy.imageio
Methods of scientific computing library scipy before is scipy.misc, the new version with imageio, misc abandoned.
from imageio import imread, imsave
# read image
raw_image = imread("panda.jpg")
# save image
imsave("new_panda.jpg", image_resize)
# show image
plt.imshow(image_resize)
plt.axis("off")
plt.show()
matplotlib.image
Drawing tool magazine matplotlib Methods,
import matplotlib.image as mpimg
# read image , return np.array
raw_image = mpimg.imread("panda.jpg")
# save image
mpimg.imsave("new_panda.jpg", raw_image)
# show image
plt.imshow(raw_image)
plt.axis("off")
plt.show()
skimage: Scikit-Image
python installation: PIP install -U scikit-Image
Scikit-Image image processing is an extension of the scipy basis, may be interested to know.
from skimage import io
# read image
raw_image = io.imread("panda.jpg")
# save image
io.imsave("new_panda.jpg", raw_image)
# show image
io.imshow(raw_image)