File size: 1,782 Bytes
7f9536b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97d9f54
 
7f9536b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cv2 as cv
import numpy as np
import requests
import urllib
from pathlib import Path

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans


def get_iiif_image_urls(ie_pid: str):

    manifest_url = f"https://rosetta.slv.vic.gov.au/delivery/iiif/presentation/2.1/{ie_pid}/manifest"
    print(manifest_url)
    session = requests.Session()

    response = session.get(manifest_url)

    manifest = response.json()

    image_ids = [
        canvas["images"][0]["resource"]["service"]["@id"]
        for canvas in manifest["sequences"][0]["canvases"]
    ]

    image_urls = [f"{image_id}/full/600,/0/default.jpg" for image_id in image_ids]

    return image_urls


def show_img_compare(img_1, img_2):
    f, ax = plt.subplots(1, 2, figsize=(10, 10))
    ax[0].imshow(img_1)
    ax[1].imshow(img_2)
    ax[0].axis("off")  # hide the axis
    ax[1].axis("off")
    f.tight_layout()
    plt.show()


def palette(clusters):
    width = 300
    palette = np.zeros((50, width, 3), np.uint8)
    steps = width / clusters.cluster_centers_.shape[0]
    for idx, centers in enumerate(clusters.cluster_centers_):
        palette[:, int(idx * steps) : (int((idx + 1) * steps)), :] = centers
    return palette


def get_palette_clusters(img):

    cluster = KMeans(n_clusters=5)
    cluster.fit(img.reshape(-1, 3))

    clusters = cluster.fit(img.reshape(-1, 3))

    return clusters


image_urls = get_iiif_image_urls("IE1267294")

response = requests.get(image_urls[0])

img = cv.imdecode(np.frombuffer(response.content, np.uint8), -1)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

dim = (500, 300)
img = cv.resize(img, dim, interpolation=cv.INTER_AREA)

clt_1 = get_palette_clusters(img)


img_palette = palette(clt_1)

print(img_palette)

show_img_compare(img, img_palette)