This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib_util.py
116 lines (82 loc) · 3.52 KB
/
lib_util.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from PIL import Image
import numpy as np
from numpy import sin, cos, arcsin, radians, \
degrees
def RGBToString(rgb_tuple):
"""
Convert a color to a css readable string
"""
color = 'rgb(%s,%s,%s)' % rgb_tuple
return color
def closest(x, y, pixels, offset, best_colours):
"""
Recursively finds the nearest colour in an image
from a set of colours given a pixel.
:param x: The x coordinate of the pixel.
:param y: The y coordinate of the pixel
:param offset: The offset to use to the search space.
:param best_colours: List/Tuple of allowable colours
:returns: the nearest colour from the best colours set
"""
if pixels[x, y] in best_colours:
return pixels[x, y]
x_low = np.amax((0, x - offset))
x_high = np.amin((x + offset, pixels.shape[0] - 1))
y_low = np.amax((0, y - offset))
y_high = np.amin((y + offset, pixels.shape[1] - 1))
x_index = np.concatenate((np.ones(y_high - y_low) * x_low,
np.arange(x_low, x_high),
np.ones(y_high - y_low) * x_high,
np.arange(x_low, x_high)))
y_index = np.concatenate((np.arange(y_low, y_high, dtype='int'),
np.ones(x_high - x_low, dtype='int') * y_high,
np.arange(y_low, y_high, dtype='int'),
np.ones(x_high - x_low, dtype='int') * y_low))
data = pixels[x_index.astype('int'),
y_index.astype('int')].flatten()
counts = np.empty_like(best_colours)
for i, col in enumerate(best_colours):
counts[i] = (data == col).sum()
if (counts.sum() == 0):
return closest(x, y, pixels, offset + 1, best_colours)
return best_colours[np.argmax(counts)]
def posterize(image):
"""
Reduces the number of colours in an image the only the most
prevalent colours (colours that cover more than 1% of the image).
This function addresses interpolation artefacts that causes
problems in PIL
:param image: A PIL image object
"""
# make a greyscaled version for histograms
g_im = image.convert('P', palette=Image.ADAPTIVE)
# Get as a numpy array
pixels = np.array(g_im)
count, colours = np.histogram(pixels,
pixels.max() - pixels.min() + 1)
colours = np.array(colours, dtype=int)
colours = colours[1:]
# Take only colors that make up 1% of the image
best_colours = colours[count > (.01 * pixels.size)]
# Just use PIL if there aren't a subset of prevalent colours
if ((best_colours.size < 2) or (best_colours.size > 15)):
return g_im.convert('P',
palette=Image.ADAPTIVE,
colors=8)
# Find pixels that aren't one of the best colours
fix_index = np.zeros(pixels.shape, dtype=bool)
for colour in best_colours:
fix_mask = np.logical_or(pixels == colour, fix_index)
fix_index = np.array((np.where(~fix_mask)))
# Loop through each pixel that needs to be adjusted and find
# the nearest best colour
for x, y in zip(fix_index[0], fix_index[1]):
point = closest(x, y, pixels, 1, best_colours)
pixels[x, y] = point
# Remap the image with the new pixel values
g_im.paste(Image.fromarray(pixels))
n_colours = best_colours.size
# Posterize the image so PIL will limit the colour map size
return g_im.convert('P',
palette=Image.ADAPTIVE,
colors=n_colours)