1. Overview of PIL library: PIL library can complete the functional requirements of image archiving and image processing. (1) Image archiving: batch processing of images, generating image previews, image format conversion, etc .; (2) image processing: basic image processing, pixel processing, color processing, etc.
About the PIL library Image class analysis instructions ()
To load an image file, im = Image.open ('Image save location'), want to save the image im.save ('Want to save the image location')
The following will use some examples to illustrate:
# im.thumbnail = ((128,128)) Generate thumbnails for im, where (128,128) is the size of the thumbnails.
from PIL import Image # Reference PIL library im = Image.open ('C: / Users / 86183 / Desktop / WeChat picture_20200414133515.jpg') # Open image im.thumbnail ((128,128)) # Image thumbnail , The binary (128,128) is the size of the generated image im.save ('C: /Users/86183/Desktop/suolvutu1.jpg') # save
<Original image <thumbnail
Example of changing colors
from PIL import Image im = Image.open ('C: / Users / 86183 / Desktop / WeChat picture_20200414134749.jpg') r, g, b = im.split () # Get RGB channel newg = g.point (lambda i: i * 0.9) #Change the G channel color value to 0.9 times the original newb = b.point (lambda i: i <100) #Select the pixel point with B channel value lower than 100 om = Image.merge (im. mode, (r, newg, newb)) # Combine 3 channels into a new image om.save ('C: / Users / 86183 / Desktop / color change.jpg') # Output image
<Original image: <After changing colors
Image filtering and enhancement: Use the Filter () method of the Image class to use the ImageFilter class, as follows: Image.filter (ImageFliter.fuction)
Get the outline of the image. Original image ibid
from PIL import Image from PIL import ImageFilter im = Image.open ('C: / Users / 86183 / Desktop / WeChat picture_20200414134749.jpg') om = im.filter (ImageFilter.CONTOUR) om.save ('C: / Users / 86183 / Desktop / Get profile.jpg ')
<Outline
Image filtering and enhancement:
Using ImageFilter.EMBOSS can make the image produce relief effect, the specific operation is as follows:
from PIL import Image from PIL import ImageFilter im = Image.open ('C: / Users / 86183 / Desktop / WeChat image_20200414134749.jpg') om = im.filter (ImageFilter.EMBOSS) om.save ('C: / Users / 86183 / Desktop / relief map.jpg ')
<Emboss
Picture character drawing:
Bitmap pictures are a regular distribution composed of pixels of different colors. If you use a character string instead of pixels, the image becomes a character drawing.
from PIL import Image ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxr\ jft/\|()1{}[]?-/+@<>i!;:,\^`.') def get_char(r, b, g, alpha=256): if alpha == 0: return ' ' gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit = 256 / len(ascii_char) return ascii_char[int(gray//unit)] def main(): im = Image.open('file:///C:/Users/86183/Desktop/微信图片_20200414182738.png') WIDTH, HEIGHT = 100,100 im = im.resize((WIDTH, HEIGHT)) txt = "" for i in range(HEIGHT): for j in range(WIDTH): txt += get_char(*im.getpixel((j, i))) fo = open("zifuhua.txt","w") fo.write(txt) fo.close() main()
<Original Picture <Character Picture
In addition to the above examples, PIL can also make GIF images and extract GIF images.
from PIL import Image im = Image.open('C:/Users/86183/Desktop/2016120211583958491.gif') try: im.save('C:/Users/86183/Desktop/tiquzhenshu{:03d}.png'.format(im.tell())) while True: im.seek(im.tell()+1) im.save('C:/Users/86183/Desktop/tiquzhenshu{:02d}.png'.format(im.tell())) except: print('操作完成')
<Original GIF image