recently because the site for image size needs, using Python to write a small script, convenient adjustment of image size, special record as follows:

 coding=utf-8 Image import shutil # import import OS class Graphics: infile ='D:\myimg.jpg'outfile ='D: \adjust_img.jpg' @classmethod def fixed_size (CLS, width, height): "in accordance with the fixed size image" Im = (cls.infile) (out = im.resize (width, height), Image.ANTIALIAS) (cls.outfile) @ classmethod def resize_by_width (CLS, w_divide_h): "in accordance with the width of the required proportion zoom" Im = (cls.infile) (x = y) im.size x_s = x y_s = x/w_divide_h (out = im.resize (x_s, y_s), Image.ANTIALIAS (cls.outfile)) @classmethod def resize_by_height (CLS, w_divide_h): "according to the height of the required proportion zoom" Im = (cls.infile) (x, y) = im.size x_s = y*w_divide_h y_s = y (out = im.resize (x_s, y_s), Image.ANTIALIAS (cls.outfile) @classmethod def resize_by_size (CLS), size): "in accordance with the generated image file size for processing (unit KB)" size * = 1024 im = (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) (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): "in accordance with the picture aspect ratio segmentation" Im = (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 < height: = region (int ((x- (x * (width / height))), 0 (/2), int (x+ (x (* * * * width / height) /2, y)))) (else: Region = 0, 0, x, y) # cutting picture crop_img = im.crop (region) # save the trimmed image (cls.outfile) 

all above is the

, want to help everyone to learn, but also hope that more family support script.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Python implementation of picture size scaling script | Script Home

You may also be interested in these articles!