Python image processing pil/pillow/generate verification code/appear keyerror: And the _IMAGINGFT C module is not installed

Source: Internet
Author: User
Tags drawtext

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 PIL import Image import randomimport Math, String Class Randomchar (): @staticmethod de F Unicode (): val = Random.randint (0x4e00, 0X9FBF) return Unichr (val) @staticmethod def GB2312 (): head = Rand Om.randint (0xb0, 0xCF) BODY = Random.randint (0xA, 0xF) tail = random.randint (0, 0xF) val = (head << 8) | (Body << 4) |  Tail str = "%x"% val return str.decode (' hex '). Decode (' gb2312 ') class Imagechar (): Def __init__ (self, fontcolor =  (0, 0, 0), size = (+), 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.fo ntsize) Self.image = image.new (' RGB ', size, bgColor) def rotate (self): self.image.rotate (random.randint (0, 30), Expand=0) def drawText (self, POS, txt, fill): Draw = Imagedraw.draw (self.image) draw.text (POS, txt, Font=self.font,           Fill=fill) del draw def Randrgb (self): return (random.randint (0, 255), Random.randint (0, 255), Random.randint (0, 255)) def randpoint (self): (width, height) = self.size return (random.randint (0, width), RA      Ndom.randint (0, height)) def randline (self, num): Draw = Imagedraw.draw (self.image) for I in range (0, num):    Draw.line ([Self.randpoint (), Self.randpoint ()], Self.randrgb ()) Del draw def Randchinese (self, num): Gap = 5 Start = 0 for I in range (0, num): char = Randomchar (). GB2312 () x = start + self.fontsize * i + random.randint (0, GAP) + gap * I self.drawtext ((x, Random.randint (-5, 5 )), Randomchar (). GB2312 (), Self.randrgb ()) self.rotate () Self.randline def save (Self, Path): Self.image.save (path, ' JPEG ' If __name__== ' __main__ ': IC = Imagechar (fontcolor= (100,211)) IC. Randchinese (4) ic.save ("1.jpeg") 



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.