diff --git a/examples/iterate_animation1.gif b/examples/iterate_animation1.gif index 42f4653..2eb4381 100644 Binary files a/examples/iterate_animation1.gif and b/examples/iterate_animation1.gif differ diff --git a/examples/palette1.png b/examples/palette1.png new file mode 100644 index 0000000..74c936b Binary files /dev/null and b/examples/palette1.png differ diff --git a/examples/palette2.png b/examples/palette2.png new file mode 100644 index 0000000..14eda06 Binary files /dev/null and b/examples/palette2.png differ diff --git a/examples/rotate_animation1.gif b/examples/rotate_animation1.gif index bb5bf6b..35eb9c6 100644 Binary files a/examples/rotate_animation1.gif and b/examples/rotate_animation1.gif differ diff --git a/giffify.py b/imageify.py similarity index 72% rename from giffify.py rename to imageify.py index 2979f6a..024303b 100644 --- a/giffify.py +++ b/imageify.py @@ -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) @@ -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/' @@ -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() \ No newline at end of file diff --git a/kmeans_mpl.py b/kmeans_mpl.py index db24b61..a0b5a53 100644 --- a/kmeans_mpl.py +++ b/kmeans_mpl.py @@ -141,4 +141,4 @@ def run_kmeans(data, k): filenames['rotate'] = rotate_files plt.close("all") - return filenames \ No newline at end of file + return filenames, centroids_result \ No newline at end of file