rendering is as follows:


figure 1 (avatar pictures cut into round, the other for transparent)


figure 2 (four Angle of ellipse to the picture)


no processing before, to handle is a bit hard ah.

use module:

code is as follows:



import OS, math
import Image
import ImageDraw

1 head pictures cut into round, the other for transparent

search for a long time, didn't find a better method, has a blog (sorry, forgot to blog address) in a more quirky way, I tried it on, in addition to handling JPG image format conversion, the other is no problem, I was in accordance with the first approach to the


code is as follows:


def circle () :

      Ima=Image. Open (" test. JPG "). The convert (" RGBA ")

      Size=ima. Size

      #is round, so need a square picture

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

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

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

      Imb=Image. The new (' RGBA (r2, r2), (255255255, 0))

      Pima=ima. The load ()

      Pimb=imb. The load ()

      R=float (r2)/2 #abscissa center

      For I in range (r2) :

              For j in range (r2) :

                      Lx=abs (#I - r + 0.5) to the center distance of abscissa

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

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

                      If l <(r, 2)=pow:

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

      Imb. Save (" test_circle. PNG ")

this method is to calculate each pixel to the distance to the origin (image center) draw round


2, four Angle of ellipse to the picture

code is as follows:



def circle_corder_image () :

      Im=Image. Open (" test. JPG "). The convert (" RGBA ")

      Rad=10   #set radius  

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

      The draw=ImageDraw. The draw (circle)

      The draw the ellipse ((0, 0, rad * 2, rad * 2), the fill=255)

      Alpha=Image. The new (' L ', im. The 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, want a want, avatar images cut into round, the other is transparent, also can use this method, and then draw circle has the following method:

code is as follows:



def circle_new () :

      Ima=Image. Open (" test. JPG "). The convert (" RGBA ")

      Size=ima. Size

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

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

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

      Circle=Image. The new (' L ', (r2, r2), 0)

      The draw=ImageDraw. The draw (circle)

      The draw the ellipse ((0, 0, r2, r2), the fill=255)

      Alpha=Image. The 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 the study of these two problems, I saw a python a powerful image processing, many is worth me to study.

you might be interested in the article:

  • python judgment the mass-tone attune, obtain a picture of two instance
  • python use proxy fetching website pictures (multithreaded)
  • python scraping of the page in the sample picture
  • python batch download pictures three methods


This concludes the body part