pillow
Pillow is a derivation branch PIL, but now has evolved into more dynamic than PIL own image processing library. pillow can be said to have replaced the PIL, encapsulates the python library (pip to install), and supports python2 and python3, the latest version is 3.0.0.
Pillow's Github page: https://github.com/python-pillow/Pillow
Pillow document (corresponding version V3.0.0):
https://pillow.readthedocs.org/en/latest/handbook/index.html
It is very simple to install pip install pillow
Use:
# Python2 Import Image # python3 (PIL library because it is derived, so you want to import the PIL Image) from PIL Import Image
To python3 example,
- open
from PIL import Image im = Image.open("1.png") im.show()
- format
format attribute defines the format of the image, if the image file from the not open, then the attribute value None; size attribute is a tuple, an image representing the width and height (in pixels); MODE attribute is a schematic of an image, commonly mode as: L to grayscale, RGB true color, CMYK image is pre-press. If the file can not be opened, IOError exception is thrown.
print(im.format, im.size, im.mode)
- save
im.save("c:\\")
- convert()
convert () method is an instance of an object image, accepts a mode parameter for designating a color mode, mode values may be summarized as follows:
·. 1 (. 1 'bit-pixels, Black and White, Stored with One Pixel per byte)
· L (. 8-'bit pixels, Black and White)
· P (. 8-' bit pixels, mapped to the any OTHER MODE the using A Color Palette)
· the RGB (3x8-'bit pixels, to true Color)
· the RGBA (4x8-' bit pixels, to true Color with Transparency mask)
· the CMYK (4x8-'bit pixels, Color Separation)
· the YCbCr (3x8-' bit pixels, Color Video the format)
· the I (32-'bit Signed Integer pixels)
· F. (32-' bit Floating Point pixels )
im = Image.open('1.png').convert('L')
Filter
from the PIL Import Image, ImageFilter IM = Image.open ( '. 1 .png') # Gaussian blur im.filter (ImageFilter.GaussianBlur) # ordinary fuzzy im.filter (ImageFilter.BLUR) # edge enhancement im.filter (ImageFilter.EDGE_ENHANCE ) # find the edge im.filter (ImageFilter.FIND_EDGES) # relief im.filter (ImageFilter.EMBOSS) # contour im.filter (ImageFilter.CONTOUR) # sharpening im.filter (ImageFilter.SHARPEN) # smooth im.filter (ImageFilter .SMOOTH) # details im.filter (ImageFilter.DETAIL)
View image histogram
im.histogram()
Convert image file format
def img2jpg(imgFile): if type(imgFile)==str and imgFile.endswith(('.bmp', '.gif', '.png')): with Image.open(imgFile) as im: im.convert('RGB').save(imgFile[:-3]+'jpg') img2jpg('1.gif') img2jpg('1.bmp') img2jpg('1.png')
Screenshots
from the PIL Import ImageGrab IM = ImageGrab.grab ((0,0,800,200)) # image of the specified area of the screen taken IM = ImageGrab.grab () # without parameters indicates full screenshot
Cutting and pasting images
= Box (120, 194, 220, 294) # define clipping region Region = im.crop (Box) # Crop Region = region.transpose (Image.ROTATE_180) im.paste (Region, Box) # Paste
Image scaling
im.resize = IM ((100,100)) # parameter indicates the new size of the image, respectively represent the width and height
Image contrast enhancement
from the PIL Import Image from the PIL Import The ImageEnhance # original image Image Image.open = ( ' lena.jpg ' ) image.show () # brightness enhancement enh_bri = ImageEnhance.Brightness (Image) Brightness = for 1.5 image_brightened = enh_bri.enhance (Brightness) image_brightened.show () # chroma enhancement enh_col = ImageEnhance.Color (Image) Color = for 1.5 image_colored = enh_col.enhance (Color) image_colored.show () #Contrast enhancement enh_con = ImageEnhance.Contrast (Image) Contrast = for 1.5 image_contrasted = enh_con.enhance (Contrast) image_contrasted.show () # sharpness enhancement enh_sha = ImageEnhance.Sharpness (Image) the sharpness = 3.0 image_sharped = enh_sha.enhance (the sharpness) image_sharped .show ()
Image module usage presentation
1 Introduction.
Image processing is a very wide application of technology, and has a very rich library of third-party extensions Python certainly will not miss this feast door. PIL (Python Imaging Library) is the most commonly used Python image processing library, the current version is 1.1.7, we can here to download study and find information.
Image class PIL library is a very important class to create an instance of this class by directly loading image files can be read and processed image grab image obtained by the method of the three methods.
2. Use.
Image Import module. Then open method Image class to load an image file. If you load a file fails, it will cause a IOError; if no error is returned, the open function returns an Image object. Now, we can check the file contents through a number of object attributes, namely:
>>> import Image >>> im = Image.open("j.jpg")
>>> im.show() >>> print im.format, im.size, im.mode JPEG (440, 330) RGB
There are three attributes, one by one we understand.
format: identification image source format, if the file is not read from the file, the values were set to None.
size: returns a tuple, there are two elements, a value of the pixel width and height sense.
mode: RGB (true color image), in addition, L (luminance), CMTK (pre-press image).
from PIL import Image from PIL import ImageEnhance import pytesseract import re pytesseract.pytesseract.tesseract_cmd = 'D:\\Program Files\\Tesseract-OCR\\tesseract.exe' tessdata_dir_config = '--tessdata-dir "D:\\Program Files\\Tesseract-OCR\\tessdata"' im=Image.open("./img/10.jpg") im=im.convert('L') im.show() im=ImageEnhance.Contrast(im) im=im.enhance(1) #im = im.resize((300, 90)) ltext = pytesseract.image_to_string(im) ltext = re.sub("\W", "", ltext) im.show() print(ltext) #print(pytesseract.image_to_string(im)) #print(pytesseract.image_to_boxes(im)) #print(im.format, im.size, im.mode)
convert (): This function can be used to convert the image to a different color mode.
ImageEnhance.Contrast (im): Use ImageEnhance can enhance the recognition rate picture
other
Simple geometric transformation.
OUT = im.resize >>> ((128, 128)) # resize images >>> im.rotate OUT = (45) # rotated counterclockwise 45 degrees. OUT = im.transpose >>> (Image.FLIP_LEFT_RIGHT) # left swapped. OUT = im.transpose >>> (Image.FLIP_TOP_BOTTOM) # vertical swapped. OUT = im.transpose >>> (Image.ROTATE_90) # rotation angle of 90 degrees. OUT = im.transpose >>> (Image.ROTATE_180) # rotation angle of 180 degrees. OUT = im.transpose >>> (Image.ROTATE_270) # rotated 270 degrees.
Sequence of images.
That is, we often see the dynamic map, the most common suffix .gif, in addition to FLI / FLC. PIL library for this movie format chart also provides some basic support. When we open this type of image file, PIL automatically loads the first frame image. We seek and tell methods may be used to move between frames.
import Image im.seek(1) # skip to the second frame try: while 1: im.seek( im.tell() + 1) # do something to im except EOFError: pass