WebP与PNG, JPEG的转换

webp文件是的谷歌制定的文件,编码和解码当然要用谷歌自己提供的工具libwebp,被整这些有的没的。

  1. 下载对应平台的libwebp
  2. 解压得到二进制文件,在bin目录下(编程的使用include和lib目录下的文件),以下是以windows 64bit为例,摘自readme.txt。详细的可以使用-h选项查看具体的用法。
path/to/filedescription
bin/cwebp.exeencoding tool
bin/dwebp.exedecoding tool
bin/gif2webp.exegif conversion tool
bin/vwebp.exewebp visualization tool
bin/webpinfo.exewebp analysis tool
lib/static libraries
include/webpheaders
test.webpa sample WebP file
test_ref.ppmthe test.webp file decoded into the PPM format
  1. 其他 --> webp: cwebp [-preset <...>] [options] in_file [-o out_file]
  2. webp --> 其他: dwebp in_file [options] [-o out_file]
    • 不指明格式默认转成PNG格式
  3. 批量转的话那就是脚本的事了,例如Python3脚本批量将webp转png(转换成png后再转成其他格式就很简单了):
import osimport sysdecoder_path = r"path/to/dwebp.exe" # Windows10下其实也支持斜杠/路径webp_path = r"path/to/webp" # webp文件所在目录,需要webp文件名不要有空格!res_path = r"path/to/png_res" # 存储转换后图片的目录,假设是pngif not os.path.exists(res_path) : os.mkdir("result")for f in os.listdir(webp_path): res_f = str(f).replace("webp", "png") # 若webp文件命名有特殊,这里需要改改映射规则 cmd = "{0} {1} {2}".format( decoder_path, os.path.join(webp_path, f), os.path.join(res_path, res_f)) os.system(cmd)

相关文章