As follows:
Figure 1 (head picture cut to round, others transparent)
Figure 2 (the 4 corners of the picture plus an ellipse)
I haven't dealt with it before, it's a bit of a struggle to deal with.
Modules used:
The code is as follows:
Import OS, Math
Import Image
Import Imagedraw
1 head pictures cut round, others transparent
Search for a long time, did not find a better way, there is a blog (sorry, forget the blog address) with a more bizarre method, I tried, in addition to processing JPG pictures No format conversion, the other is not a problem, I was the first way to do
The code is as follows:
Def circle ():
IMA = Image.open ("Test.jpg"). Convert ("RGBA")
Size = Ima.size
# because it's going to be round, so 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")
This method calculates the distance from each pixel to the origin (that is, the center point of the picture) to draw a circle.
2. Add an ellipse to the 4 corners of the picture
The code is 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 ')
With this method, think of a thought, head picture cut into a round, the other for transparency, with this method is also possible, so the circle has the following method:
The 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 the last thing I want to have, but through the study of these 2 questions, I saw the Python image processing is powerful, and much more worthy of my study.