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.