SoFunction
Updated on 2024-07-15

The use of python's pil in detail

I: PIL function introduction and installation

PIL, full name Python Image Library, the main role is image processing, can be used for image cut, paste, scaling, mirroring, watermarking, color blocking, filters, image format conversion, color field space conversion, CAPTCHA, rotate images, 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 has evolved into a more dynamic image processing library than PIL itself. All we need to install is Pillow.

PIL Specific Uses:

  • Image Archives PIL is perfect for image archiving and batch processing tasks with images. You can use PIL to create thumbnails, convert image formats, print images and more.
  • Image Display (Image Display). PIL newer versions support including Tk PhotoImage, BitmapImage and Windows DIB and other interfaces. PIL supports a number of GUI framework interfaces that can be used for image display.
  • Image Processing (Image Processing). PIL includes basic image processing functions, including point processing, using a number of convolution kernels (convolution kernels) to do filtering (filter), and color space conversion. PIL library also supports image size conversion, image rotation, and arbitrary affine transformations. There are also histogram methods that allow you to show some of the statistical properties of the image. This can be used to achieve automatic contrast enhancement of images, as well as global statistical analysis.

pip install Pillow

It looks like Pillow is installed by default and can be accessed through thepip listCheck if it is already installed

II: Basic operation of PIL

The following are the basic operations for opening an image to save it.

from PIL import Image

# 1. Open the picture
img = ("image/")
# 2. Show image (thread will be interrupted after execution of show image, resumed after closing image)
()
# 3. Save the picture
("image/")

在这里插入图片描述

In the course of my attempts, I found that images in jpg format report an error when saved as jpg:OSError: cannot write mode RGBA as JPEGAfter reviewing the information found that the reason for the error is mainly because the number of channels of jpg format and JPG format images are different.

  • jpg is four-channel: RGBA means Red, Green, Blue, Alpha color space, Alpha means transparency
  • JPG is three-channel: RGB means red, green, blue

So, to save a jpg format image as a JPG format you have to discard the A channel:

from PIL import Image

# 1. Open the picture
img = ("image/")
# 2. Show image (thread will be interrupted after execution of show image, resumed after closing image)
()
# 3. Save the picture
img = ("RGB")
("image/")

在这里插入图片描述

Here is the code to rotate the image:

from PIL import Image

# 1. Open the picture
img = ("pli/")
# 2. Horizontal flip
img1 = (Image.FLIP_LEFT_RIGHT)
# 3. Save the picture
("pli/")
# 4. Vertical flip
img2 = (180)
# 5. Save photos
("pli/")
# 6. Horizontal + Vertical Flip
img3 = (Image.FLIP_LEFT_RIGHT).rotate(180)
# 7. Save the picture
("pli/")

在这里插入图片描述

III: Adding Text to Pictures

To draw text on an image two modules of PIL are used: 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/")
# 2. Call the drawing module
draw = (img)
# 3. Setting fonts
tfont = ("Moe God Handwriting.ttf", 24)
# 4. Add text
"""
    Parameter I:Position of the text in the image:(x, y)
    Parameter II:textual content
    Parameter III:font color,Of course colors can be usedRGBvalue specification
    Parameter IV:Font Type
"""
((50, 30), "eyes++", fill="green", font=tfont)
# 5. Save the picture
("image/")
# 6. Display pictures
()

在这里插入图片描述

IV: PIL filter function

from PIL import Image, ImageFilter
img = ("image/")
img = ()
("image/")
()

在这里插入图片描述

The filter types are as follows:

在这里插入图片描述

V: PIL mirroring function

from PIL import Image
img = ("image/")
img = (Image.FLIP_LEFT_RIGHT)
("image/")

在这里插入代码片transpose

There are several models

  • FLIP_LEFT_RIGHT: left and right mirroring
  • FLIP_TOP_BOTTOM: top and bottom mirroring
  • 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 pattern means, and I haven't looked it up, but the effect is something like the following, a blind guess would be a diagonal pair of turns 。。。。。

在这里插入图片描述

In addition to using transpose to create a mirror image, it is also possible to use rotate, but rotate can only rotate:

在这里插入图片描述

After realizing that rotating this way would be angular, and then after being pointed out by some great guy who wishes to remain anonymous, and realizing that it was rotate rotation is just pixel rotation and the canvas doesn't move, I wrote the following test code:

from PIL import Image

img = ("image/")
img1 = (Image.ROTATE_90)
("image/")
img2 = (90)
("image/")

在这里插入图片描述

It can be noticed that the rotate rotation of Little Burial does not automatically fill blank pixels, while the platelet's will automatically black fill. It's because the jpg format is non-distortively compressed, allows the use of palette technology similar to the GIF format, supports true color images, and has features such as alpha channels (semi-transparency). Whereas the jpg format does not have an alpha channel, so jpg format images cannot be transparent, jpg format can.

VI: Picture stitching function

It's a patchwork, but it's more like pasting two images onto a new canvas

from PIL import Image, ImageDraw

# Open the picture
img1 = ("image/")
img2 = ("image/")
# View image sizes for easy stitching of images
print()
print()
# Create a new blank image, three parameters are mode (RGB/RGBA), size, color
newimg = (mode="RGB", size=(1174, 614), color=(255, 100, 50))
# Splice the picture, the first parameter is the picture, the second is the starting position.
(img1, (0, 0))
(img2, (587, 0))
()

在这里插入图片描述

VII: PIL cropping function

The method used for image cropping is (), this method extracts a certain rectangular size from the image. It receives as an argument a four-element tuple with the elements (left, upper, right, lower) and the origin of the coordinate system (0, 0) being the upper left corner.

from PIL import Image
img = ("image/")
print()
imgCut = ((100, 200, 500, 600))
()

Insert code snippet here

在这里插入图片描述

VIII: Image Zoom

It may not look like one-half that way, but it's a display issue and you can look at the data:

from PIL import Image

# :
img = ('image/')
# Obtain image size.
w, h = 
# Scaled to 50%.
((w//2, h//2))
# Save the scaled image in jpeg format.
('image/')

在这里插入图片描述

If interested in learning more about this, check out my personal website:yes++'s personal website

to this article on the use of python pil article is introduced to this, more related python pil use content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!