Python uses image to manipulate pictures common techniques analysis

Source: Internet
Author: User
This example describes Python's common techniques for using image to manipulate pictures. Share to everyone for your reference. The specific analysis is as follows:

Using Python to work with pictures is very handy, and here is a little Python code for working with images that requires the Image Processing Toolkit pil (python image Library) to be installed.

#coding =utf-8import imageimport urllib2import stringioimport os# change picture size def resize_img (img_path): try:img = Image.open ( Img_path) (width,height) = Img.size New_width = new_height = Height * new_width/width out = Img.resize ((    new_width,new_height), image.antialias) ext = os.path.splitext (Img_path) [1] new_file_name = '%s%s '% (' small ', ext) Out.save (new_file_name,quality=95) except Exception,e:print e# change Picture type def change_img_type (img_path): try:img = Ima    Ge.open (Img_path) img.save (' new_type.png ') except exception,e:print e# processing remote picture def handle_remote_img (Img_url): try: Request = Urllib2. Request (Img_url) Img_data = Urllib2.urlopen (Request). Read () Img_buffer = Stringio.stringio (img_data) img = Image. Open (Img_buffer) img.save (' remote.jpg ') (width,height) = img.size out = Img.resize ((200,height * 200/width), Ima Ge. AntiAlias) out.save (' remote_small.jpg ') except exception,e:print eif __name__ = = ' __main__ ': Img_path = ' test.jpg' Resize_img (Img_path) change_img_type (img_path) Img_url = ' http://img.hb.aicdn.com/ 042f8a4a70239f724ff7b9fa0fc8edf18658f41022ada-wcitwe_fw554 ' Handle_remote_img (img_url)

Problems that you may encounter

Importerror:no module named Image

Workaround: Install the Python Imaging Library (PIL)
Copy the Code code as follows:

sudo easy_install PIL


Installation PiL appears:
-jpeg Support Not available

-zlib (Png/zip) support not available

-freetype2 Support Not available

Operation JPEG Pictures and PNG images appear:

Ioerror:decoder JPEG not available and Ioerror:encoder zip not available

Workaround:
(1) Remove the installed PiL
Copy the Code code as follows:

sudo rm-rf/usr/local/lib/python2.6/site-packages/pil-1.1.7-py2.6-linux-x86_64.egg/


(2) Install the relevant library
Copy CodeThe code is as follows:

sudo apt-get install Libjpeg8 libjpeg62-dev libfreetype6 Libfreetype6-dev
sudo ln-s/usr/lib/x86_64-linux-gnu/libjpeg.so/usr/lib/
sudo ln-s/usr/lib/x86_64-linux-gnu/libfreetype.so/usr/lib/
sudo ln-s/usr/lib/x86_64-linux-gnu/libz.so/usr/lib/


(3) Reinstall PIL
Copy CodeThe code is as follows:

sudo easy_install PIL


Terminal appears:

-jpeg Support Available
-zlib (png/zip) support available
-FREETYPE2 Support Available

Now try it, it's OK.

Hopefully this article will help you with Python programming.

  • 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.