Home >Backend Development >Python Tutorial >Example tutorial on installing Python's PIL and Pillow libraries for image processing on Linux

Example tutorial on installing Python's PIL and Pillow libraries for image processing on Linux

WBOY
WBOYOriginal
2016-07-06 13:29:491358browse

Installation
Normal situation, just

pip install PIL==1.1.7

or

pip install Pillow==2.9.0

That’s it. But please pay attention to the output after installation
After the installation is complete, please 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

Is there a required but unsupported format? If so, support packages need to be installed. Take jpg/png/web as an example.
Dependent library (library) installation
First check if it is installed

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.la
lrwxrwxrwx 1 root root   17 Mar 21 16:19 /usr/lib/libjpeg.so -> libjpeg.so.62.0.0
lrwxrwxrwx 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, you need to install the package
Debian system:

apt-get install libjpeg8-dev for jpg
apt-get install zlib1g-dev for png
apt-get install libwebp-dev for webp

RedHat system:

yum install libjpeg-devel libpng-devel libwebp-devel

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

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

Centos 6.5:

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

Reinstall
After the above is ready, there are two ways to reinstall:

pip install -I PIL==1.1.7

-I means Force reinstall. When the installation is complete, pay attention to the output format support.
Or download the source code and reinstall it, taking PIL as an example

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

Format support 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 here (not in Debian & Ubuntu, but Centos 6.5), you need to python setup.py build_ext -i and try again.

Batch conversion of images
The demand is like this. Since cameras now have very high pixels, the photos taken are very large, and it is too slow to upload them to online photo albums, so I need to change the size first. Before, I searched for image processing software directly on the Internet. Later, After thinking about it for a while, since I know how to program, I can definitely do this myself.
Python's image processing is very cool. It can be done with just a few lines of code. The pillow library is used here.
Code below.

#coding=utf-8 
from PIL import Image#需要pillow库 
import glob, os 
in_dir ='tmp_photo'#源图片目录 
out_dir = in_dir+'_out'#转换后图片目录 
percent = 0.4#缩放比例 
if not os.path.exists(out_dir): os.mkdir(out_dir) 
 
#图片批处理 
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() 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn