Usage Example (Libtiff wrapper)
>>> from Libtiff import TIFF
>>> # to open a TIFF file for reading:
>>> tif = Tiff.open (' Filename.tif ', mode= ' R ')
>>> to read a image in the Currect TIFF directory and return it as NumPy array:
>>> image = TIF.R Ead_image ()
>>> # to read all images in a TIFF file:
>>> for image in Tif.iter_images (): # do St Uff with Image
>>> # to open a TIFF file for writing:
>>> tif = Tiff.open (' filename.tif ', mode= ') W ')
>>> # to write a-image to TIFF file
>>> tif.write_image (image)
Usage Example (pure Python module)
from libtiff import TIFFfile, TIFFimage
# to open a tiff file for reading
tif = TIFFfile('filename.tif')
# to return memmaps of images and sample names (eg channel names, SamplesPerPixel>=1)
samples, sample_names = tiff.get_samples()
# to create a tiff structure from image data
tiff = TIFFimage(data, description='')
# to write tiff structure to file
tiff.write_file('filename.tif', compression='none') # or 'lzw'
del tiff # flushes data to disk
From libtiff import TIFF
From scipy import misc
##tiff file parsing into image sequence
##tiff_image_name: tiff file name;
##out_folder: Folder for saving image sequences
##out_type: Save the type of image, such as .jpg, .png, .bmp, etc.
Def tiff_to_image_array(tiff_image_name, out_folder, out_type):
Tif = TIFF.open(tiff_image_name, mode = "r")
Idx = 0
For im in list(tif.iter_images()):
#
Im_name = out_folder + str(idx) + out_type
Misc.imsave(im_name, im)
Print im_name, 'successfully saved!!!'
Idx = idx + 1
Return
##Image sequence saved as tiff file
##image_dir: The folder where the image sequence is located
##file_name: name of the tiff file to be saved
##image_type: The type of image sequence
##image_num: Number of images to save
Def image_array_to_tiff(image_dir, file_name, image_type, image_num):
Out_tiff = TIFF.open(file_name, mode = 'w')
#It assumed image names are sorted by serial number
For i in range(0, image_num):
Image_name = image_dir + str(i) + image_type
Image_array = Image.open(image_name)
#Zoom into uniform size
Img = image_array.resize((480, 480), Image.ANTIALIAS)
Out_tiff.write_image(img, compression = None, write_rgb = True)
Out_tiff.close()
Return
Read with OpenCV
Import Cv2
cv2.imread ("filename", flags)
For the cv2,imread of the number of channels and bit depth of the flags have four choices:
imread_unchanged = -1# do not convert, such as saving for 16-bit picture, read out still 16 bits.
Imread_grayscale = 0# is converted to grayscale, for example, for 16-bit images, read out for 8-bit, type CV_8UC1.
Imread_color = Convert to RGB three-channel image, image depth to 8-bit
Imread_anydepth = 2# keeps the image depth unchanged and converts it to a grayscale image.
Imread_anycolor = 4# If the image channel number is less than or equal to 3, the original channel number remains unchanged, and if the number of channels is greater than 3, only the first three channels are taken. Image depth to 8-bit
for multichannel TIFF images, to ensure normal image data reading, it is obvious to choose imread_unchanged as the flags set value for Imread.
Install Pylibtiff pil use
Import the Image module. You can then load an image file by using the Open method in the image class. If the load file fails, a ioerror is generated, and if no error is returned, the Open function returns an Image object. Now we can examine the contents of the file through some object properties, namely:
>>> import Image
>>> im = Image.open ("j.jpg")
>>> print Im.format, im.size, Im.mode
JPEG (440,%) RGB
An instance of the Image class has 5 properties, respectively:
Format: Returns the formatting of the picture file in string (JPG, PNG, BMP, None, etc.), or none if it is not an instance obtained from an open file.
Mode: Returns the pattern (RGB, CMYK, etc) of the picture in string. Complete list See official description • Picture Mode list
Size: Returns the size of the picture file (width, height) at two tuple
Palette: Valid only if mode is P, return Imagepalette sample
Info: Returns an overview of the information functions of the sample as a dictionary.
Reading and Writing Images:open (Infilename), Save (Outfilename)
Cutting and pasting and merging Images:
Crop (): Extracts an image of a rectangle's size from an image. It receives a tuple of four elements as a parameter, and the elements are (left, upper, right, lower), and the origin of the coordinate system (0, 0) is the upper left-hand corner.
Paste ():
Merge ():
>>> box = (MB, MB)
>>> region = im.crop (box)
>>> region.show ()
>& gt;> region = Region.transpose (image.rotate_180)
>>> region.show ()
>>> im.paste (region , box)
>>> im.show ()
Rotate a Picture:
def roll (image, Delta):
"roll an image Sideways"
xsize, ysize = image.size
delta = delta% xsize
if delta = = 0:return Image
Part1 = Image.crop ((0, 0, Delta, ysize))
part2 = Image.crop ((delta, 0, Xsize, ysize))
image . Paste (Part2, (0, 0, Xsize-delta, ysize))
Image.paste (part1, (Xsize-delta, 0, Xsize, ysize)) return
image
Geometric transformations
>>>out = im.resize((128, 128)) #
>>>out = im.rotate(45) #Rotate 45 degrees counterclockwise.
>>>out = im.transpose(Image.FLIP_LEFT_RIGHT) #Left and right swap.
>>>out = im.transpose(Image.FLIP_TOP_BOTTOM) #Up and down swap.
>>>out = im.transpose(Image.ROTATE_90) #Rotate a 90 degree angle.
>>>out = im.transpose(Image.ROTATE_180) #Rotate 180 degrees.
>>>out = im.transpose(Image.ROTATE_270) #Rotate 270 degrees.
The thumbnail () method of the image class can be used to make thumbnails. It accepts a two-dollar array as the size of the thumbnail, and then shrinks the example to a specified size.
Import OS, sys from
pil import Image for
infile in sys.argv[1:
outfile = Os.path.splitext (infile) [0] + ". Thu Mbnail "
if infile!= outfile:
try:
im = Image.open (infile)
x, y = im.size im.thumbnail
((X//2, Y//2))
Im.save (outfile, "JPEG")
except IOError:
print "Cannot create thumbnail for", infile
Here we use Im.size to get the size of the original file, and then make the thumbnail with thumbnail (), the size is One-fourth of the previous image file. Similarly, if the document cannot be opened, a prompt that cannot be executed is printed on the terminal. PiL. Image.fromarray (obj, Mode=none)
Creates an image memory from a object exporting the array interface (using the buffer protocol).
If obj is isn't contiguous, then the Tobytes method was called and Frombuffer () is used.
Parameters:
obj–object with array interface
Mode–mode to-use ('ll is determined from type if None) see:modes .
Returns: An
image object.
New in version 1.1.6.
PIL Document