WebP與PNG, JPEG的轉換

webp文件是的谷歌制定的文件,編碼和解碼固然要用谷歌本身提供的工具libwebp,被整這些有的沒的。

  1. 下載對應平臺的libwebp
  2. 解壓獲得二進制文件,在bin目錄下(編程的使用include和lib目錄下的文件),如下是以windows 64bit爲例,摘自readme.txt。詳細的可使用-h選項查看具體的用法。
path/to/file description
bin/cwebp.exe encoding tool
bin/dwebp.exe decoding tool
bin/gif2webp.exe gif conversion tool
bin/vwebp.exe webp visualization tool
bin/webpinfo.exe webp analysis tool
lib/ static libraries
include/webp headers
test.webp a sample WebP file
test_ref.ppm the 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格式
    • webp文件名不能有空格
  3. 批量轉的話那就是腳本的事了,例如Python3腳本批量將webp轉png(轉換成png後再轉成其餘格式就很簡單了):
import os
import sys

decoder_path = r"path/to/dwebp.exe" # Windows10下其實也支持斜槓/路徑
webp_path = r"path/to/webp" # webp文件所在目錄,webp文件名不能有空格!
res_path = r"path/to/png_res" # 存儲轉換後圖片的目錄,假設是png

if 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} -o {2}".format(
        decoder_path, os.path.join(webp_path, f), os.path.join(res_path, res_f))
    os.system(cmd)
相關文章
相關標籤/搜索