0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WebPに一括返還

Posted at

概要

  • 特定のディレクトリにある画像をすべてWebPに変換
  • 変換はPillowが勝手にするらしく初期値ではロスあり、品質80%で変換するらしい
  • 画像の内容にもよるがFoooocusの出力画像ではPNG画像に比べ10分の1まで画像サイズが減少した
  • jpgでもいいかもしれない
  • たぶんPythonが入ってるだけでいい。(Python3.10で実行しました)
  • Foooocusでは出力画像を自動で保存しておりPNGがたくさんあって邪魔だったので一括変換しました。

コード

Geminiの出力

import os
from PIL import Image

def convert_png_to_webp(root_dir):
    """
    指定したディレクトリ以下のすべてのPNG画像をwebp形式に変換し、元のPNGファイルを削除します。

    Args:
        root_dir (str): 変換対象のルートディレクトリのパス。
    """
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.lower().endswith(".png"):
                png_filepath = os.path.join(dirpath, filename)
                webp_filepath = os.path.splitext(png_filepath)[0] + ".webp"
                    with Image.open(png_filepath) as img:
                        img.save(webp_filepath, "webp")
                    print(f"変換完了: {png_filepath} -> {webp_filepath}")
                except Exception as e:
                    print(f"変換失敗: {png_filepath} - {e}")

if __name__ == "__main__":
    target_directory = "."  # 変換したいフォルダのパスに置き換えてください
    convert_png_to_webp(target_directory)
    print("処理が完了しました。")

結果

プロンプト:I am Qiita!
PNG:
2025-04-22_13-52-48_1279.png

WebP:

対応していない形式のファイルです。
QiitaはWebPに対応してないらしい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?