スマートフォンやデジタルカメラで撮影した写真の中には、拡張子が.heic
となっているものがあります。
HEIC(High Efficiency Image File Format)は高圧縮・高画質が特徴ですが、Windowsや一部のアプリでは標準対応していないこともあります。
そこで今回は、Pythonを使ってHEIC画像をJPEGやPNGなどに変換するスクリプトを紹介します。
1. 環境構築
Python環境がインストールされている前提で進めます(macOSやLinuxの場合は標準搭載されていることが多いです)。
まず、必要なライブラリをインストールします。
pip install pillow pillow-heif
- Pillow:Pythonの画像処理ライブラリ
- pillow-heif:HEIC/HEIF形式をPillowで開けるようにするプラグイン
インストール後、これらを使ってHEIC画像を任意の形式(JPEG, PNG, WEBPなど)に変換できるようになります。
2. 単一ファイル変換スクリプト
ファイル名:convert_heic_single.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
単一の HEIC 画像を指定形式(JPEG, PNG など)に変換するスクリプト
"""
from PIL import Image
import pillow_heif
import sys
import os
def convert_heic(input_path, output_path, output_format="JPEG"):
"""
HEIC画像を指定形式に変換する関数
:param input_path: 入力するHEICファイルのパス
:param output_path: 出力先ファイルのパス
:param output_format: 出力形式("JPEG", "PNG" など)
"""
# HEIC形式を開けるように登録
pillow_heif.register_heif_opener()
# HEIC画像を開く
img = Image.open(input_path)
# JPEGは透過非対応のためRGB変換
if output_format.upper() == "JPEG":
img = img.convert("RGB")
# 変換して保存
img.save(output_path, format=output_format)
print(f"変換完了: {output_path}")
if __name__ == "__main__":
# コマンドライン引数のチェック
if len(sys.argv) < 3:
print("使い方: python convert_heic_single.py 入力.heic 出力.jpg [形式]")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
fmt = sys.argv[3] if len(sys.argv) >= 4 else "JPEG"
if not os.path.exists(input_file):
print(f"ファイルが存在しません: {input_file}")
sys.exit(1)
convert_heic(input_file, output_file, fmt)
実行例
python convert_heic_single.py sample.heic sample.jpg
形式を指定する場合(例:PNG)
python convert_heic_single.py sample.heic sample.png PNG
3. ディレクトリ内一括変換スクリプト
ファイル名:convert_heic_batch.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ディレクトリ内の HEIC 画像を指定形式に一括変換するスクリプト
"""
from PIL import Image
import pillow_heif
import os
import glob
import sys
def convert_heic(input_path, output_path, output_format="JPEG"):
"""
HEIC画像を指定形式に変換
"""
pillow_heif.register_heif_opener()
img = Image.open(input_path)
if output_format.upper() == "JPEG":
img = img.convert("RGB")
img.save(output_path, format=output_format)
print(f"変換完了: {output_path}")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("使い方: python convert_heic_batch.py 入力ディレクトリ 出力ディレクトリ [形式]")
sys.exit(1)
input_dir = sys.argv[1]
output_dir = sys.argv[2]
fmt = sys.argv[3] if len(sys.argv) >= 4 else "JPEG"
if not os.path.isdir(input_dir):
print(f"ディレクトリが存在しません: {input_dir}")
sys.exit(1)
os.makedirs(output_dir, exist_ok=True)
pillow_heif.register_heif_opener()
for heic_file in glob.glob(os.path.join(input_dir, "*.heic")):
base = os.path.splitext(os.path.basename(heic_file))[0]
output_path = os.path.join(output_dir, f"{base}.{fmt.lower()}")
convert_heic(heic_file, output_path, fmt)
実行例
python convert_heic_batch.py ./heic_images ./converted_images
形式を指定する場合(例:PNG)
python convert_heic_batch.py ./heic_images ./converted_images PNG
4. オプションと注意点
単一版
入力ファイルパス
:変換元のHEICファイル出力ファイルパス
:保存先のファイル[形式]
:省略時はJPEG。PNG
,WEBP
なども可
一括版
入力ディレクトリ
:変換元のHEIC画像があるディレクトリ出力ディレクトリ
:変換後の画像を保存するディレクトリ[形式]
:省略時はJPEG
5. まとめ
今回のスクリプトを使えば、HEIC画像を簡単にJPEGやPNGに変換できます。
単一版はちょっとした変換に、一括版は大量変換に適しています。
透過が必要な場合はPNG形式を選び、通常の写真はJPEGで十分です。
コメント