The recent development of my own blog in Python requires Python to generate a verification code, of course, it must use the Python graphics processing library PIL, because I use Windows.
So after the installation of the PIL began to write, according to the topic of the _IMAGINGFT C module is not installed error, find a lot of suggestions, and finally determine under Windows should use PILLPW. Click the open link to find Pillow?2.5.2.win32?py2.7.exe because I use the python2.7 and Win32 system, so should download this, we can find the response according to their own needs of the package to install, Pillow, It is a revised version of some bug fixes for PiL
Then there is a mistake, that is, when using Image.Save (' code.jpg ', ' jpg '), there will be keyerror: ' jpg ' ERROR,
See StackOverflow article Http://stackoverflow.com/questions/21128256/why-does-pillow-not-recognize-the-jpeg-format
After being inspired, you only need to change the import image to the from PIL import image.
By the way, I enclose a code to produce Chinese verification code.
Import Imagedraw,imagefontFrom PILImport ImageImport RandomImport Math, stringClassRandomchar():@staticmethodDefUnicode(): val = Random.randint (0x4e00,0X9FBF)Return UNICHR (Val)@staticmethodDefGB2312(): Head = Random.randint (0xb0,0xCF) BODY = Random.randint (0xA,0xF) tail = Random.randint (0,0xF) val = (Head <<8) | (Body <<4) | Tail str ="%x"% valReturn Str.decode (' Hex '). Decode (' gb2312 ')ClassImagechar():Def__init__(Self, fontcolor =(0,0,0), size =(100,), Fontpath =' SIMSUN.TTC ', BgColor =(255,255,255), FontSize =: self.size = Size Self.fontpath = Fontpath Self.bgcolor = BgColor Self.fontsize = fontSize Self.fontcolor = fontcolor Self.font = Imagefont.truetype (Self.fontpath, self.fontsize) self.image = Image.new (' RGB ', size, BgColor)DefRotate(self): Self.image.rotate (Random.randint (0,), expand=0)DefDrawText(Self, POS, txt, fill): Draw = Imagedraw.draw (self.image) draw.text (POS, TXT, font=self.font, Fill=fill)Del DrawDefRandrgb(self):Return (Random.randint (0,255), Random.randint (0,255), Random.randint (0,255))DefRandpoint(self): (width, height) = self.sizeReturn (Random.randint (0, width), random.randint (0, height))DefRandline(Self, num): Draw = Imagedraw.draw (self.image)For IIn range (0, num): Draw.line ([Self.randpoint (), Self.randpoint ()], Self.randrgb ())Del DrawDefRandchinese(Self, num): Gap =5 start =0For Iin range (0, num): char = Randomchar (). GB2312 () x = start + self.fontsize * i + random.randint (0, GAP) + gap * I self.drawtext ((x, Random.ra Ndint (-5, 5), Randomchar (). GB2312 (), Self.randrgb ()) self.rotate () self.randline (18) def save ' JPEG ') if __name__== ' __main__ ': IC = Imagechar (fontcolor= (100,211, 90)) Ic.randchinese (4) Ic.save (
Python image processing pil/pillow/generate verification code/KEYERROR: And the _IMAGINGFT C module is not installed