Python implements the image size scaling script,

Source: Internet
Author: User

Python implements the image size scaling script,

Recently, due to the need of the website for image size, I wrote a small script in python to adjust the image size. The special record is as follows:

# Coding = UTF-8 import Image import shutil import OS class Graphics: infile = 'd: \ myimg.jpg 'outfile = 'd: \ adjust_img.jpg '@ classmethod def fixed_size (cls, width, height): "processing images by fixed size" im = Image. open (cls. infile) out = im. resize (width, height), Image. ANTIALIAS) out. save (cls. outfile) @ classmethod def resize_by_width (cls, w_divide_h): "" scale by width "im = Image. open (cls. infile) (x, y) = im. size x_s = x y_s = x/w_divide_h out = im. resize (x_s, y_s), Image. ANTIALIAS) out. save (cls. outfile) @ classmethod def resize_by_height (cls, w_divide_h): "" scale by height "im = Image. open (cls. infile) (x, y) = im. size x_s = y * w_divide_h y_s = y out = im. resize (x_s, y_s), Image. ANTIALIAS) out. save (cls. outfile) @ classmethod def resize_by_size (cls, size): "" processing based on the size of the generated Image file (unit: KB) "" size * = 1024 im = Image. open (cls. infile) size_tmp = OS. path. getsize (cls. infile) q= 100 while size_tmp> size and q> 0: print q out = im. resize (im. size, Image. ANTIALIAS) out. save (cls. outfile, quality = q) size_tmp = OS. path. getsize (cls. outfile) q-= 5 if q = 100: shutil. copy (cls. infile, cls. outfile) @ classmethod def cut_by_ratio (cls, width, height): "" split by Image aspect ratio "im = Image. open (cls. infile) width = float (width) height = float (height) (x, y) = im. size if width> height: region = (0, int (y-(y * (height/width)/2), x, int (y + (y * (height/width)/2) elif width 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.