こんにちは。
この記事では、画像を一括で WebP 形式に変換する方法を紹介します。
変換ツールとしては Google が公式で出している libwebp を使いますが、そのままでは使いにくいため、複数の画像をドラッグ&ドロップするだけで変換できるようにします。
変換ツールのダウンロードと使い方
libwebpのダウンロード手順
まず、以下のページの「downloads repository」から、最新版のファイルをダウンロードします。
因みに、2021/02/05 現在の Windows 用の最新版は「libwebp-1.2.0-windows-x64.zip」です。
ダウンロードしたら、適当な場所に解凍しておきます。
libwebpの使い方
ここは興味のある方向けなので、読み飛ばしても大丈夫です。
まず、コマンドプロンプトを開いて、解凍したフォルダに移動します。
png, jpg, gif の画像は以下のように webp に変換できます。
bin\cwebp -lossless image0.png -o image0.webp
bin\cwebp image1.jpg -o image1.webp
bin\cwebp image2.gif -o image2.webp
png から webp は可逆圧縮が可能なので、損失なく変換したい場合は上記のようにオプション -lossless
を指定します。
また、webp を別形式に変換することもできます。
bin\dwebp image3.webp -o image3.png
webp画像が見られない場合は、以下のように可視化することができます。
bin\vwebp image0.webp
このように変換や可視化ができますが、これをすべての画像に対して手作業で行うのは手間なので、まとめて処理できるようにバッチファイルを作成します。
バッチファイルの作成と使い方
webp_batch_converter.batの保存手順
バッチファイルは複数の処理をまとめて書いておくことができるWindows用のファイルです。
(LinuxやUNIXのシェルスクリプトのようなもの)
選択した複数の画像をまとめて変換する処理を書いたバッチファイルを作成することで、自動での一括変換が可能になります。
今回作成したバッチファイルの保存方法は以下に示す通り、ダウンロードリンクからダウンロードするか、メモ帳などにテキストをコピーして保存します。
ダウンロードリンクから保存
まず、以下のリンクからダウンロードしてください。
次に、ダウンロードしたファイルを右クリックして「プロパティ」を開きます。
表示された画面内の下の方に書かれている「セキュリティ: このファイルは……(略)」の部分にある「許可する」にチェックをつけ、「OK」をクリックします。
テキストで保存
メモ帳を開き、以下のコードをコピーして保存します。
保存するときには、次の2点に注意してください。
- ファイルの種類は「テキスト文書」ではなく、「すべてのファイル」にしてください。
- ファイル名は「webp_batch_converter.bat」のように、拡張子を .bat にしてください。
Copy Code
@rem WebP Batch Converter
@rem This file enables batch conversion of images to and from WebP.
@rem Converted images are saved in another directory to avoid mixing.
@rem This file depends on libwebp released by Google.
@echo off
@rem args: file_img0, file_img1, ...
:main
setlocal
set path_libwebp="%~dp0"
for %%i in (%*) do (
call :converter %path_libwebp% %%i
)
endlocal
exit /b
@rem args: path_libwebp, file_img
:converter
if %~x2==.png (
call :png %1 %2
) else if %~x2==.PNG (
call :png %1 %2
) else if %~x2==.jpg (
call :jpg %1 %2
) else if %~x2==.JPG (
call :jpg %1 %2
) else if %~x2==.gif (
call :gif %1 %2
) else if %~x2==.GIF (
call :gif %1 %2
) else if %~x2==.webp (
call :webp %1 %2
) else if %~x2==.WEBP (
call :webp %1 %2
)
exit /b
@rem args: path_libwebp, file_img
:png
call :encodeWebp %1 %2 webp "-lossless"
exit /b
:jpg
call :encodeWebp %1 %2 webp ""
exit /b
:gif
call :encodeWebp %1 %2 webp ""
exit /b
:webp
call :decodeWebp %1 %2 png ""
exit /b
@rem args: path_libwebp, file_img, format, option
:encodeWebp
call :savedInAnotherDir "%~1bin\cwebp" %2 %3 %4
exit /b
:decodeWebp
call :savedInAnotherDir "%~1bin\dwebp" %2 %3 %4
exit /b
@rem args: file_libwebp, file_img, format, option
:savedInAnotherDir
setlocal
set child_dir=%~dp2
set another_dir="%child_dir:~0,-1%_%3"
mkdir %another_dir%
set converted_file="%child_dir:~0,-1%_%3\%~n2.%3"
%1 %~4 %2 -o %converted_file%
endlocal
exit /b
ファイルの保存場所
保存したバッチファイルは、先程解凍した libwebp のフォルダの中に入れてください。
(Readme.txt などが入っている場所です)
フォルダの構造を示しておくと、以下のようになっているはずです。
フォルダ構造
└─libwebp-1.2.0-windows-x64
│ Readme-mux.txt
│ Readme.txt
│ test.webp
│ test_ref.ppm
│ webp_batch_converter.bat
│
├─bin
│
├─include
│
└─lib
バッチファイルを指定の場所に置きたくない人は、コード内14行目の set path_libwebp=
以降を適宜変更してください。
webp_batch_converter.batの使い方
変換したい画像をすべて選択してバッチファイルにドラッグ&ドロップすると、選択された画像がすべて自動的に変換されます。
元の画像と変換後の画像が混ざらないよう、変換後の画像は別フォルダに保存されます。
変換された webp 画像は「元フォルダ名_webp」フォルダ以下に保存され、png 画像は「元フォルダ名_png」フォルダ以下に保存されます。
例えば、以下のように「sample」フォルダの中に画像が保存されていたとします。
フォルダ構造(変換前)
├─sample
│ image0.png
│ image1.jpg
│ image2.gif
│ image3.webp
これら4つの画像をすべてドラッグ&ドロップすると、以下のように保存されます。
フォルダ構造(変換後)
├─sample
│ image0.png
│ image1.jpg
│ image2.gif
│ image3.webp
│
├─sample_png
│ image3.png
│
├─sample_webp
│ image0.webp
│ image1.webp
│ image2.webp
webp 以外は webp に、webp は png に変換されます。
ショートカットの作成
ここまでの手順では、バッチファイルを使うたびに libwebp のフォルダを開く必要があります。
これが面倒な場合は、ショートカットを利用することで解決できます。
作成手順と使い方は非常にシンプルです。
バッチファイルを右クリックし、「ショートカットの作成」を選択します。
生成されたショートカットは元のファイルと同じ動作をするので、ショートカットに画像をドラッグ&ドロップしても一括変換されます。
ショートカットの便利な点を以下にまとめました。
- 元ファイルと同じ動作をする
- ファイル名を自由に変更できる
- 置く場所を自由に決められる
- いくつでも作成できる
操作しやすい場所にショートカットを作成しておくと作業効率が上がると思うので、是非活用してみてください。
まとめ
Google 公式の変換ツールを使って、WebP に一括変換する方法を紹介しました。
何か要望等があれば、コメントにてお知らせください。
コメント
Wow, superb blog format! How lengthy have you ever been running a blog for? you make running a blog look easy. The overall look of your website is fantastic, let alone the content!
I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again!
Undeniably believe that which you said. Yoour favorite justification appeared tto bbe on the internet the easiest thing to bee awaree of.
I saay to you, I certainly get irked wgile peopoe think agout orries
thhat thesy plainl ddo not knjow about. You managed to hit the nail upon the top as well ass defined
oout tthe whole thing witghout having side effect , people can take a signal.
Will liksly be back to gett more. Thanks
Some really great information, Sword lily I observed this. “Underpromise overdeliver.” by Tom Peters.
I know this if off topic butt I’m loolking into starting my own weblog and was ccurious what aall iss requird to get setup?
I’m assumibg havikng a blog like yours would cost a pretty penny?
I’m not ver webb sasvvy sso I’m nnot 100% certain. Any recommendations or advice would be grsatly appreciated.
Cheers
Fantastic beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog website? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept
I like the valuable info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I’m quite certain I’ll learn lots of new stuff right here! Good luck for the next!
Your style is very uniqye compared tto othe folks I’ve red stuff from.
I appreciate you for posting when you have tthe opportunity, Guess I’ll just book mark thyis web site.
Keep tyis going please, great job!
Hello! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!