Examples of Python processing fillet pictures, circular pictures _python

Source: Internet
Author: User
Tags abs pow

The effect chart is as follows:


Figure 1 (head picture cut to round, others transparent)


Figure 2 (Add an ellipse to the 4 corners of the picture)


I haven't dealt with it before, it's a bit laborious to deal with.

Modules to use:

Copy Code code as follows:

Import OS, Math
Import Image
Import Imagedraw

1 head picture cut into a round, the other is transparent

Search for a long time, did not find a better way, there is a blog (sorry, forget the blog address) used a more bizarre method, I tried, in addition to processing JPG pictures No format conversion, the other is no problem, I was the first to follow the method to

Copy Code code as follows:

Def circle ():

IMA = Image.open ("Test.jpg"). Convert ("RGBA")

Size = Ima.size

# because it's a circle, you need a square picture

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) #圆心横坐标

For I in range (R2):

For j in Range (R2):

LX = ABS (i-r+0.5) #到圆心距离的横坐标

ly = ABS (j-r+0.5) #到圆心距离的纵坐标

L = Pow (lx,2) + POW (ly,2)

If L <= Pow (R, 2):

PIMB[I,J] = Pima[i,j]

Imb.save ("Test_circle.png")

The method is to calculate the distance from each pixel to the origin (that is, the center point of the picture) to draw a circle.


2, to the picture of the 4 corners plus the Oval

Copy Code code as follows:

Def circle_corder_image ():

im = Image.open ("Test.jpg"). Convert ("RGBA")

rad = 10 # Set 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, Rad, 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, think of a thought, head picture cut into a circle, the other is transparent, with this method is also possible, so draw the circle has the following method:

Copy Code code 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 finally I want to have, but through to these 2 questions research, I saw the Python picture processing formidable, many also is worth me to study the research.

Related Article

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.