Python image processing Library (PIL) Installation and simple use

Source: Internet
Author: User
Tags image processing library

Python image processing Library (PIL) Installation and simple use

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.7
Platform 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 required
Library, and set the corresponding ROOT variable in
Setup. 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.gz

Tar 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 /"
Export _root = "/usr/lib64 /"
ZLIB_ROOT = "/usr/lib64 /"
Export _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.7
Platform 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 time
Import tempfile
Import Image

Class 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 ()

-------------------------------------- Split line --------------------------------------

Install Python3.4 on CentOS source code

Python core programming version 2. (Wesley J. Chun). [Chinese version of hd pdf]

Python development technology details. (Zhou Wei, Zong Jie). [hd PDF scan version + book guide video + code]

Obtain Linux information using a Python script

Build a desktop algorithm transaction research environment using Python in Ubuntu

A Brief History of Python Development

Python details: click here
Python: click here

This article permanently updates the link address:

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.