LoginSignup
9
6

More than 1 year has passed since last update.

PythonでHEICファイルをPNGファイルに変換

Last updated at Posted at 2021-01-05

iPhoneのライブフォトで撮影した写真はHEICファイルで保存される.
このファイルはJPEGやPNGに比べて対応しているソフトウェアが少なく扱いにくい.
そこでHEICファイルをPNGファイルに変換する方法をメモしておく.

Google Colaboratoryを使えばブラウザから実行できる.

環境

  • Ubuntu 20.04.1 LTS on WSL2
  • Python 3.8.5

ソースコード

変換したいファイルと同一の階層にconv.pyを配置して実行する.

conv.py
from PIL import Image
import pyheif

def conv(image_path):
    new_name = image_path.replace('heic', 'png')
    heif_file = pyheif.read(image_path)
    data = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
        )
    data.save(new_name, "PNG")

import glob
lst = glob.glob("*.heic")
for l in lst:
    conv(l)

利用パッケージ

  • pyheif
  • PIL

PILは以下のコマンドでインストールする.

pip install Pillow pyheif

pyheifはOSごとに適したコマンドでインストールする.

macOSの場合,以下でインストールする.

brew install libffi libheif
pip install git+https://github.com/david-poirier-csn/pyheif.git

Debian系のLinuxディストリビューションの場合,以下でインストールする.

apt install libffi libheif-dev libde265-dev
pip install git+https://github.com/david-poirier-csn/pyheif.git

RedHat系のLinuxディストリビューションの場合,以下でインストールする.

yum install libffi libheif-devel libde265-devel
pip install git+https://github.com/david-poirier-csn/pyheif.git

実行結果

$ python conv.py
$ ls
IMG_3488.heic*  IMG_3494.heic*  IMG_3497.heic*  IMG_3499.heic*  IMG_3503.heic*  IMG_3510.heic*  IMG_3514.heic*  a.py*  
IMG_3488.mov*   IMG_3494.mov*   IMG_3497.mov*   IMG_3499.mov*   IMG_3503.mov*   IMG_3510.mov*   IMG_3514.mov*   env/

参考URL

Pythonで指定ディレクトリのHEICファイルをJPEGへ変換する - Qiita

9
6
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
9
6