In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use pil in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction and installation of PIL function
PIL, full name Python Image Library, the main function is image processing, can be used for image cut, paste, zoom, mirror, watermark, color block, filter, image format conversion, color field space conversion, CAPTCHA, rotating image, image enhancement, histogram processing, interpolation and filtering and other functions. However, it is only supported up to Python 2.7. Pillow is a derivative of PIL, but it has developed into a more dynamic image processing library than PIL itself. What we need to install is Pillow.
Specific uses of PIL:
Image archiving (Image Archives). PIL is ideal for image archiving and image batch processing tasks. You can use PIL to create thumbnails, convert image formats, print images, and so on.
Image display (Image Display). Newer versions of PIL support interfaces such as Tk PhotoImage,BitmapImage and Windows DIB. PIL supports many GUI framework interfaces, which can be used for image display.
Image processing (Image Processing). PIL includes basic image processing functions, including point processing, filter using a large number of convolution kernels (convolution kernels), and color space conversion. The PIL library also supports image size conversion, image rotation, and arbitrary affine transformations. PIL also has some histogram methods that allow you to show some of the statistical properties of the image. This can be used to achieve automatic image contrast enhancement, as well as global statistical analysis.
Pip install Pillow
It seems that Pillow is installed by default. You can check whether it has been installed through pip list.
Second: the basic operation of PIL
The following is the basic operation of opening a picture and saving a picture.
From PIL import Image# 1. Open the picture img = Image.open ("image/10.jpg") # 2. Display picture (thread will be interrupted after displaying picture, restore after closing image) img.show () # 3. Save picture img.save ("image/xiaomai.jpg")
I found in the process of trying, jpg format pictures saved to jpg format will report an error: OSError: cannot write mode RGBA as JPEG, after consulting the data found that the error is mainly due to the difference in the number of channels between jpg format and JPG format.
Jpg is four channels: RGBA means red, green, blue, Alpha color space, Alpha refers to transparency
JPG is three channels: RGB means red, green, blue
Therefore, if you want to save a picture in jpg format to JPG format, you have to discard Channel A:
From PIL import Image# 1. Open the picture img = Image.open ("image/10.jpg") # 2. Display picture (thread will be interrupted after displaying picture, restore after closing image) img.show () # 3. Save picture img = img.convert ("RGB") img.save ("image/xiaomai.jpg")
Here is the code for the image rotation:
From PIL import Image# 1. Open the picture img = Image.open ("pli/7.jpg") # 2. Horizontal flip img1 = img.transpose (Image.FLIP_LEFT_RIGHT) # 3. Save the picture img1.save ("pli/1.jpg") # 4. Vertical flip img2 = img.rotate (180) # 5. Save the photo img2.save ("pli/2.jpg") # 6. Horizontal + vertical flip img3 = img.transpose (Image.FLIP_LEFT_RIGHT). Flip (180) # 7. Save picture img3.save ("pli/3.jpg")
Three: add text to the picture
Two modules of PIL are used to draw text on an image: ImageDraw and ImageFont. ImageDraw is used to create drawing objects, and ImageFont is used to load fonts.
From PIL import Image, ImageDraw, ImageFont# 1. Open the picture img = Image.open ("image/10.jpg") # 2. Call the drawing module draw = ImageDraw.Draw (img) # 3. Set font tfont = ImageFont.truetype ("cute handwritten .ttf", 24) # 4. Add text "" parameter 1: text position in the picture: (x, y) parameter 2: text content parameter 3: font color, of course, color can also be specified with RGB value parameter 4: font type "" draw.text ((50,30), "eyes++", fill= "green", font=tfont) # 5. Save the picture img.save ("image/addWord.jpg") # 6. Show picture img.show ()
Four: PIL filter function from PIL import Image, ImageFilterimg = Image.open ("image/10.jpg") img = img.filter (ImageFilter.CONTOUR) img.save ("image/Filter.jpg") img.show ()
The types of filters are as follows:
PIL mirroring function from PIL import Imageimg = Image.open ("image/10.jpg") img = img.transpose (Image.FLIP_LEFT_RIGHT) img.save ("image/mirror.jpg")
Transpose
There are several modes.
FLIP_LEFT_RIGHT: mirror left and right
FLIP_TOP_BOTTOM: upper and lower mirror images
ROTATE_90: turn 90 degrees counterclockwise
ROTATE_180: turn 180 degrees counterclockwise
ROTATE_270: turn 270 degrees counterclockwise
TRANSPOSE: pixel matrix transpose
TRANSVERSE
I don't know what the last mode means, and I can't find it, but the effect is as follows, blind guessing is diagonal.
In addition to using transpose to create images, it is possible to use rotate, but rotate can only be rotated:
Found that this rotation will have edges and corners, and then through the guidance of a boss who spoke on condition of anonymity, found that the rotate rotation is only pixel rotation, the canvas does not move, so I wrote the following test code:
From PIL import Imageimg = Image.open ("image/12.jpg") img1 = img.transpose (Image.ROTATE_90) img1.save ("image/test.jpg") img2 = img.rotate (90) img2.save ("image/test2.jpg")
It can be found that the small buried rotate rotation will not automatically fill the blank pixels, while the platelet will automatically fill in black. Because the jpg format is non-distorted compression, allows the use of color palette technology similar to the GIF format, supports true color images, and has features such as alpha channels (translucency). The jpg format does not have an alpha channel, so the jpg format can not be transparent, the jpg format can.
Six: picture stitching function
Although it's splicing, it's more like pasting two pictures onto a new canvas.
From PIL import Image, ImageDraw# Open the picture img1 = Image.open ("image/10.jpg") img2 = Image.open ("image/addWord.jpg") # to view the size of the picture Easy stitching image print (img1.size) print (img1.size) # create a blank image. The three parameters are mode (RGB/RGBA), size, color newimg = Image.new (mode= "RGB", size= (1174, 614), color= (255,100,50)) # stitching image, the first parameter is the picture, and the second is the starting position newimg.paste (img1, (0,0)) newimg.paste (img2, (587,0)) newimg.show ()
Seven: PIL clipping function
The method used for image clipping is image.crop (), which can extract a rectangular-sized image from the image. It takes a tuple of four elements as parameters, each element (left, upper, right, lower), and the origin (0,0) of the coordinate system is the upper left corner.
From PIL import Imageimg = Image.open ("image/10.jpg") print (img.size) imgCut = img.crop ((100,200,500,600)) imgCut.show ()
Insert the code chip here
Eight: picture zooming
It may not be 1/2 in this way, but this is a display problem. You can look at the data:
From PIL import Image#: img = Image.open ('image/10.jpg') # get the image size: W, h = img.size# zooms to 50%:img.thumbnail (wAccord 2, hdeband 2)) # Save the scaled image in jpeg format: img.save (' image/zoom.jpg')
If you are interested in learning more about it, you can come to my personal website: eyes++ 's personal website
This is the end of "how to use pil in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.