将webp转换为png

webp格式体积小,但是很多看图软件不支持该格式。下面的例子套用了Python处理命令行参数的程序,并加入了PIL库进行转化,默认的是png格式,也可以设定为其他的。

  通过`pip install --user pillow`安装python3.x版本的PIL.

 1 #!/usr/bin/python
 2 # -*- coding:utf-8 -*-
 3 
 4 import sys, getopt
 5 from PIL import Image
 6 
 7 
 8 def usage():
 9     print("python web2png.py -i <inputfile> -o <outputfile>")
10 
11 
12 def main(argv):
13     inputfile = ""
14     outputfile = ""
15     flag = "png" #可以设定选择其他格式
16     try:
17         opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="])
18     except getopt.GetoptError:
19         usage()
20         sys.exit(2)
21     for opt, arg in opts:
22         if opt == "-h":
23             usage()
24             sys.exit()
25         elif opt in ("-i", "--ifile"):
26             inputfile = arg
27         elif opt in ("-o", "--ofile"):
28             outputfile = arg
29         else:
30             #print(arg)
31             print("Invalid arg")
32             sys.exit(2)
33     im = Image.open(inputfile)
34     im.save(outputfile, format=flag, lossless=True)  #转换
35 
36 
37 if __name__ == "__main__":
38     main(sys.argv[1:])

 

[1] https://docs.python.org/3/library/getopt.html

[2] https://pypi.org/project/Pillow/

 

posted @ 2023-03-19 17:41  安然春夏  阅读(205)  评论(0)    收藏  举报