In [5]: from skimage import io
In [6]: I = io.imread('lena512color.tiff')
In [7]: print I.shape
(512, 512, 3)
In [12]: io.imsave('output.bmp', I)
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])
In [8]: io.imshow(I)
RGB → Gray
In [9]: from skimage.color import rgb2gray
In [10]: G = rgb2gray(I)
In [11]: io.imshow(G)
See here for other color conversions.
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
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])))
Recommended Posts