Skip to content

Commit

Permalink
Added color palette
Browse files Browse the repository at this point in the history
  • Loading branch information
allengu01 committed Sep 27, 2020
1 parent 5ba41a5 commit 345b2c3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
Binary file modified examples/iterate_animation1.gif
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 examples/palette1.png
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 examples/palette2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/rotate_animation1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 16 additions & 5 deletions giffify.py → imageify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os, shutil
import imageio
import cv2

from PIL import Image

def split_rgb(image):
b, g, r = cv2.split(image)
Expand All @@ -20,18 +20,17 @@ def reset():
def main():
reset()

image_file = 'starrynight.jpg'
image_file = 'minimalist_landscape1.jpg'
img = cv2.imread('images/' + image_file) # change file here
resized_img = cv2.resize(img, (40, 40), interpolation=cv2.INTER_AREA)
#resized_img = cv2.resize(img, (60, 80))
img_r, img_g, img_b = split_rgb(resized_img)
flatten_img_r, flatten_img_g, flatten_img_b = list(map(flatten, [img_r, img_g, img_b]))
pixels = np.stack([flatten_img_r, flatten_img_g, flatten_img_b], axis=1)

# K-MEANS
k = 3
k = 4
print("Number of Pixels:", pixels.shape[0])
kmeans_filenames = run_kmeans(pixels, k)
kmeans_filenames, centroids = run_kmeans(pixels, k)

# ITERATION ANIMATION
iterate_animation_dir = 'figs/'
Expand All @@ -54,4 +53,16 @@ def main():
os.remove(file_path)
imageio.mimsave('figs/rotate_animation.gif', images, fps=10)

# COLOR PALETTE
color_width = 360 // k
image_array = np.empty([3, 360, 0])
for centroid in centroids:
color_block = np.ones([3, 360, color_width]) * centroid[:, np.newaxis, np.newaxis]
image_array = np.append(image_array, color_block, axis=2)
print(image_array)
print(image_array.shape)
palette = Image.fromarray(image_array.transpose(1, 2, 0).astype(np.uint8), 'RGB')
palette.save('figs/palette.png')


main()
2 changes: 1 addition & 1 deletion kmeans_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ def run_kmeans(data, k):
filenames['rotate'] = rotate_files
plt.close("all")

return filenames
return filenames, centroids_result

0 comments on commit 345b2c3

Please sign in to comment.