Example of processing rounded and circular images using python

Source: Internet
Author: User

As follows:


Figure 1 (the Avatar is cropped in a circular shape, and the others are transparent)


Figure 2 (add an ellipse to the four corners of the image)


I have never handled it before. It is a little laborious to handle it.

Modules used:
Copy codeThe Code is as follows:
Import OS, math
Import Image
Import ImageDraw

1. the Avatar is cropped in a circular shape and the others are transparent.

After searching for a long time, I didn't find a good method. I used a strange method for a blog (Sorry, I forgot my blog address). I tried it, no format conversion is available except for processing JPG images. I followed that method first.

Copy codeThe Code is as follows:
Def circle ():

Ima = Image. open ("test.jpg"). convert ("RGBA ")

Size = ima. size

# A square image is required because the circle is required

R2 = min (size [0], size [1])

If size [0]! = Size [1]:

Ima = ima. resize (r2, r2), Image. ANTIALIAS)

Imb = Image. new ('rgba', (r2, r2), (255,255,255, 0 ))

Pima = ima. load ()

Pimb = imb. load ()

R = float (r2/2) # abscissa of the center

For I in range (r2 ):

For j in range (r2 ):

Lx = abs (I-r + 0.5) # abscissa from the center of the circle

Ly = abs (j-r + 0.5) # ordinate the distance from the center to the center

L = pow (lx, 2) + pow (ly, 2)

If l <= pow (r, 2 ):

Pimb [I, j] = pima [I, j]

Imb. save ("test_circle.png ")

This method calculates the distance from each pixel to the origin (that is, the image center point) to draw a circle.


2. Add an ellipse to the four corners of the image
Copy codeThe Code is as follows:
Def circle_corder_image ():

Im = Image. open ("test.jpg"). convert ("RGBA ")

Rad = 10 # Set the radius

Circle = Image. new ('l', (rad * 2, rad * 2), 0)

Draw = ImageDraw. Draw (circle)

Draw. ellipse (0, 0, rad * 2, rad * 2), fill = 255)

Alpha = Image. new ('l', im. size, 255)

W, h = im. size

Alpha. paste (circle. crop (0, 0, rad, rad), (0, 0 ))

Alpha. paste (circle. crop (0, rad * 2), (0, h-rad ))

Alpha. paste (circle. crop (rad, 0, rad * 2, rad), (w-rad, 0 ))

Alpha. paste (circle. crop (rad, rad, rad * 2, rad * 2), (w-rad, h-rad ))

Im. putalpha (alpha)

Im.save('test_circle_corder.png ')

After using this method, I thought about it. The Avatar is cut into a circular image, and the others are transparent. This method is also acceptable, so the following method is used to draw a circular image:
Copy codeThe Code is as follows:
Def circle_new ():

Ima = Image. open ("test.jpg"). convert ("RGBA ")

Size = ima. size

R2 = min (size [0], size [1])

If size [0]! = Size [1]:

Ima = ima. resize (r2, r2), Image. ANTIALIAS)

Circle = Image. new ('l', (r2, r2), 0)

Draw = ImageDraw. Draw (circle)

Draw. ellipse (0, 0, r2, r2), fill = 255)

Alpha = Image. new ('l', (r2, r2), 255)

Alpha. paste (circle, (0, 0 ))

Ima. putalpha (alpha)

Ima.save('test_circle.png ')

Although I have all the things I want at last, I have learned how powerful python image processing is by studying these two questions. Many of them are worth studying.

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.