[Python] Image Compression

Source: Internet
Author: User

Because some compression technologies are required to build my photography website to speed up Website access.

So I learned how to compress images in a python environment.


1. Download and install PIL

After you download and install the package, django reports an error saying that the PIL package has an error.

Then, I google the error and found django on this page, saying that PIL cannot be used and pillow must be used.

So I have to uninstall PIL again.

When I used pip and uninstall pil, I found that my pip version was too low and needed to be upgraded. Finally, I used easy_install to upgrade pip.


Reinstall pillow. You 'd better uninstall the earlier version of pillow and then install it again.

Recovery completed .... Too pitfall...


2. When PIL is used, the IOError: decoder jpeg not available error occurs.

Solution:

install libpng and libjpeg package (combo installer) from this link: http://ethan.tira-thompson.com/Mac_OS_X_Ports.html
sudo pip install -I pillow

3. Use PIL to compress the image and reference it from the http://fc-lamp.blog.163.com/blog/static/174566687201282424018946/

# Coding: UTF-8 ''' python Image processing @ author: fc_lamp @ blog: http://fc-lamp.blog.163.com/'''from PIL import image as Image # proportional Compression image def resizeImg (** args ): args_key = {'ori _ img ': '', 'dst _ img':'', 'dst _ W': '', 'dst _ H ':'', 'Save _ Q': 75} arg = {} for key in args_key: if key in args: arg [key] = args [key] im = image. open (arg ['ori _ img ']) ori_w, ori_h = im. size widthRatio = heightRatio = None ratio = 1 if (ori_w and ori_w> arg ['dst _ W']) or (ori_h and ori_h> arg ['dst _ H']): if arg ['dst _ W'] and ori_w> arg ['dst _ W']: widthRatio = float (arg ['dst _ W']) /ori_w # correct way to obtain decimals if arg ['dst _ H'] and ori_h> arg ['dst _ H']: rows = float (arg ['dst _ H'])/ori_h if widthRatio and heightRatio: if widthRatio 



Related Article

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.