2
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?

More than 1 year has passed since last update.

HEICからPNGへ変換すーる(Python)

Posted at

はじめに

がちもとさんアドベントカレンダー9日目の記事です。
iPhoneの画像をPCに取り込んだ時に、拡張子がHEICになっちゃってるので、PNGに変えていきます。

開発環境

  • Windows 11 PC
  • Python 3.11

導入

1.ライブラリのインストール
pip install pillow, pillow_heif

2.プログラムを作成

heic2png.py
import os
from PIL import Image
import pillow_heif

# 変換元のフォルダと変換後のフォルダを指定します
input_folder = "input/heic/"
output_folder = "output/png/"

# 変換元のフォルダ内のHEICファイルを取得します
heic_files = [f for f in os.listdir(input_folder) if f.endswith('.HEIC')]

for heic_file in heic_files:
    # HEICファイルのパスを作成
    heic_path = os.path.join(input_folder, heic_file)
    
    # PNGファイルのパスを作成
    png_file = os.path.splitext(heic_file)[0] + '.png'
    png_path = os.path.join(output_folder, png_file)
    
    # HEICファイルをPNGに変換
    heif_file = pillow_heif.read_heif(heic_path)
    image = Image.frombytes(
        heif_file.mode, 
        heif_file.size, 
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
    )
    image.save(png_path, 'PNG')
    
    print(f'{heic_file}{png_file} に変換しました。')

print('変換が完了しました。')

このスクリプトを実行すると、指定したフォルダ内のすべてのHEICファイルがPNG形式に変換され、指定した出力フォルダに保存されます。

お疲れさまでした。

2
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
2
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?