webp文件是的谷歌制定的文件,編碼和解碼當然要用谷歌自己提供的工具libwebp,別整那些有的沒的的方法。
如果再pc上的瀏覽器(如Chrome,Edge等)打開微信的推送,爬蟲爬取到圖片可能就是webp格式的
- 下載對應平台的libwebp
- 解壓得到二進制文件,在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 |
- 其他 --> webp:
cwebp [-preset <...>] [options] in_file [-o out_file]
- webp --> 其他:
dwebp in_file [options] [-o out_file]
- 不指明格式默認轉成PNG格式
- webp文件名不能有空格
- 批量轉的話那就是腳本的事了,例如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)