Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Pillow Image processing method of python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian introduces in detail "python Pillow image processing method how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this article "python Pillow image processing method how to use" article can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge.

I. installation

It is considered to be the official image processing library of python

PIL is ideal for image archiving and image batch processing tasks. You can use PIL to create thumbnails, convert image formats, print images, etc.

PIL supports many GUI framework interfaces, which can be used for image display.

The PIL library also supports image size conversion, image rotation, and arbitrary affine transformation.

Pip install Pillow 2. Open the picture from PIL import Imageim = Image.open ("picture.jpg") im.show () 3, convert the format and save from PIL import Imageim = Image.open ("picture.jpg") im.save ("result.png") 4, create a thumbnail from PIL import Imageim = Image.open ("picture.jpg") im.thumbnail ((128,128)) im.save ("result.jpg") 5. Get the image attributes

Get the source of the image, if the image is not read from the file its value is None.

From PIL import Imageim = Image.open ("picture.jpg") print (im.format) VI. Picture information from PIL import Imageim = Image.open ("picture.jpg") print (im.info) 7. Color palette

Returns an instance of the Image Palette class if the mode of the image is "P"; otherwise, it will be None

From PIL import Imageim = Image.open ("picture.jpg") print (im.palette) VIII. Artboard

Generate an artboard using the given variables mode and size

From PIL import Imageim= Image.new ("RGB", (128,128), "# FF0000") im.show () IX. Picture mode

The mode of the image is as follows

LRV 8-bit pixels, black and white

PRV 9-bit pixels, using the color palette to map to any other mode

1:1-bit pixels, black and white images, saved as 8-bit pixels

RGB:3*8 bit pixels, true color

RGBA:4*8 bit pixel, true color + transparent channel

CMYK:4*8 bit pixels, printing four-color mode or color printing mode

YCbCr:3*8 bit pixels, color video format

IRd 32-bit integer pixel

FRV 33-bit floating-point pixel

From PIL import Imageim = Image.open ("picture.jpg") print (im.mode) 10. Mode conversion

Converts the current image to another mode and returns a new image

From PIL import Imageim = Image.open ("picture.jpg") new_im = im.convert ('L') print (new_im.mode) new_im.show () 11. Matrix mode conversion

Use a transformation matrix to convert a "RGB" image to an "L" or "RGB" image

From PIL import Imageim = Image.open ("picture.jpg") print (im.mode) matrix = (0.412453 matrix 0.357580,0.180423, 00.212671 new_im 0.119193, 0.950227, 0) new_im = im.convert ("L", matrix) print (new_im.mode) new_im.show () 12, picture size

Gets the size of the image, which, calculated in terms of the number of pixels, returns a binary of width and height

From PIL import Imageim = Image.open ("picture.jpg") print (im.size) XIII. Channel separation

Returns a tuple of channels in the current image

Separating one RGB image will result in three new images

Three red, green and blue pictures corresponding to each channel of the original image

From PIL import Imageim = Image.open ("picture.jpg") rGrady b = im.split () print (r.mode) print (r.size) print (im.size) XIV, copy, crop, paste, merge from PIL import Imageim = Image.open ("picture.jpg") # copy im.copy () # crop im.crop ((100,100,400,400)) # paste one picture onto another image im.paste (im.transpose (Image.ROTATE_180)) Im) # merge classes use some single-channel images Create a new image Image.merge ("RGB", im.split ()) XV. Geometric transformation

Image class has resize (), rotate () and transpose (), transform () methods for geometric transformation.

From PIL import Imageim = Image.open ("picture.jpg") # modify size im.resize ((128,128)) # Angle rotation im.rotate (45) # returns a copy of the current image that is flipped or rotated # the value is: FLIP_LEFT_RIGHT,FLIP_TOP_BOTTOM,ROTATE_90,ROTATE_180,ROTATE_270im.transpose (Image.ROTATE_90) # generates a new image with a given size Have the same mode as the original image im.transform ((200,200), Image.EXTENT, (0,0,300,300)) XVI. Advanced image processing

