PILイメージとnumpy配列の間で変換する



Convert Between Pil Image



または、画像からnumpy配列を取得するには、次のコマンドを使用します。

from PIL import Image from numpy import array img = Image.open('input.png') arr = array(img)

そして、numpy配列から画像を取得するには、次を使用します。



img = Image.fromarray(arr) img.save('output.png')

サンプル:

from PIL import Image import numpy im = Image.open('sample2.png') np_im = numpy.array(im) print np_im.shape OUTPUT (200, 400, 3) np_im = np_im - 18 new_im = Image.fromarray(np_im) new_im.save('numpy_altered_sample2.png')

im = Image.open( 'sample2.png')