{"id":834,"date":"2020-05-13T16:37:43","date_gmt":"2020-05-13T16:37:43","guid":{"rendered":"http:\/\/jackofalltradesmasterofsome.com\/blog\/?p=834"},"modified":"2020-05-14T13:55:58","modified_gmt":"2020-05-14T13:55:58","slug":"robotic-python-automation-python-to-create-abstract-art-automatically","status":"publish","type":"post","link":"https:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/05\/13\/robotic-python-automation-python-to-create-abstract-art-automatically\/","title":{"rendered":"Robotic Python Automation &#8211; Python to Create Abstract Art Automatically"},"content":{"rendered":"\n<p>Robotic Python Automation &#8211; Python to Create Abstract Art Automatically<\/p>\n\n\n\n<p>Looking to spruce up your home office? Here is a quick fun project you can do in Python to Create Abstract Art Automatically to print out for your home! This tutorial uses the basics of working with images, files, math and basic loops to create images you can color in however you like to match the style of your home.<\/p>\n\n\n\n<p><em><strong>Check out my free Udemy Classes\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.udemy.com\/user\/vimal-vachhani-2\/\" target=\"_blank\">here\u00a0<\/a>to learn Python for free!<\/strong>\u00a0And be sure to check out my books for additional free educational material. Other materials can be found\u00a0<a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/08\/modern-data-architecture-using-microsoft-azure-online-class-and-free-ebook\/\">here<\/a>. <\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new Jupyter Notebook in the following location \u201cC:\\Users\\yourusername\\Documents\\Python\\Training\\\u201d<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"166\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-10.png\" alt=\"\" class=\"wp-image-835\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-10.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-10-300x80.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new folder on your C Drive \u201cC:\\Users\\yourusername\\Documents\\Python\\Training\\art\u201d and create a empty file called \u201ctemplate.png\u201d with a transparent background. <\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"315\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-11.png\" alt=\"\" class=\"wp-image-836\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-11.png 388w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-11-300x244.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"466\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-12.png\" alt=\"\" class=\"wp-image-837\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-12.png 433w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-12-279x300.png 279w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Install pillow so we can work with images in Python <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pillow<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>We than use numpy and matplot lib to generate a\nrandom shape with the following code. Change some of the constants below to\nexperiment with generating different shapes. The loop (i) can also be changes\nto generate more or less shapes. This will create 25 shape in Python which you\ncan see in Juptyer and also save them to the art folder.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from matplotlib.path import Path\nimport matplotlib.patches as patches\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\ni=1\n\nwhile i&lt;20: \n    # UPDATE THESE VARIABLES TO CHANGE THE TYPES OF SHAPES\n    n = 4 # Number of possibly sharp edges\n    r = 0.7 # magnitude of the perturbation from the unit circle, \n    # should be between 0 and 1\n    N = n*3+1 # number of points in the Path\n    # There is the initial point and 3 points per cubic bezier curve. Thus, the curve will only pass though n points, which will be the sharp edges, the other 2 modify the shape of the bezier curve\n\n    angles = np.linspace(0,2*np.pi,N)\n    codes = np.full(N,Path.CURVE4)\n    codes[0] = Path.MOVETO\n\n    verts = np.stack((np.cos(angles),np.sin(angles))).T*(2*r*np.random.random(N)+1-r)[:,None]\n    verts[-1,:] = verts[0,:] # Using this instad of Path.CLOSEPOLY avoids an innecessary straight line\n    path = Path(verts, codes)\n\n    fig = plt.figure()\n    ax = fig.add_subplot(111)\n    \n    patch = patches.PathPatch(path, facecolor='none', lw=0.5)\n    ax.add_patch(patch)\n\n    ax.set_xlim(np.min(verts)*1.1, np.max(verts)*1.1)\n    ax.set_ylim(np.min(verts)*1.1, np.max(verts)*1.1)\n    ax.axis('off') # removes the axis to leave only the shape\n    \n    plt.savefig('art\/foo'+str(i)+'.png', transparent=True, dpi=300)\n    \n    i=i+1\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"375\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-13.png\" alt=\"\" class=\"wp-image-838\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-13.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-13-300x180.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"262\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-14.png\" alt=\"\" class=\"wp-image-839\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-14.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-14-300x126.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>In our last step all we need to do now is loop through each of the files we created and pasting them into the template image. We use the random function to paste them into random spots on the new image and then save the file as \u201cworkofart.png\u201d. This create a cool new pattern on a transparent background!<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfrom PIL import Image, ImageDraw\nimport numpy as np\nimport os\nimport random\n\ni=1\n#os.remove(\"art\/workofart.png\")\nbackground = Image.open(\"art\/template.png\")\n\n\nwhile i&lt;20: \n    foreground = Image.open(\"art\/foo\"+str(i)+\".png\")\n    background.paste(foreground, (random.randint(-1000,1000), random.randint(-1000,1000)), foreground)\n    background.save(\"art\/workofart.png\")\n    i=i+1\n    \n\nbackground.close()\nforeground.close()  \n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"412\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-15.png\" alt=\"\" class=\"wp-image-840\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-15.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-15-300x198.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>The last step is have some fun. Use your favorite paint colors. Once completed, take over to your local print shop and put in a frame above your desk! Take a look at the artwork I created below. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"415\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-16.png\" alt=\"\" class=\"wp-image-841\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-16.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/05\/image-16-300x200.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p><em><strong>Check out my free Udemy Classes\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.udemy.com\/user\/vimal-vachhani-2\/\" target=\"_blank\">here\u00a0<\/a>to learn Python for free!<\/strong>\u00a0And be sure to check out my books for additional free educational material. Other materials can be found\u00a0<a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/08\/modern-data-architecture-using-microsoft-azure-online-class-and-free-ebook\/\">here<\/a>. <\/em> <\/p>\n\n\n\n<p>Robotic Python Automation &#8211; Python to Create Abstract Art Automatically <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Robotic Python Automation &#8211; Python to Create Abstract Art Automatically Looking to spruce up your home office? Here is a quick fun project you can do in Python to Create Abstract Art Automatically to print out for your home! This tutorial uses the basics of working with images, files, math and basic loops to create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,94],"tags":[8,98],"class_list":["post-834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-python","tag-automation","tag-python"],"_links":{"self":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/comments?post=834"}],"version-history":[{"count":4,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/834\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/834\/revisions\/845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media\/841"}],"wp:attachment":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media?parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/categories?post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/tags?post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}