Python commonly used image processing library
Among the many language python image processing library, opencv-python and PIL two libraries is undoubtedly one of the best. Unfortunately, PIL is not updated for a long time now, but fortunately, PIL in a branch Pillow has been continued for another update, and Pillow installation and use are relatively simple.
installation
pip install pillow
Specific use
Test image
Open an image
from PIL import Image, ImageFilter, ImageOps
import numpy as np
img = Image.open("test.jpg", mode="r") # mode可以不给,给必须是"r"
img common attributes
print(img.size) # (640, 426)
print(img.format) # JPEG,图像格式
print(img.mode) # RGB
print(img.info) # 这个里面的信息目前没搞懂是啥意思
See a more detailed description of the mode
RGB converted to grayscale
img.convert("L")
Results are as follows:
Image filtering
img.filter(ImageFilter.SHARPEN)
Common filtering mode as follows:
ImageFilter.BLUR | Fuzzy Filter |
---|---|
ImageFilter.CONTOUR | Contour filter |
ImageFilter.EMBOSS | Relief filter |
ImageFilter.GaussianBlur | Gaussian blur |
ImageFilter.MedianFilter | Median filter |
ImageFilter.SHARPEN | Sharpen |
Referring more filtering mode and effect
Size Magnification
img.resize((224, 224))
Reading an image from an array np
img2array = np.asarray(img) # 把读取到的图像转成arrary数组
img = Image.fromarray(img2array) # 从np数组中读取图像
Note: np read from the array format having an image attribute not, printing is None
Image rotation
img.rotate(60)
After the save operation image
Image All operations are carried out on the copy, if you want to save the image after the operation must be received with a variable image in operation after the save
img_rotate = img.rotate(60)
img_rotate.save("rotate.jpg")
Display image
img.show()