{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os \n", "from glob import glob \n", "import pandas as pd\n", "import numpy as np\n", "\n", "from PIL import Image, ImageColor\n", "import extcolors\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "import torch\n", "\n", "import dnnlib \n", "import legacy\n", "\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images_textiles = glob('/Users/ludovicaschaerf/Desktop/TextAIles/TextileGAN/Original Textiles/*')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### LAWS\n", "\n", "1. primary colours on small surfaces and secondary or tertiary colors on large backgrounds\n", "2. primary in upper portions and sec/third in lower portions of objects\n", "3. primaries of equal intensities harmonize, secondaries harmonized by opposite primary in equal intensity, tertiary by remaining secondary\n", "4. a full colors contrasted by a lower tone color should have the latter in larger proportion\n", "5. when a primary has a hue (second coloration) of another primary, the secondary must have the hue of the third primary\n", "6. blue in concave surfaces, yellow in convex, red in undersites\n", "7. if too much of a color, the other colors should have the hue version without that color\n", "8. all three primaries should be present\n", "9. ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test 1\n", "\n", "primary - secondary - tertiary " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_color_rank(hue, saturation, value):\n", " if value < 5:\n", " color = 'Black'\n", " rank = 'None'\n", " elif saturation < 3:\n", " color = 'White'\n", " rank = 'None'\n", " elif saturation < 15:\n", " color = 'Gray'\n", " rank = 'None'\n", " elif hue == 0:\n", " color = 'Gray'\n", " rank = 'None'\n", " \n", " elif hue >= 330 or hue <= 15:\n", " color = 'Red'\n", " rank = 'Primary'\n", " elif hue > 15 and hue < 25:\n", " color = 'Red Orange'\n", " rank = 'Tertiary'\n", " elif hue >= 25 and hue <= 40:\n", " color = 'Orange'\n", " rank = 'Secondary'\n", " elif hue > 40 and hue < 50:\n", " color = 'Orange Yellow'\n", " rank = 'Tertiary'\n", " elif hue >= 50 and hue <= 85:\n", " color = 'Yellow'\n", " rank = 'Primary'\n", " elif hue > 85 and hue < 95:\n", " color = 'Yellow Green'\n", " rank = 'Tertiary'\n", " elif hue >= 95 and hue <= 145:\n", " color = 'Green'\n", " rank = 'Secondary'\n", " elif hue >= 145 and hue < 180:\n", " color = 'Green Blue'\n", " rank = 'Tertiary'\n", " elif hue >= 180 and hue <= 245:\n", " color = 'Blue'\n", " rank = 'Primary'\n", " elif hue > 245 and hue < 265:\n", " color = 'Blue Violet'\n", " rank = 'Tertiary'\n", " elif hue >= 265 and hue <= 290:\n", " color = 'Violet'\n", " rank = 'Secondary'\n", " elif hue > 290 and hue < 330:\n", " color = 'Violet Red'\n", " rank = 'Tertiary'\n", " return color, rank" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def rgb2hsv(r, g, b):\n", " # Normalize R, G, B values\n", " r, g, b = r / 255.0, g / 255.0, b / 255.0\n", " \n", " # h, s, v = hue, saturation, value\n", " max_rgb = max(r, g, b) \n", " min_rgb = min(r, g, b) \n", " difference = max_rgb-min_rgb \n", " \n", " # if max_rgb and max_rgb are equal then h = 0\n", " if max_rgb == min_rgb:\n", " h = 0\n", " \n", " # if max_rgb==r then h is computed as follows\n", " elif max_rgb == r:\n", " h = (60 * ((g - b) / difference) + 360) % 360\n", " \n", " # if max_rgb==g then compute h as follows\n", " elif max_rgb == g:\n", " h = (60 * ((b - r) / difference) + 120) % 360\n", " \n", " # if max_rgb=b then compute h\n", " elif max_rgb == b:\n", " h = (60 * ((r - g) / difference) + 240) % 360\n", " \n", " # if max_rgb==zero then s=0\n", " if max_rgb == 0:\n", " s = 0\n", " else:\n", " s = (difference / max_rgb) * 100\n", " \n", " # compute v\n", " v = max_rgb * 100\n", " # return rounded values of H, S and V\n", " return tuple(map(round, (h, s, v)))\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def obtain_hsv_colors(img):\n", " colors = extcolors.extract_from_path(img, tolerance=7, limit=7)\n", " colors = [(rgb2hsv(h[0][0], h[0][1], h[0][2]), h[1]) for h in colors[0] if h[0] != (0,0,0)]\n", " return colors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors = obtain_hsv_colors(images_textiles[0])\n", "print(colors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for col in colors:\n", " print(get_color_rank(*col[0]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for img in images_textiles[:30]:\n", " colors = obtain_hsv_colors(img)\n", " plt.imshow(plt.imread(img))\n", " plt.show()\n", " for col in colors:\n", " print(col[0])\n", " print(get_color_rank(*col[0]))\n", " \n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### use for training only images with medium saturation and value\n", "\n", "use codes and not only hue for color categorization\n", "or remove colors that are creater with black and whites" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "art-reco_x86", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }