Sagar Bharadwaj commited on
Commit
f490375
·
1 Parent(s): 17df544

Added simplify image

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ __pycache__/
2
+ .ipynb_checkpoints/
3
+ .DS_Store
ColorByNumber.ipynb ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "af221ec3-755c-42d5-8bb4-739644c482b6",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import cv2\n",
11
+ "\n",
12
+ "from colorbynumber.utils import show_image\n",
13
+ "from colorbynumber.simplify_image import simplify_image"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": null,
19
+ "id": "497a508a-87fc-49f7-baca-3505ff0b57f1",
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "# Image from: https://www.pexels.com/photo/scarlet-macaw-1618424/\n",
24
+ "# Photographer: Tim Mossholder\n",
25
+ "\n",
26
+ "image = cv2.imread(\"ExampleImages/Macaw.jpeg\")\n",
27
+ "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
28
+ "show_image(image)"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": null,
34
+ "id": "7368c3bd-a7a6-467b-be90-cf628d5b3b81",
35
+ "metadata": {},
36
+ "outputs": [],
37
+ "source": [
38
+ "color_list = [\n",
39
+ " (255, 0, 0), # Red\n",
40
+ " (0, 255, 0), # Green\n",
41
+ " (0, 0, 255), # Blue\n",
42
+ "]\n",
43
+ "simplified_image = simplify_image(image, color_list)\n",
44
+ "show_image(simplified_image)"
45
+ ]
46
+ }
47
+ ],
48
+ "metadata": {
49
+ "kernelspec": {
50
+ "display_name": "Python 3",
51
+ "language": "python",
52
+ "name": "python3"
53
+ },
54
+ "language_info": {
55
+ "codemirror_mode": {
56
+ "name": "ipython",
57
+ "version": 3
58
+ },
59
+ "file_extension": ".py",
60
+ "mimetype": "text/x-python",
61
+ "name": "python",
62
+ "nbconvert_exporter": "python",
63
+ "pygments_lexer": "ipython3",
64
+ "version": "3.9.18"
65
+ }
66
+ },
67
+ "nbformat": 4,
68
+ "nbformat_minor": 5
69
+ }
ExampleImages/Macaw.jpeg ADDED
colorbynumber/simplify_image.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def _closest_color(pixel, color_list):
4
+ """
5
+ Finds the closest color in the list to the given pixel (RGB values)
6
+
7
+ Args:
8
+ pixel: A tuple representing the RGB values of a pixel (R, G, B).
9
+ color_list: A list of tuples representing RGB values of allowed colors.
10
+
11
+ Returns:
12
+ A tuple representing the RGB values of the closest color in the list.
13
+ """
14
+ distances = np.array([np.linalg.norm(np.array(pixel) - np.array(color)) for color in color_list])
15
+ return color_list[np.argmin(distances)]
16
+
17
+ def simplify_image(image, color_list):
18
+ """
19
+ Converts all colors in an image to the closest color in the color list.
20
+
21
+ Args:
22
+ image: Image in the RGB color space as a 3D array.
23
+ color_list: A list of tuples representing RGB values of allowed colors.
24
+
25
+ Returns:
26
+ A copy of the image with all colors replaced with the closest color in the list.
27
+ """
28
+
29
+ # Replace each pixel with closest color from the list
30
+ converted_image = image.copy() # Operate on a copy
31
+ height, width, channels = image.shape
32
+ for y in range(height):
33
+ for x in range(width):
34
+ pixel = image[y, x]
35
+ closest = _closest_color(pixel, color_list)
36
+ converted_image[y, x] = closest
37
+ return converted_image
colorbynumber/utils.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from matplotlib import pyplot as plt
2
+
3
+ def show_image(image):
4
+ plt.imshow(image)
5
+ plt.axis('off')
6
+ plt.show()
environment.yml CHANGED
@@ -7,3 +7,4 @@ dependencies:
7
  - python=3.9
8
  - opencv
9
  - jupyterlab
 
 
7
  - python=3.9
8
  - opencv
9
  - jupyterlab
10
+ - matplotlib