CoCalc Public Filesgalaxypic1.ipynbOpen with one click!
Author: Harald Schilly
Views : 97
Compute Environment: Ubuntu 18.04 (Deprecated)

read image pillow/pil and matplotlib into numpy array

Kernel: "Python 3 (Ubuntu Linux)"

In [1]:
!wget -q -N http://www.mindlaws.com/space/galaxypic1.jpg
In [2]:
!file galaxypic1.jpg
galaxypic1.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, comment: "Xat.com uJDO", baseline, precision 8, 350x350, frames 3
In [1]:
from IPython.display import Image Image('galaxypic1.jpg')
In [2]:
import PIL import numpy as np img = PIL.Image.open("galaxypic1.jpg").convert("L") galaxy = np.array(img) print(galaxy.shape) galaxy
(350, 350)
array([[1, 1, 1, ..., 3, 2, 2], [1, 1, 1, ..., 3, 3, 2], [1, 1, 1, ..., 3, 3, 3], ..., [2, 2, 2, ..., 1, 1, 1], [2, 2, 2, ..., 2, 2, 2], [2, 2, 2, ..., 2, 2, 2]], dtype=uint8)
In [3]:
import matplotlib.pyplot as plt galaxy2 = plt.imread("galaxypic1.jpg") galaxy2.shape
(350, 350, 3)
In [4]:
galaxy2[90:100, 90:100, :].sum(axis=2)
array([[32, 27, 27, 27, 29, 29, 23, 26, 29, 32], [32, 29, 27, 27, 29, 32, 26, 26, 30, 33], [31, 29, 29, 29, 31, 31, 27, 30, 33, 33], [35, 31, 31, 31, 33, 33, 27, 30, 33, 38], [35, 33, 31, 31, 33, 35, 27, 30, 33, 38], [35, 33, 31, 33, 33, 35, 30, 30, 35, 38], [38, 38, 41, 42, 42, 42, 21, 50, 33, 42], [38, 38, 42, 42, 42, 42, 53, 22, 23, 23], [38, 38, 42, 42, 42, 42, 51, 23, 90, 77], [38, 39, 42, 42, 42, 42, 62, 25, 62, 31]], dtype=uint64)
In [ ]: