Gifsicle的Python 封装
来源:互联网 发布:女生一个人旅行 知乎 编辑:程序博客网 时间:2024/05/16 16:13
gifsicle的Python封装
感觉gifsicle 还是一款比较强大的gif图片处理软件,功能包括了:
1、对gif中所有帧的任意位置切割;
2、gif中任意帧的修改、分解,并提供多幅gif图片中的任意帧的合并操作
3、gif图片的缩小与放大,包括等比例的缩放。
4、控制gif的循环播放次数以及播放周期
5、and so on
在对Gifsicle的封装过程中,我主要是将其分成两部分(其实整个封装应该是三部分内容),首先是对传入图片的处理,接着对整体的输出图片处理。将每一张需要处理的图片创建一个实例,然后记录对这张图片的操作,最后将其添加进输出图片实例中,并对输出图片的相关修改操作,最后执行convert方法输出图片。
#! /usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'glcsnz123'import osimport matplotlibimport commandsclass GifInfo:#gifsicle -I def __init__(self, imgfile): if not os.path.isfile(imgfile): return self.src = imgfile self.__rotate = "" self.__crops = "" self.__resizes = "" def resize_gif(self, width=None, height=None): if width is None and height is None: return False if width is None: self.__resizes = " --resize-height %d " % height return True if height is None: self.__resizes = " --resize-width %d " % width return True self.__resizes = " --resize %dx%d" % (width, height) return True def resize_fit_gif(self, width=None, height=None): if width is None and height is None: return False if width is None: self.__resizes = " --resize-fit-height %d " % (height) return True if height is None: self.__resizes = " --resize-fit-width %d " % (width) return True self.__resizes = " --resize-fit %dx%d " % (width, height) return True def fix_scale(self, Xscale, Yscale=None): self.__resizes = " --scale " + str(Xscale / 100.0) if Yscale is not None: self.__resizes += "x" + str(Yscale / 100.0) self.__resizes += " " def rotate_gif(self, degree=0): if degree == 90 or degree == "90": self.__rotate = " --rotate-90 " elif degree == 180 or degree == "180": self.__rotate = " --rotate-180 " elif degree == 270 or degree == "270": self.__rotate = " --rotate-270 " else: return False return True def crop_gif_bypos(self, lefttop, rightdown): if rightdown[0] < lefttop[0] or rightdown[1] < lefttop[1]: return False self.__crops = " --crop " + ','.join(map(str, lefttop)) + "-" + ",".join(map(str, rightdown)) + " " return True def crop_gif_bywh(self, lefttop, wh): if wh[0] <= 0 or wh[1] <= 0: return False self.__crops = " --crop " + ",".join(map(str, lefttop)) + "+" + "x".join(map(str, wh)) + " " return True def __str__(self): return " ".join([self.__crops, self.__rotate, self.__resizes, self.src])class Gifsicle: def __init__(self): pass def convert(self, infile, outfile=None): if outfile is None: res = commands.getstatusoutput("gifsicle --batch " + str(infile)) if res[0] == 0: return True return False res = commands.getstatusoutput("gifsicle " + str(infile) + " > " + outfile) if res[0] == 0: return True return Falseif __name__ == '__main__': gi = GifInfo("/home/glcsnz123/images/psb.gif") gi.crop_gif_bywh((23, 23), (220, 220)) gi.rotate_gif(90) gi.fix_scale(95, 95) gf = Gifsicle() gf.convert(gi, "/home/glcsnz123/images/test.gif") print str(gi)
- Gifsicle的Python 封装
- gifsicle使用手册
- Python----类的封装
- 用ImageMagick中的convert以及其它的gifsicle对GIF动态图片进行尺寸修改
- Python封装的性能研究
- Hadoop的Python语言封装
- Python的logging模块封装
- python对象的私有封装
- 【python】Python的logging模块封装
- Python 一个简单的MySQLdb封装类
- python 读取 excel 的方法封装
- 初始python 类的继承与封装
- Python 一个简单的MySQLdb封装类
- python数据库函数的封装调用
- 使用Swig封装的python对象
- python调用java基于pyjnius的封装
- Python针对SSH技术的封装
- python网络分析-network第一版的封装
- apache ng 压力测试
- Web基础:jQuery 中篇:操纵DOM
- list view滚动到顶部第一个item
- .net Page 执行事件顺序
- 向基类构造函数传递参数的(两种方法)
- Gifsicle的Python 封装
- 【VLC】创建管道
- SAP错误问题汇总(转)
- login.js
- google.load()
- Shell脚本学习-命令行参数处理
- The content of element type "session-factory" must match "(property*,mapping*,(class-cache| collect
- 兼容IE、新版Chrome、Firefox,实现本地图片等比例缩放预览
- asp.net页面事件加载顺序