LoginSignup
8
7

More than 5 years have passed since last update.

PythonのPILを使って複数の画像を1枚のTiff画像(multi image tiff)に変換する方法

Last updated at Posted at 2018-07-20

経緯

200枚程度の画像を1つのTiff Stack画像として保存したかったが、pythonを使って行う方法がなかなか見つけられなかった。

有名な画像処理ライブラリのPillowで簡単に出来たので、やり方を共有しておきます。

Pillow
https://pillow.readthedocs.io/en/latest/

環境、使用するライブラリ

$ python -V
Python 3.5.2

$ pip list | grep Pillow
Pillow   5.1.0

インストールしてない方は下記のコード実行してください。

$ pip install Pillow

実際のコード


def saveTiffStack(save_path, imgs):
    """
    :param save_path: 画像を保存するパス
    :param imgs: 保存したい画像のnumpy.array  Ex)10枚の32x32のRGBに画像をtiffstackにしたい場合、 imgs.shape => (10, 32, 32, 3)
    :return:
    """
    stack = []
    for img in imgs:
        stack.append(Image.fromarray(img))
    stack[0].save(save_path, compression="tiff_deflate", save_all=True, append_images=stack[1:])

圧縮方法としてcompression="tiff_deflate"を指定してますが可逆圧縮なので、jpegのように解像度が低くなることはありません。

まとめ

参考になれば幸いです。

8
7
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
8
7