Brief overview about some of the main python libraries which promote input and output of digital image files.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from _utils import *
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
from PIL import Image as pil
%%time
try:
img_pil = pil.open('../_data/lenna.png')
img_pil = np.asarray(img_pil)
except:
print('Can not load image!')
Wall time: 66 ms
summary(img_pil)
R G B min: 54.0000 1.0000 8.0000 1st Quar: 146.0000 59.0000 78.0000 median: 197.0000 97.0000 100.0000 mean: 180.2240 99.0565 105.4126 3rd Quar: 220.0000 135.0000 125.0000 max: 255.0000 248.0000 239.0000 sigma: 49.0486 52.8771 34.0609
%%time
histogram(img_pil)
img = pil.fromarray(img_pil)
#img.save('_output/image_pil.tiff')
Wall time: 1.29 s
import scipy.misc as scipy
%%time
try:
img_scipy = scipy.imread('../_data/woman01.png')
except:
print('Can not load image!')
Wall time: 13 ms
summary(img_scipy)
R G B min: 9.0000 0.0000 6.0000 1st Quar: 67.0000 56.0000 90.0000 median: 123.0000 101.0000 123.0000 mean: 129.2053 99.2666 125.1805 3rd Quar: 173.0000 134.0000 150.0000 max: 255.0000 255.0000 255.0000 sigma: 62.3956 53.3887 39.1241
%%time
histogram(img_scipy)
Wall time: 1.38 s
#scipy.imsave('_output/image_scipy.tiff', img_scipy8)
Reading and writing images using OpenCV.
Install by command line:
>>> conda install --channel menpo opencv
or
>>> pip install opencv-python
openCV supports bit depth greater than 8. The result is in BGR mode and it's necessary invert channels to convert to RGB.
import cv2
%%time
try:
img_cv = cv2.imread('../_data/woman02.png', -1)
b, g, r = cv2.split(img_cv)
img_cv = cv2.merge((r, g, b))
except:
print('Can not load image!')
Wall time: 25 ms
summary(img_cv)
R G B min: 0.0000 22.0000 34.0000 1st Quar: 128.0000 139.0000 140.0000 median: 145.0000 142.0000 148.0000 mean: 137.5893 139.9451 144.0028 3rd Quar: 147.0000 144.0000 151.0000 max: 255.0000 255.0000 255.0000 sigma: 33.8707 29.0540 28.2876
%%time
histogram(img_cv)
Wall time: 1.36 s
#cv2.imwrite('_output/image_cv.tiff', img_cv)
%%time
try:
img_cv8 = cv2.imread('../_data/sphere_8bits.tiff', -1)
b, g, r = cv2.split(img_cv8)
img_cv8 = cv2.merge((r, g, b))
except:
print('Can not load 8 bits image!')
try:
img_cv16 = cv2.imread('../_data/sphere_16bits.tiff', -1)
b, g, r = cv2.split(img_cv16)
img_cv16 = cv2.merge((r, g, b))
except:
print('Can not load 16 bits image!')
try:
img_cv32 = cv2.imread('../_data/sphere_32bits.tiff', -1)
b, g, r = cv2.split(img_cv32)
img_cv32 = cv2.merge((r, g, b))
except:
print('Can not load 32 bits image!')
Wall time: 121 ms
print('8 bits')
summary(img_cv8)
8 bits R G B min: 3.0000 4.0000 3.0000 1st Quar: 93.0000 92.0000 93.0000 median: 117.0000 116.0000 118.0000 mean: 129.5133 122.3053 129.6245 3rd Quar: 164.0000 141.0000 164.0000 max: 254.0000 254.0000 255.0000 sigma: 52.7126 44.4485 52.7339
%%time
histogram(img_cv8, bins=2**8, interval=[0, 2**8 - 1])
Wall time: 1.53 s
print('\n16 bits')
summary(img_cv16)
16 bits R G B min: 863.0000 863.0000 863.0000 1st Quar: 23796.0000 23648.0000 23828.0000 median: 30144.0000 29844.0000 30211.0000 mean: 33284.9283 31432.4724 33313.5088 3rd Quar: 42053.0000 36328.0000 42116.0000 max: 65154.0000 65097.0000 65493.0000 sigma: 13546.7241 11422.8350 13552.2067
%%time
histogram(img_cv16, bins=2**8, interval=[0, 2**16 - 1])