[Python] Image processing with scikit-image

Loading image files

In [5]: from skimage import io

In [6]: I = io.imread('lena512color.tiff')

In [7]: print I.shape
(512, 512, 3)

Writing image file

In [12]: io.imsave('output.bmp', I)

Writing multiple image files

In the tiff format, multiple image files can be saved as one file as shown below.

In [13]: I1 = io.imread('Lenna.bmp')

In [14]: I2 = io.imread('Mandrill.bmp')

In [15]: I3 = io.imread('Parrots.bmp')

In [16]: io.imsave('output.tiff', [I1, I2, I3])

In [17]: I4 = io.imread('output.tif')

In [18]: I4.shape
Out[18]: (3, 256, 256, 3)

In [19]: io.imshow(I4[0])

In [20]: io.imshow(I4[1])

In [21]: io.imshow(I4[2])

skimage_03.png skimage_04.png skimage_05.png

Image display

In [8]: io.imshow(I)

skimage_01.png

Color conversion

RGB → Gray

In [9]: from skimage.color import rgb2gray

In [10]: G = rgb2gray(I)

In [11]: io.imshow(G)

skimage_02.png

See here for other color conversions.

Change type and value

Perform type conversion and value conversion at the same time. In the case of img_as_float, the value from 255 to 0 is converted to the value from 1.0 to 0.0. If you just want to convert the type, use astype.

In [50]: from skimage import img_as_float, img_as_int, img_as_ubyte, img_as_uint

In [51]: I_float = img_as_float(I)

In [52]: print I_float.dtype, I_float.max(), I_float.min()
float64 1.0 0.0117647058824

In [53]: I_int   = img_as_int(I)

In [54]: print I_int.dtype, I_int.max(), I_int.min()
int16 32767 385

In [55]: I_ubyte = img_as_ubyte(I)

In [56]: print I_ubyte.dtype, I_ubyte.max(), I_ubyte.min()
uint8 255 3

In [57]: I_uint  = img_as_uint(I)

In [58]: print I_uint.dtype, I_uint.max(), I_uint.min()
uint16 65535 771

Image resizing

scikit-image also has transform.resize, but since the conversion method cannot be specified, imresize of scipy is used.

In [79]: from skimage import io

In [80]: import numpy as np

In [81]: from scipy.misc import imresize

In [82]: I = io.imread('Lenna.bmp')

In [83]: IN = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='nearest')

In [84]: IB = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='bilinear')

In [85]: IC = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='bicubic')

In [86]: io.imshow(np.hstack((IN[200:264,200:264], IB[200:264,200:264], IC[200:264,200:264])))

skimage_06.png

Recommended Posts

[Python] Image processing with scikit-image
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Image processing with Python 100 knock # 10 median filter
Image processing with MyHDL
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Image Processing with PIL
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (2)
Sorting image files with Python (3)
Tweet with image in Python
Sorting image files with Python
Image processing by python (Pillow)
Image Processing Collection in Python
Cut out an image with python
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
Image segmentation with scikit-image and scikit-learn
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
[Let's play with Python] Image processing to monochrome and dots
[Image processing] Posterization
FizzBuzz with Python3
Scraping with Python
HTML email with image to send with python
Statistics with python
Image processing by Python 100 knock # 1 channel replacement
Create an image processing viewer with PySimpleGUI
Scraping with Python
Create a dummy image with Python + PIL.
Python with Go
Periodically perform arbitrary processing with Python Twisted
Let Heroku do background processing with Python
Integrate with Python
100 Language Processing Knock with Python (Chapter 2, Part 2)
Python file processing
Grayscale by matrix-Reinventor of Python image processing-
AES256 with python
Use cryptography library cryptography with Docker Python image