Using the ImageEnhance object, you can set it up quickly. You can adjust contrast, brightness, color balance, and sharpness

From PIL import Imagefrom PIL import ImageEnhanceim = Image.open ("picture.jpg") enh = ImageEnhance.Contrast (im) enh.enhance. Show ("30% more contrast") 17. Filter processing from PIL import Imagefrom PIL import ImageFilterim = Image.open ("picture.jpg") # mean filter im1 = im.filter (ImageFilter.BLUR) # contour finding im2 = im.filter (ImageFilter.CONTOUR) # Edge detection im3 = im.filter (ImageFilter.FIND_EDGES) im.show () im1.show () im2.show () im3.show () 18, set transparency to compose two images

A new image is generated using the given two images and the transparency variable alpha

The variable alpha value is between 0 and 1

If the variable alpha is 0. 0, a copy of the first image is returned. If the variable alpha is 1. 0, a copy of the second image is returned.

From PIL import Imageim1 = Image.open ("picture1.jpg") im2 = Image.open ("picture2.jpg") im = Image.blend (im1, im2, 0.30) im.show () XIX. Set mode to combine two pictures

Generate a new image using the given two images and the mask image as transparency

The mode of the image can be "1", "L" or "RGBA"

All images must have the same size

From PIL import Imageim1 = Image.open ("picture1.jpg") im2 = Image.open ("picture2.jpg") im = Image.composite (im1, im2,'L') im.show () 20, draft mode

Situations where speed is higher than quality.

Allows you to convert the picture to a given mode and size as much as possible without reading the contents of the file (which may not be exactly equal to the given parameters), which is very effective when generating thumbnails

From PIL import Imageim = Image.open ("picture.jpg") print (im.size,im.mode) new_im = im.draft ("L", (200200)) print (new_im.size,new_im.mode) new_im.show () 21, get the channel name

Returns a tuple including the name of each channel

For example, for RGB images will return ("R", "G", "B")

From PIL import Imageim = Image.open ("picture.jpg") print (im.getbands ()) 22, get bounding box

A bounding box for calculating the non-zero area of an image

This bounding box is a 4-tuple that defines the coordinates of left, upper, right, and lower pixels.

If the image is empty, this method will return empty

From PIL import Imageim = Image.open ("picture.jpg") print (im.getbbox ()) 23, get pixel value from PIL import Imageim = Image.open ("picture.jpg") print (im.getdata ()) 24, get picture extreme value

Returns a 2-tuple

Include the minimum and maximum values in the image

From PIL import Imageim = Image.open ("picture.jpg") print (im.getextrema ()) 25, specified location pixel value from PIL import Imageim = Image.open ("picture.jpg") print (im.getpixel (10L0) 26, acquired image histogram from PIL import Imageim = Image.open ("picture.jpg") im_histogram = im.histogram () print (im_histogram [0]) 27, memory allocation

Allocate memory for the image and load it from the file

Returns a pixel access object for reading and modifying pixels

From PIL import Imageim = Image.open ("picture.jpg") pix = im.load () print (pix [0Magne2]) 28, find the specified frame

Find the specified frame in the given dynamic graph

If the lookup goes beyond the end of the sequence, an EOFError exception is generated

When the file is opened, the PIL library is automatically assigned to frame 0

From PIL import Imageim_gif = Image.open ("loading.gif") print (im_gif.mode) # defaults to frame 0 im_gif.show () im_gif.seek (1) im_gif.show () im_gif.seek (3) im_gif.show () 29, Get the current frame position from PIL import Imageim_gif = Image.open ("loading.gif") print (im_gif.tell ()) im_gif.seek (3) print (im_gif.tell ()) read here This article "how to use python's Pillow image processing method" has been introduced, and if you want to master the knowledge points of this article, you still need to practice and use it to understand it. If you want to know more about the article, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report