-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Miguel Gonzalez Duque
committed
Jul 25, 2020
0 parents
commit 0736849
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import click | ||
import numpy as np | ||
from PIL import Image | ||
from scipy.spatial import KDTree, Voronoi | ||
from itertools import product | ||
|
||
def compute_mean_color(img, region): | ||
""" | ||
This function takes an image array and | ||
one of those tuples that np.where outputs. | ||
""" | ||
region_slice = img[region] | ||
return np.mean(region_slice, axis=0) | ||
|
||
@click.command() | ||
@click.argument("img_path", type=str) | ||
@click.option("--out", type=str, default="out.jpg") | ||
@click.option("--n_points", type=int, default=300) | ||
def process(img_path, out, n_points): | ||
# Loading the image | ||
img = Image.open(img_path) | ||
img = np.asarray(img) | ||
|
||
random_i = np.random.randint(0, img.shape[1], size=n_points) | ||
random_j = np.random.randint(0, img.shape[0], size=n_points) | ||
|
||
points = np.vstack((random_i, random_j)).T | ||
|
||
# Creating the kdtree | ||
vor = Voronoi(points) | ||
kdtree = KDTree(vor.points) | ||
|
||
# Identifying regions pixel by pixel | ||
region_array = - np.ones(img.shape[:2]) | ||
all_pos = product(range(img.shape[1]), range(img.shape[0])) | ||
positions = np.array(list(all_pos)) | ||
distances, labels = kdtree.query(positions) | ||
|
||
for pos, label in zip(positions, labels): | ||
region_array[pos[1], pos[0]]= label | ||
|
||
# Storing colors | ||
colors = {} | ||
for region_id in np.unique(region_array): | ||
colors[region_id] = compute_mean_color(img, np.where(region_array == region_id)) | ||
|
||
new_img = np.zeros(img.shape) | ||
for region_id in np.unique(region_array): | ||
region = np.where(region_array == region_id) | ||
new_img[region] = colors[region_id] | ||
|
||
new_img = new_img.astype(int) | ||
|
||
# Saving it | ||
PIL_image = Image.fromarray(new_img.astype('uint8'), 'RGB') | ||
PIL_image.save(out) | ||
|
||
if __name__ == "__main__": | ||
process() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
click==7.1.2 | ||
numpy==1.19.1 | ||
Pillow==7.2.0 | ||
scipy==1.5.1 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.