介绍
这篇文章主要介绍Python如何实现图片尺寸缩放脚本,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Python如何实现图片尺寸缩放脚本
实现方法:
#,编码=utf-8 import Image import shutil import os , , class 图形:,=,infile & # 39; D: \ \ myimg.jpg& # 39;,=,outfile & # 39; D: \ \ adjust_img.jpg& # 39;, , ,@classmethod ,def fixed_size (cls,宽度,高度):, “““才能按照固定尺寸处理图片“““, 我才能=,Image.open (cls.infile), out 才能=,im.resize((宽度,高度),Image.ANTIALIAS), out.save才能(cls.outfile), , ,@classmethod ,def resize_by_width (cls, w_divide_h):, “““才能按照宽度进行所需比例缩放“““, 我才能=,Image.open (cls.infile), ,,(x, y),=, im.size 时间=x_s 才能;x y_s 才能=,x/w_divide_h out 才能=,im.resize ((y_s间也),Image.ANTIALIAS), out.save才能(cls.outfile), , ,@classmethod ,def resize_by_height (cls, w_divide_h):, “““才能按照高度进行所需比例缩放“““, 我才能=,Image.open (cls.infile), ,,(x, y),=, im.size x_s 才能=,y * w_divide_h 时间=y_s 才能;y out 才能=,im.resize ((y_s间也),Image.ANTIALIAS), out.save才能(cls.outfile), , ,@classmethod ,def resize_by_size (cls,大小):, “““才能按照生成图片文件大小进行处理(单位KB)“““, *=size 才能;1024年, 我才能=,Image.open (cls.infile), 时间=size_tmp 才能;os.path.getsize (cls.infile), q 才能=,100, while 才能;size_tmp 祝辞,size 以及q 祝辞,0:, ,,print q ,,out =, im.resize (im.size, Image.ANTIALIAS), ,,out.save (cls.outfile,,质量=问), ,,size_tmp =, os.path.getsize (cls.outfile), ,,q -=, 5, if 才能;q ==, 100:, ,,shutil.copy (cls.infile, cls.outfile), , ,@classmethod ,def cut_by_ratio (cls,宽度,高度):, “““才能按照图片长宽比进行分割“““, 我才能=,Image.open (cls.infile), width 才能=,浮子(宽度), height 才能=,浮子(高度), ,,(x, y),=, im.size if 才能;width 祝辞,高度:, ,,region =, (0,, int ((y - (y *,(时间/height 宽度)))/2),,x,, int ((y + (y *,(时间/height 宽度)))/2)), elif 才能width & lt;,高度:, ,,region =, (int ((x - (x *,(时间/width 高度)))/2),,0,,int ((x + (x *,(时间/width 高度)))/2),,y), ,,: ,,region =, (0, 0, x,, y), , #才能裁切图片, 时间=crop_img 才能;im.crop(地区), #才能保存裁切后的图片, crop_img.save才能(cls.outfile)
以上是“Python如何实现图片尺寸缩放脚本”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!