Python image processing Library (PIL) Installation and simple use

Source: Internet
Author: User
Tags image processing library

The following error is reported when an image processing program is created on the server running the Python environment today:

NameError: global name 'Image' is not defined

After importing the Image, it is found that Python does not have its own Image processing library and needs to be installed independently ...... Check that the commonly used image processing library in Python is called PIL, which can be installed using pip ~ Therefore, use virtualenv to input pip install PIL.

The installation was completed very quickly, so I refreshed it happily and waited for the program to pass, and an error was returned:

IOError: decoder jpeg not available

Google found that PIL installed through pip won't install jpeg decoder ...... Check the Installation Log:

--------------------------------------------------------------------PIL 1.1.7 SETUP SUMMARY--------------------------------------------------------------------version       1.1.7platform      linux2 2.7.5 (default, Sep 18 2013, 09:53:07)  [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]--------------------------------------------------------------------*** TKINTER support not available*** JPEG support not available*** ZLIB (PNG/ZIP) support not available*** FREETYPE2 support not available*** LITTLECMS support not available--------------------------------------------------------------------To add a missing option, make sure you have the requiredlibrary, and set the corresponding ROOT variable in thesetup.py script.

JPEG support not available ...... Jpg is not supported ......

So I had to install it manually:

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gztar xvfz Imaging-1.1.7.tar.gz

After downloading and decompressing the package, go to the decompressed directory and find Imaging-1.1.7/setup. in the file py, modify the following lines of code (the default value of TCL_ROOT is set to NONE, and the path to the system library must be uploaded here ):

TCL_ROOT = "/usr/lib64/"JPEG_ROOT = "/usr/lib64/"ZLIB_ROOT = "/usr/lib64/"TIFF_ROOT = "/usr/lib64/"FREETYPE_ROOT = "/usr/lib64/"LCMS_ROOT = "/usr/lib64/"

Check Before installation:

python /root/bkjia_venv/Imaging-1.1.7/setup.py build_ext -i

Check if there is no problem. You can execute the installation:

python /root/bkjia_venv/Imaging-1.1.7/setup.py install

Installed successfully:

--------------------------------------------------------------------PIL 1.1.7 SETUP SUMMARY--------------------------------------------------------------------version       1.1.7platform      linux2 2.7.5 (default, Sep 18 2013, 09:53:07)              [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]--------------------------------------------------------------------*** TKINTER support not available--- JPEG support available--- ZLIB (PNG/ZIP) support available--- FREETYPE2 support available*** LITTLECMS support not available--------------------------------------------------------------------

Now jpg is supported and the program is successfully executed. Here, we will record the process to facilitate later users. By the way, you can use Tornado to upload images and generate thumbnails:

import timeimport tempfileimport Imageclass AsciiImageProcessHandler(tornado.web.RequestHandler):    def post(self):        if self.request.files:            for f in self.request.files['image']:                rawname = f['filename']                dstname = str(int(time.time()))+'.'+rawname.split('.').pop()                thbname = "thumb_"+dstname                self.write( dstname )                tf = tempfile.NamedTemporaryFile()                tf.write(f['body'])                tf.seek(0)                # create normal file                # img = Image.open(src)                img = Image.open(tf.name)                img.thumbnail((920,920),resample=1)                img.save("./static/upload/asciiimg/"+dstname)                # create thumb file                img.thumbnail((100,100),resample=1)                img.save("./static/upload/asciiimg_tn/"+thbname)                 tf.close()

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.