Skip to content

Commit

Permalink
Show colors
Browse files Browse the repository at this point in the history
  • Loading branch information
woctezuma committed Dec 27, 2023
1 parent 949cd74 commit daa5210
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion extract_steam_palette.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,58 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import mediapy as media\n",
"\n",
"from PIL import ImageColor, Image, ImageDraw\n",
"\n",
"# Reference: https://stackoverflow.com/questions/54165439/what-are-the-exact-color-names-available-in-pils-imagedraw\n",
"\n",
"def show_colors(c):\n",
" n = len( c )\n",
"\n",
" cols = NUM_COLORS\n",
" rows = ((n-1) // cols) +1\n",
" cellHeight = 30\n",
" cellWidth = 170\n",
" imgHeight = cellHeight * rows\n",
" imgWidth = cellWidth * cols\n",
"\n",
" i = Image.new( \"RGB\", (imgWidth,imgHeight), (0,0,0) )\n",
" a = ImageDraw.Draw( i )\n",
"\n",
" for idx, rgb in enumerate( c ):\n",
" y0 = cellHeight * (idx // cols)\n",
" y1 = y0 + cellHeight\n",
" x0 = cellWidth * (idx % cols)\n",
" x1 = x0 + (cellWidth / 1)\n",
"\n",
" a.rectangle( [ x0, y0, x1, y1 ], fill=tuple(rgb), outline='black' )\n",
"\n",
" media.show_image(i)"
],
"metadata": {
"id": "A4wbefjykP5U"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"NUM_COLORS = 3\n",
"\n",
"path_or_url = \"https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/Free-Game-3-teaser_1920x1080-56b6434f5564766a6dc7a1d7fada8c18\"\n",
"dominant_colors = extract_colors(path_or_url, num_colors=NUM_COLORS)"
"dominant_colors = extract_colors(path_or_url, num_colors=NUM_COLORS)\n",
"print(dominant_colors)\n",
"show_colors(dominant_colors)\n",
"\n",
"path_or_url = get_image_url(1267910)\n",
"dominant_colors = extract_colors(path_or_url, num_colors=NUM_COLORS)\n",
"print(dominant_colors)\n",
"show_colors(dominant_colors)"
],
"metadata": {
"id": "Sc-f5KLU3WDH"
Expand Down

0 comments on commit daa5210

Please sign in to comment.