LoginSignup
14
5

More than 5 years have passed since last update.

Chainer v4 PILからNumpy NumpyからPILの変換

Last updated at Posted at 2018-08-28

モジュールインポート

from PIL import Image
import numpy as np

PIL→Numpy


img = Image.open("aaa.png")
numpyArray = np.asarray(img).astype("f").transpose(2, 0, 1) / 128 - 1.0

Numpy→PIL

array = np.asarray(((numpyArray + 1) * 128).astype("i").transpose(1, 2, 0))
img = Image.fromarray(np.uint8(array))
img.show()

特にfromarrayでuint8にするのをよく忘れるので書き留めます。
誰かの役に立つことを祈ります。

まだ始めたばかりなので間違っていたらご指摘お願いします。

14
5
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
14
5