Python Learning note __13.1 Three-party module Pillow

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

The Python platform de facto image Processing Standard library is PIL: Python Imaging library. A group of volunteers created a compatible version based on PiL, named Pillow

1 , manipulating images

From PIL import Image

# Open a JPG image file, note the current path:

im = image.open(' test.jpg ')

# Get Image size:

W, h = im.size

Print (' Original image size:%sx%s '% (w, h))

# Zoom to 50%:

Im. thumbnail ((W//2, H//2))

Print (' Resize image to:%sx%s '% (W//2, H//2))

# Save the scaled image as thumbnail.jpg in JPEG format:

Im. Save (' thumbnail.jpg ', ' jpeg ')

2 , Blur effect

From PIL import Image, ImageFilter

# Open a JPG image file, note the current path:

im = image.open(' test.jpg ')

# Apply Blur filter:

Im2 = im. filter (Imagefilter.blur)

Im2.save (' blur.jpg ', ' jpeg ')

3 , generate the letter Captcha picture

From PIL import Image, Imagedraw, Imagefont, ImageFilter

Import Random

# Random Letters:

Def Rndchar ():

Return Chr (Random.randint (65, 90))

# Random Color 1:

Def rndcolor ():

Return (Random.randint, 255), Random.randint (255), Random.randint (64, 255))

# Random Color 2:

Def rndColor2 ():

Return (Random.randint (127), Random.randint (127), Random.randint (32, 127))

# Size: 60 x:

width = 60 * 4

Height = 60

# Create Image object, RGB: Color mode, (width, height): Picture size. (255, 255, 255): 16 binary color code, this is white

Image = image.new(' RGB ', (width, height), (255, 255, 255))

# Create a Font object that defines the fonts and font size of the image. Use absolute path to avoid error

Font = Imagefont.truetype("c/windows/fonts/ahronbd.ttf", 36)

# Create a Draw object that will initially create an image object as a parameter

Draw = Imagedraw.draw(image)

# Populate each pixel:

For x in range (width):

For y in range (height):

Draw.point ((x, y), fill=rndcolor ())

# Output Text:

For T in range (4):

Draw.text ((* t + ten), Rndchar (), Font=font, Fill=rndcolor2 ()) # set character position, character, character's font, character color

Fuzzy

Image = Image.filter (Imagefilter.blur)

Image.Save (' code.jpg ', ' jpeg ')

4 , expand Documents

Pillow Official document (https://pillow.readthedocs.io/en/5.1.x/)

python-image basic image processing Operations (https://www.cnblogs.com/kongzhagen/p/6295925.html)


Python Learning note __13.1 Three-party module Pillow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.