AI01, Loading and manipulating images
Back to the previous page|page management
List of posts to read before reading this article
Contents
- How to Load and Display Images
- How to Save Images to File
- How to Convert Color Space
- How to Convert Images to NumPy Arrays and Back
- How to Resize Images
- How to Flip, Rotate, and Crop Images
- How to Create Basic Drawings
- How to Gamma Correction(Gamma Encoding)
- How to Determine Structural Similarity
How to Load and Display Images
Lib for loading an image | Loaded image type |
---|---|
keras | <class ‘PIL.JpegImagePlugin.JpegImageFile’> |
PIL | <class ‘PIL.JpegImagePlugin.JpegImageFile’> |
skimage | <class ‘imageio.core.util.Array’> |
cv2 | <class ‘numpy.ndarray’> |
matplotlib | <class ‘numpy.ndarray’> |
Loaded image type | Lib for showing an image |
---|---|
<class ‘PIL.JpegImagePlugin.JpegImageFile’> | keras, PIL, matplotlib |
<class ‘imageio.core.util.Array’> <class ‘numpy.ndarray’> |
skimage, matplotlib |
Loading and showing an image with keras
# example of loading an image with the Keras API
from keras.preprocessing.image import load_img
# load the image
image = load_img('beach.jpg')
# report details about the image
print(type(image))
print(image.format)
print(image.mode)
print(image.size)
# show the image
image.show()
OUTPUT
<class 'PIL.JpegImagePlugin.JpegImageFile'>
JPEG
RGB
(640, 427)
Loading and showing an image with Pillow
# load and show an image with Pillow
from PIL import Image
# load the image
image = Image.open('opera_house.jpg')
# summarize some details about the image
print(image.format)
print(image.mode)
print(image.size)
print(type(image))
# show the image
image.show()
OUTPUT
JPEG
RGB
(640, 360)
<class 'PIL.JpegImagePlugin.JpegImageFile'>
Loading and showing an image with skimage
%matplotlib inline
from skimage import io # pip install scikit-image
image = io.imread('puppy.jpg')
print(type(image))
print(image.shape)
io.imshow(image)
OUTPUT
<class 'imageio.core.util.Array'>
(366, 487, 3)
<matplotlib.image.AxesImage at 0x22978a710f0>
Loading and showing an image with matplotlib
# load and display an image with Matplotlib
from matplotlib import image
from matplotlib import pyplot
# load image as pixel array
data = image.imread('opera_house.jpg')
print(type(data))
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()
OUTPUT
<class 'numpy.ndarray'>
uint8
(360, 640, 3)
Loading an image with cv2 and showing an image with skimage
import required packages
- pip install scikit-image
- pip install opencv-contrib-python
%matplotlib inline
from skimage import io
import cv2
image = cv2.imread('cat.jpg')
print(type(image))
print(image.shape)
io.imshow(image)
OUTPUT
<class 'numpy.ndarray'>
(225, 225, 3)
<matplotlib.image.AxesImage at 0x1f1b00a26a0>
Loading all images in a directory
# load all images in a directory
from os import listdir
from matplotlib import image
# load all images in a directory
loaded_images = list()
for filename in listdir('images'):
# load image
img_data = image.imread('images/' + filename)
# store loaded image
loaded_images.append(img_data)
print('> loaded %s %s' % (filename, img_data.shape))
OUTPUT
> loaded beauty.jpg (150, 120, 3)
> loaded opera_house.jpg (360, 640, 3)
How to Save Images to File
Saving image with matplotlib
import matplotlib.pyplot as plt
from matplotlib import image
img = image.imread('input_image.jpg')
plt.imshow(img)
plt.savefig('output_image.jpg')
Saving image with keras
# example of saving an image with the Keras API
from keras.preprocessing.image import load_img
from keras.preprocessing.image import save_img
from keras.preprocessing.image import img_to_array
# load image as as grayscale
img = load_img('beach.jpg', color_mode='grayscale')
# convert image to a numpy array
img_array = img_to_array(img)
# save the image with a new filename
save_img('bondi_beach_grayscale.jpg', img_array)
Saving image with PIL
# example of saving an image in another format
from PIL import Image
# load the image
image = Image.open('opera_house.jpg')
# save as PNG format
image.save('opera_house.png', format='PNG')
# load the image again and inspect the format
image2 = Image.open('opera_house.png')
print(image2.format)
Saving image with skimage
#Import libraries
from skimage import io
from skimage import color
#Read image
img = io.imread('puppy.jpg')
#Convert to YPbPr
img_ypbpr= color.rgb2ypbpr(img)
#Convert back to RGB
img_rgb= color.ypbpr2rgb(img_ypbpr)
#Save image
io.imsave("puppy_ypbpr.jpg", img_ypbpr)
Saving image with Pandas(format : xlsx)
from skimage import io
import pandas as pd
image = io.imread('puppy.jpg')
df = pd.DataFrame(image.flatten()) # flatten() : convert the three dimensions of an RGB image to a single dimension
# saving in excel format
filepath = 'pixel_values1.xlsx'
df.to_excel(filepath, index=False) # pip install OpenPyXL
How to Convert Color Space
RGB to grayscale and Vice Versa
from PIL import Image
img = Image.open('input_image.jpg').convert('LA')
img.show()
RGB to HSV and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
from skimage import data
from pylab import *
img = io.imread('puppy.jpg') #Read image
img_hsv = color.rgb2hsv(img) #Convert to HSV
img_rgb = color.hsv2rgb(img_hsv) #Convert back to RGB
#Show both figures
figure(0)
io.imshow(img_hsv)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x1b99ef0da90>
RGB to XYZ and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
from skimage import data
#Read image
img = io.imread('puppy.jpg')
#Convert to XYZ
img_xyz = color.rgb2xyz(img)
#Convert back to RGB
img_rgb = color.xyz2rgb(img_xyz)
#Show both figures
figure(0)
io.imshow(img_xyz)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x194140a0cf8>
RGB to LAB and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
#Read image
img = io.imread('puppy.jpg')
#Convert to LAB
img_lab = color.rgb2lab(img)
#Convert back to RGB
img_rgb = color.lab2rgb(img_lab)
#Show both figures
figure(0)
io.imshow(img_lab)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x194141acf98>
RGB to YUV and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
#Read image
img = io.imread('puppy.jpg')
#Convert to YUV
img_yuv = color.rgb2yuv(img)
#Convert back to RGB
img_rgb = color.yuv2rgb(img_yuv)
#Show both figures
figure(0)
io.imshow(img_yuv)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x194142c0940>
RGB to YIQ and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
#Read image
img = io.imread('puppy.jpg')
#Convert to YIQ
img_yiq = color.rgb2yiq(img)
#Convert back to RGB
img_rgb = color.yiq2rgb(img_yiq)
#Show both figures
figure(0)
io.imshow(img_yiq)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x19415d2a6d8>
RGB to YPbPr and Vice Versa
%matplotlib inline
#Import libraries
from skimage import io
from skimage import color
#Read image
img = io.imread('puppy.jpg')
#Convert to YPbPr
img_ypbpr= color.rgb2ypbpr(img)
#Convert back to RGB
img_rgb= color.ypbpr2rgb(img_ypbpr)
#Show both figures
figure(0)
io.imshow(img_ypbpr)
figure(1)
io.imshow(img_rgb)
OUTPUT
<matplotlib.image.AxesImage at 0x19415e8e470>
How to Convert Images to NumPy Arrays and Back
Convert an loaded image with keras to type of ‘numpy.ndarray’
# example of converting an image with the Keras API
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
import numpy as np
from PIL import Image
# load the image
img = load_img('beach.jpg')
print(type(img))
# convert to numpy array(img_ndarray is equal way to img_array)
img_ndarray = np.asarray(img).astype('float32')
img_array = img_to_array(img)
print(img_ndarray.dtype)
print(img_ndarray.shape)
print(img_array.dtype)
print(img_array.shape)
# convert back to image
img_pil1 = array_to_img(img_array)
img_pil2 = img_array.astype(np.uint8)
img_pil2 = Image.fromarray(img_pil2)
OUTPUT
<class 'PIL.JpegImagePlugin.JpegImageFile'>
float32
(427, 640, 3)
float32
(427, 640, 3)
Convert an loaded image with PIL to type of ‘numpy.ndarray’
# load image and convert to and from NumPy array
from PIL import Image
from numpy import asarray
# load the image
image = Image.open('opera_house.jpg')
print(type(image))
# convert image to numpy array
data = asarray(image)
# summarize shape
print(type(data))
print(data.shape)
# create Pillow image
image2 = Image.fromarray(data)
print(type(image2))
# summarize image details
print(image2.format)
print(image2.mode)
print(image2.size)
image2
OUTPUT
<class 'PIL.JpegImagePlugin.JpegImageFile'>
<class 'numpy.ndarray'>
(360, 640, 3)
<class 'PIL.Image.Image'>
None
RGB
(640, 360)
Convert an loaded image with skimage to type of ‘numpy.ndarray’
%matplotlib inline
from skimage import io # pip install scikit-image
import numpy as np
image = io.imread('puppy.jpg')
image = np.asarray(image)
type(image)
OUTPUT
<class 'numpy.ndarray'>
How to Resize Images
Resizing images, preserving the aspect ratio with PIL
# create a thumbnail of an image
from PIL import Image
# load the image
image = Image.open('opera_house.jpg')
# report the size of the image
print(image.size)
# create a thumbnail and preserve aspect ratio
image.thumbnail((100,100))
# report the size of the modified image
print(image.size)
# show the image
image.show()
OUTPUT
(640, 360)
(100, 56)
Resizing images, forcing the pixels into a new shape with PIL
# resize image and force a new shape
from PIL import Image
# load the image
image = Image.open('opera_house.jpg')
# report the size of the image
print(image.size)
# resize image and ignore original aspect ratio
img_resized = image.resize((200,200))
# report the size of the thumbnail
print(img_resized.size)
# show the image
img_resized.show()
OUTPUT
(640, 360)
(200, 200)
Resizing images, forcing the pixels into a new shape with skimage
from skimage import io
from skimage.transform import resize
img = io.imread('puppy.jpg')
img_res = resize(img, (100,100))
io.imshow(img_res)
io.imsave("ss.jpg", img_res)
OUTPUT
How to Flip, Rotate, and Crop Images
Flipping images with PIL
# create flipped versions of an image
from PIL import Image
from matplotlib import pyplot
# load image
image = Image.open('opera_house.jpg')
# horizontal flip
hoz_flip = image.transpose(Image.FLIP_LEFT_RIGHT)
# vertical flip
ver_flip = image.transpose(Image.FLIP_TOP_BOTTOM)
# plot all three images using matplotlib
pyplot.subplot(311)
pyplot.imshow(image)
pyplot.subplot(312)
pyplot.imshow(hoz_flip)
pyplot.subplot(313)
pyplot.imshow(ver_flip)
pyplot.show()
OUTPUT
Rotating images with PIL
# create rotated versions of an image
from PIL import Image
from matplotlib import pyplot
# load image
image = Image.open('opera_house.jpg')
# plot original image
pyplot.subplot(311)
pyplot.imshow(image)
# rotate 45 degrees
pyplot.subplot(312)
pyplot.imshow(image.rotate(45))
# rotate 90 degrees
pyplot.subplot(313)
pyplot.imshow(image.rotate(90))
pyplot.show()
OUTPUT
Rotating images with skimage
%matplotlib inline
from skimage import io
from skimage.transform import rotate
img = io.imread('puppy.jpg')
img_rot = rotate(img, 20)
io.imshow(img_rot)
OUTPUT
<matplotlib.image.AxesImage at 0x2708d61b390>
Cropping images with PIL
# example of cropping an image
from PIL import Image
# load image
image = Image.open('opera_house.jpg')
# create a cropped image
cropped = image.crop((100, 100, 200, 200))
# show cropped image
cropped.show()
OUTPUT
How to Create Basic Drawings
Lines
%matplotlib inline
from skimage import io
from skimage import draw
img = io.imread('puppy.JPG')
print(img.shape)
x,y = draw.line(0,0,100,100)
img[x, y] = 0
io.imshow(img)
OUTPUT
(366, 487, 3)
<matplotlib.image.AxesImage at 0x25100100908>
Rectangles
from skimage import io
from skimage import draw
img = io.imread('puppy.jpg')
def rectangle(x, y, w, h):
rr, cc = [x, x + w, x + w, x], [y, y, y + h, y + h]
return (draw.polygon(rr, cc))
rr, cc = rectangle(10, 10, 355,355)
img[rr, cc] = 1
io.imshow(img)
OUTPUT
<matplotlib.image.AxesImage at 0x25100046320>
Circles
#Import libraries
from skimage import io
from skimage import draw
#Load image
img = io.imread('puppy.jpg')
#Define circle coordinates and radius
x, y = draw.circle(10,10, 100)
#Draw circle
img[x, y] = 1
#Show image
io.imshow(img)
OUTPUT
<matplotlib.image.AxesImage at 0x251001516d8>
Bezier Curve
#Import libraries
from skimage import io
from skimage import draw
#Load image
img = io.imread('puppy.jpg')
#Define Bezier curve coordinates
x, y = draw.bezier_curve(0,0, 100, 100, 200,300,100)
#Draw Bezier curve
img[x, y] = 1
#Show image
io.imshow(img)
OUTPUT
<matplotlib.image.AxesImage at 0x251011930b8>
Text
%matplotlib inline
#import required packages
import cv2
import numpy as np
from skimage import io
#Read image
image = cv2.imread("cat_1.jpg")
#Define font
font = cv2.FONT_HERSHEY_SIMPLEX
#Write on the image
cv2.putText(image, "I am a Cat", (50, 50), font, 0.8, (0, 255, 0), 2, cv2.LINE_AA)
io.imshow(image)
OUTPUT
<matplotlib.image.AxesImage at 0x2386ff17ac8>
How to Gamma Correction(Gamma Encoding)
%matplotlib inline
from skimage import exposure
from skimage import io
from pylab import *
img = io.imread('puppy.jpg')
gamma_corrected1 = exposure.adjust_gamma(img, 0.5)
gamma_corrected2 = exposure.adjust_gamma(img, 5)
figure(0)
io.imshow(gamma_corrected1)
figure(1)
io.imshow(gamma_corrected2)
OUTPUT
<matplotlib.image.AxesImage at 0x1dd37ad2cc0>
How to Determine Structural Similarity
Structural Similarity is used to find the index that indicate how much two images are similar.Here, SSIM takes three arguments. The first refers to the image; the second indicates the range of the pixels (the highest pixel color value less the lowest pixel color value). The third argument is multichannel.
from skimage import io
from skimage.measure import compare_ssim as ssim
img_original = io.imread('puppy.jpg')
img_modified = io.imread('puppy_ypbpr.jpg')
ssim_original = ssim(img_original, img_original, data_range=img_original.max() - img_original.min(), multichannel=True)
ssim_different = ssim(img_original, img_modified, data_range=img_modified.max() - img_modified.min(), multichannel=True)
print(ssim_original,ssim_different)
OUTPUT
1.0 0.41821662536853843
List of posts followed by this article
Reference