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