<jupyter_start><jupyter_text>Let's fetch another colourful image<jupyter_code>API_URL = "https://api.wellcomecollection.org/catalogue/v2/images"
response = requests.get(API_URL, params={
    "query": "cow",
    "color": "FFEC03"
}).json()
image_url = IIIFImageClient.init_from_url(
    response["results"][4]["thumbnail"]["url"]
).size(width=400)
image_bytes = requests.get(image_url).content
image = Image.open(BytesIO(image_bytes))
image<jupyter_output><empty_output><jupyter_text>We can use `numpy` to turn the image into more numerical data<jupyter_code>import numpy as np
image_data = np.array(image)
pixels = image_data.reshape(-1, 3)
pixels
pixels.shape<jupyter_output><empty_output><jupyter_text>that's a lot of pixels! Instead of plotting all of them, we can just plot a sample to get a sense of the colour distribution in 3d space<jupyter_code>random_indexes = np.random.choice(pixels.shape[0], 1000)
pixels = pixels[random_indexes]<jupyter_output><empty_output><jupyter_text>Now we can use matplotlib to plot the data in 3D RBG space!<jupyter_code>%matplotlib inline
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(
    *pixels.T,    # unpack pixels into R, G, B
    c=pixels/256  # plot each point with its colour
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11495946941783346, 'fraction_numerical': 0.016212232866617538, 'mean_word_length': 6.380434782608695, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '296872', 'license': 'MIT', 'n_tokens_mistral': 497, 'n_tokens_neox': 486, 'n_words': 120, 'path': 'python/notebooks/04 - analyse image colours.ipynb', 'repo_name': 'wellcomecollection/mozfest', 'size': '1357'}