Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Gonzalez Duque committed Jul 25, 2020
0 parents commit 0736849
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions process.py
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()
4 changes: 4 additions & 0 deletions requirements.txt
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
Binary file added salento.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added salento_voronoi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0736849

Please sign in to comment.