Install Python's PIL and Pillow libraries on Linux to process images,

Source: Internet
Author: User
Tags glob webp

Install Python's PIL and Pillow libraries on Linux to process images,

Install
Normally, you only need

pip install PIL==1.1.7

Or

pip install Pillow==2.9.0

You can. However, pay attention to the output after installation.
After the installation is complete, pay attention to the output:

*** TKINTER support not available*** JPEG support not available*** WEBP support not available*** ZLIB (PNG/ZIP) support not available*** FREETYPE2 support not available*** LITTLECMS support not available

Whether there is a required but not supported format. If yes, you need to install the supported packages. Take jpg/png/web as an example.
Library Installation
First, check whether the installation is complete.

ll /usr/lib/libjpeg.*-rw-r--r-- 1 root root 221942 Jun 30 2010 /usr/lib/libjpeg.a-rw-r--r-- 1 root root  918 Jun 30 2010 /usr/lib/libjpeg.lalrwxrwxrwx 1 root root   17 Mar 21 16:19 /usr/lib/libjpeg.so -> libjpeg.so.62.0.0lrwxrwxrwx 1 root root   17 Jan 10 10:44 /usr/lib/libjpeg.so.62 -> libjpeg.so.62.0.0-rw-r--r-- 1 root root 145048 Jun 30 2010 /usr/lib/libjpeg.so.62.0.0

If not, the installation package is required.
Debian:

apt-get install libjpeg8-dev for jpgapt-get install zlib1g-dev for pngapt-get install libwebp-dev for webp

RedHat system:

yum install libjpeg-devel libpng-devel libwebp-devel

After the installation is complete, you need to manually create a soft link
DEBIAN 7 & Ubuntu14.04:

ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/libln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/libln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/libln -s /usr/lib/x86_64-linux-gnu/libwebp.so /usr/lib

Centos 6.5:

ls -s /usr/lib64/libjpeg.so /usr/libls -s /usr/lib64/libz.so /usr/libls -s /usr/lib64/libwebp.so /usr/lib

Reinstall
After the above steps are ready, you can reinstall them in two ways:

pip install -I PIL==1.1.7

-I indicates Force reinstall. Pay attention to the output format support when the installation is complete.
Or download the source code and reinstall it once. Take PIL as an example.

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gztar -xzvf Imaging-1.1.7.tar.gzcd Imaging-1.1.7python setup.py install

Supported formats can be found in the source code directory.

python selftest.py* TKINTER support not installed—- JPEG support ok—- ZLIB (PNG/ZIP) support ok

If there is a problem (not In Debian & Ubuntu, but in Centos 6.5), you need python setup. py build_ext-I and then try again.

Batch conversion of images
This is the requirement. Because the camera pixels are very high and the photos taken are very large, it is too slow to upload the photos to the online album, so we need to first turn the size down, I used to search for image processing software on the Internet. I thought about it later. Now that programming is required, I can do it myself.
The image processing in Python is cool, with several lines of code. Here the pillow library is used.
The code below.

# Coding = UTF-8 from PIL import Image # The pillow library import glob is required, OS in_dir = 'tmp _ photo '# source image directory out_dir = in_dir +' _ out' # After conversion, the image directory percent = 0.4 # scaling ratio if not OS. path. exists (out_dir): OS. mkdir (out_dir) # image batch processing def main (): for files in glob. glob (in_dir + '/*'): filepath, filename = OS. path. split (files) im = Image. open (files) w, h = im. size im = im. resize (int (w * percent), int (h * percent) im. save (OS. path. join (out_dir, filename) if _ name __= = '_ main _': main ()

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.