Created
April 16, 2020 21:19
-
-
Save andrashann/7e46d0b5ff88e3e8e64ed15033caa8d7 to your computer and use it in GitHub Desktop.
programmatic design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "pmyRmAkUj-EW" | |
}, | |
"outputs": [], | |
"source": [ | |
"import itertools\n", | |
"import random\n", | |
"\n", | |
"import base64\n", | |
"import glob\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "Y2RaVTjIj-EZ" | |
}, | |
"source": [ | |
"We are creating a bunch of SVG files out of a folder of pictures. We \n", | |
"randomize the locations and colors of two rectangles behind the picture\n", | |
"to generate up to 72 unique combinations." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "zPfkP37Uj-Eb" | |
}, | |
"outputs": [], | |
"source": [ | |
"SVG_HEAD = '''<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", | |
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", | |
"<svg width=\"350\" height=\"433\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", | |
"'''\n", | |
"SVG_FOOT = '</svg>'\n", | |
"\n", | |
"# location of source images to use:\n", | |
"IMG_DIR = 'images'\n", | |
"# don't have images to play with? get some from http://unsample.net/.\n", | |
"\n", | |
"# output folder for svg files:\n", | |
"SVG_DIR = 'svg'\n", | |
"\n", | |
"if not os.path.exists(SVG_DIR):\n", | |
" os.makedirs(SVG_DIR)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "kZ_1H7lUj-Ed" | |
}, | |
"outputs": [], | |
"source": [ | |
"# get list of images\n", | |
"file_list = sorted(glob.glob(\"{}/*.jpg\".format(IMG_DIR)) + glob.glob(\"{}/*.png\".format(IMG_DIR)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "Kzs5dBJfj-Eg" | |
}, | |
"outputs": [], | |
"source": [ | |
"# set up list of random values for svg\n", | |
"\n", | |
"x = [0, 50, 100] # potential x coordinates for the objects\n", | |
"y = [0, 50, 100] # potential y coordinates for the objects\n", | |
"c = [\"255,113,41\", \"90,30,0\"] # colors of the two rectangles that are not the image\n", | |
"\n", | |
"# a full list of combinations of the permutations of the parameters\n", | |
"perm = list(itertools.product(itertools.permutations(x), \n", | |
" itertools.permutations(y),\n", | |
" itertools.permutations(c)))\n", | |
"\n", | |
"# with a seed, get the actual list in a random order\n", | |
"random.seed(2030)\n", | |
"param_list = random.sample(perm, 72)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[((0, 100, 50), (100, 0, 50), ('255,113,41', '90,30,0')),\n", | |
" ((50, 0, 100), (0, 100, 50), ('255,113,41', '90,30,0')),\n", | |
" ((50, 0, 100), (100, 50, 0), ('90,30,0', '255,113,41')),\n", | |
" ((50, 0, 100), (100, 0, 50), ('255,113,41', '90,30,0')),\n", | |
" ((50, 0, 100), (50, 100, 0), ('90,30,0', '255,113,41'))]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"param_list[:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": {}, | |
"colab_type": "code", | |
"collapsed": true, | |
"id": "LYj4GHwmj-Ei", | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"for i in range(len(file_list)):\n", | |
" svgstr = SVG_HEAD\n", | |
" p = param_list[i]\n", | |
" \n", | |
" for rect in [0,1]:\n", | |
" svgstr += '''<rect width=\"262\" height=\"345\" x=\"{x}\" y=\"{y}\" style=\"fill:#fff;\" />\\n'''.format(\n", | |
" x=p[0][rect]-6, y=p[1][rect]-6)\n", | |
" svgstr += '''<rect width=\"250\" height=\"333\" x=\"{x}\" y=\"{y}\" style=\"fill:rgb({c});\" />\\n'''.format(\n", | |
" x=p[0][rect], y=p[1][rect], c=p[2][rect])\n", | |
" \n", | |
" with open(file_list[i], \"rb\") as image_file:\n", | |
" img = base64.b64encode(image_file.read())\n", | |
" svgstr += '''<rect width=\"262\" height=\"345\" x=\"{x}\" y=\"{y}\" style=\"fill:#fff;\" />\\n'''.format(\n", | |
" x=p[0][2]-6, y=p[1][2]-6)\n", | |
" svgstr += '''<image width=\"250\" height=\"333\" x=\"{x}\" y=\"{y}\" xlink:href=\"data:image/jpeg;base64,{img}\" \n", | |
" style=\"outline-color:white;outline-style:solid;outline-width:6px;\"/>\\n'''.format(\n", | |
" x=p[0][2], y=p[1][2], img=img)\n", | |
" \n", | |
" svgstr += SVG_FOOT\n", | |
" \n", | |
" with open(SVG_DIR + os.sep + os.path.splitext(os.path.basename(file_list[i]))[0]+'.svg', \"w\") as f: \n", | |
" f.write(svgstr) " | |
] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"name": "svg_generator.ipynb", | |
"provenance": [], | |
"version": "0.3.2" | |
}, | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment