The usual prelims...
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Start with a CC-BY image from Flickr/Green Optics. This one is another option.
import requests
from PIL import Image
from io import BytesIO
url = "https://farm9.staticflickr.com/8076/8326425318_3721a23141_b_d.jpg"
r = requests.get(url)
im = Image.open(BytesIO(r.content))
plt.imshow(im)
plt.show()
from PIL import Image
im = Image.open(fname)
image_as_array = np.asarray(im)
i.shape
(586, 1024, 3)
red = i[...,0]
red
array([[ 38, 41, 12, ..., 165, 166, 167], [ 55, 67, 48, ..., 165, 166, 166], [ 84, 94, 113, ..., 165, 165, 166], ..., [188, 189, 205, ..., 176, 148, 165], [187, 188, 214, ..., 144, 139, 145], [167, 183, 224, ..., 157, 146, 134]], dtype=uint8)
plt.imshow(red)
<matplotlib.image.AxesImage at 0x10b4d1780>
def index(a, colours=8):
b = a.reshape(a.size)
b = float(colours) * b / np.amax(b)
bins = np.linspace(0, colours-1, colours)
c = np.digitize(b, bins)
return c.reshape(a.shape)
red_2bit = index(red, 4)
plt.imshow(red_2bit, cmap="Greys")
<matplotlib.image.AxesImage at 0x1141ed590>