您好,登錄后才能下訂單哦!
這篇文章主要介紹Python如何實現圖片尺寸縮放腳本,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Python如何實現圖片尺寸縮放腳本
實現方法:
# 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): """按照固定尺寸處理圖片""" 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): """按照寬度進行所需比例縮放""" 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): """按照高度進行所需比例縮放""" 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): """按照生成圖片文件大小進行處理(單位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): """按照圖片長寬比進行分割""" 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 < height: region = (int((x-(x * (width / height)))/2), 0, int((x+(x * (width / height)))/2), y) else: region = (0, 0, x, y) #裁切圖片 crop_img = im.crop(region) #保存裁切后的圖片 crop_img.save(cls.outfile)
以上是“Python如何實現圖片尺寸縮放腳本”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。