How to create procedural art in less than 100 lines of code

 3r33312. 3r3-31. How to create procedural art in less than 100 lines of code
3r33300.  3r33312.

What is generative art? 3r33232. 3r33300.  3r33312. This is the result of a system that makes its own decisions about the subject instead of the person. A system can be as simple as a single Python program, if it has rules and a moment of randomness. 3r33300.  3r33312. 3r33300.  3r33312. With programming, it's pretty easy to come up with rules and restrictions. For this there are conditional operators. But finding ways to make these rules create something interesting may not be so easy. 3r33300.  3r33312. 3r33300.  3r33312. Conway’s Game of Life [/i] 3r33300.  3r33312. 3r33300.  3r33312. 3r3334. The game "Life"
(Conway’s Game of Life) is a well-known set of four simple rules defining the “birth” and “death” of each cell in the system. Each rule plays a role in advancing the system through each generation. Although the rules are simple and easy to understand, complex patterns quickly emerge that ultimately form exciting results. 3r33300.  3r33312. 3r33300.  3r33312. The rules may be responsible for creating the basics of something interesting, but even something as exciting as Conway’s Game of Life is predictable. Four rules are the determining factors for each generation. Therefore, in order to obtain unexpected results, you must enter a randomization in the initial state of the cells. Starting with a random matrix, each execution will be unique without the need to change the rules. 3r33300.  3r33312. 3r33300.  3r33312. The best examples of generative art are those that find a combination of predictability and chance to create something interesting that is statistically impossible to repeat. 3r33300.  3r33312. 3r33300.  3r33312.
Why should you try this? 3r33232. 3r33300.  3r33312. Generative art will not always be what you want to spend time on. But if you decide to work on it, then you can count on the following benefits: 3r-3300.  3r33312. 3r33300.  3r33312.
 3r33312.
Experience. Generative art is another opportunity to hone new and old skills. It can serve as an input for working out such concepts as algorithms, data structures, and even new languages. 3r3365.  3r33312.
Tangible results. In programming, we rarely see the physical results of our efforts. Well, or at least I don't see it. Right now in my living room there are several posters with prints of my procedural art. And I love that this is done by code. 3r3365.  3r33312.
Attractive designs. Everyone had the experience of explaining a personal project to someone, perhaps even during an interview. Generative art speaks for itself. Most people will appreciate the results, even if they cannot fully understand the methods. 3r3365.  3r33312.
3r33300.  3r33312.
Where to begin? 3r33232. 3r33300.  3r33312. Getting started with generative art is the same process as starting work with any other project. The most important step is to come up with an idea or find it for further development. As soon as you have a goal, you can start working on achieving it. 3r33300.  3r33312. 3r33300.  3r33312. Most of my creative projects are done in Python. It is a fairly simple language with many useful packages that help in image processing. For example, Pillow . 3r33300.  3r33312. 3r33300.  3r33312. Fortunately, you don’t need to search for a long time where to start - I’ll share my code below. 3r33300.  3r33312. 3r33300.  3r33312.
Sprite Generator 3-333299. 3r33300.  3r33312. This project started when I saw a post with a sprite generator written in jаvascript. The program created 5 × 5 pixel-art sprites with random color choices, and its result resembled multi-colored space invaders. 3r33300.  3r33312. 3r33300.  3r33312. I wanted to practice image processing in Python, so I decided to recreate this concept on my own. In addition, I thought that I could expand it, since the original project was severely limited in size of sprites. And I want to specify not only the size of the sprites, but also their number and even the size of the image. 3r33300.  3r33312. 3r33300.  3r33312. Here are two different results of my program:
 3r33312. 3r33300.  3r33312. 3r3104. 3r33300.  3r33312. 7x7–30–1900 3r33300.  3r33312. 3r33300.  3r33312. 3r3113. 3r33300.  3r33312. 43x43–6–1900 3r3–3289. 3r33300.  3r33312. 3r33300.  3r33312. These two images are completely different from each other, but they are both the results of the same system. Not to mention the fact that, due to the complexity and random generation of sprites, there is a high probability that even with the same arguments, these images will forever remain unique. I love it. 3r33300.  3r33312. 3r33300.  3r33312.
Environment

3r33300.  3r33312. Before meeting with the generator of sprites should prepare a small foundation for the work. 3r33300.  3r33312. 3r33300.  3r33312. If you haven't worked with Python before, download Python ??? . At first I had problems with setting up the environment, if you come across them too - look at 3r3136. virtual environments
. And make sure Pillow also installed. 3r33300.  3r33312. 3r33300.  3r33312. After setting up the environment, you can copy my code to a file with a extension. .py and run the following command: 3r300.  3r33312. 3r33300.  3r33312. 3r33170. 3r3171. python spritething.py[SPRITE_DIMENSIONS] [NUMBER] [IMAGE_SIZE]
3r33300.  3r33312. For example, the command to create the first matrix of sprites would be: 3r300.  3r33312. 3r33300.  3r33312. 3r33170. 3r3171. python spritething.py ???
3r33300.  3r33312.

Code

3r33300.  3r33312. 3r33170. 3r3171. import PIL, random, sys
from PIL import Image, ImageDraw
origDimension = 1500
r = lambda: random.randint (???)
rc = lambda: (r (), r (), r ())
listSym =[]3r33312. def create_square (border, draw, randColor, element, size):
if (element == int (size /2)):
draw.rectangle (border, randColor)
elif (len (listSym) == element + 1):
draw.rectangle (border, listSym.pop ())
else:
listSym.append (randColor)
draw.rectangle (border, randColor)
def create_invader (border, draw, size):
x? y? x? y1 = border
squareSize = (x1-x0) /size
randColors =[rc(), rc(), rc(), (0,0,0), (0,0,0), (0,0,0)]3r33312. i = 1r3r3312. for y in range (? size):
I * = -1
element = 0
for x in range (? size):
topLeftX = x * squareSize + x0
topLeftY = y * squareSize + y0
botRightX = topLeftX + squareSize
botRightY = topLeftY + squareSize
create_square ((topLeftX, topLeftY, botRightX, botRightY), draw, random.choice (randColors), element, size)
if (element == int (size /2) or element == 0): 3r3333312. I * = -1; 3r33312. element + = I
def main (size, invaders, imgSize):
origDimension = imgSize
origImage = Image.new (‘RGB’, (origDimension, origDimension))
draw = ImageDraw.Draw (origImage)
invaderSize = origDimension /invaders
padding = invaderSize /size
for x in range (? invaders):
for y in range (? invaders):
topLeftX = x * invaderSize + padding /2
topLeftY = y * invaderSize + padding /2
botRightX = topLeftX + invaderSize - padding
botRightY = topLeftY + invaderSize - padding
create_invader ((topLeftX, topLeftY, botRightX, botRightY), draw, size)
origImage.save (“Examples /Example -“ + str (size) + "x" + str (size) + "-" + str (invaders) + "-" + str (imgSize) + ". jpg") 3r33312. if __name__ == "__main__":
main (int (sys.argv[1]), int (sys.argv[2]), int (sys.argv[3]))
3r33300.  3r33312. This solution is far from perfect, but it shows that the creation of generative art does not require a ton of code. I will explain the key points. 3r33300.  3r33312. 3r33300.  3r33312. The main function starts with creating the original image and determining the size of the sprites. Two cycles for they are responsible for determining the boundary of each sprite, basically dividing the image size by the number of requested sprites. These values ​​are used to determine the coordinates for each of them. 3r33300.  3r33312. 3r33300.  3r33312. Look at the image below. Imagine that each of the four squares is a sprite with a size of 1. The border, which is passed to the next function, refers to the coordinates of the upper left and lower right corners. So the tuple in the upper left sprite will be (???1), and the tuple in the upper right will be (???1). They will be used as dimensions and base coordinates for the squares of each sprite. 3r33300.  3r33312. 3r33300.  3r33312. An example of determining the boundaries of the sprite
3r33300.  3r33312. 3r33300.  3r33312. Function create_invader defines the border for each square inside the sprite. The same boundary determination process is applied here and is presented below, only instead of the full image we use a predefined boundary for working inside. These final coordinates for each square will be used in the next function to draw the sprite. 3r33300.  3r33312. 3r33300.  3r33312. An example of the breakdown of the sprite 3 × 3 [/i] 3r33300.  3r33312. 3r33300.  3r33312. To determine the color, a simple array of three random RGB tuples and three black ones is used to simulate a 50% chance of being drawn. Lambda functions at the top of the code are responsible for generating RGB values. 3r33300.  3r33312. 3r33300.  3r33312. The real trick of this feature is the creation of symmetry. Each square is associated with the value of the element. The figure below shows how the values ​​of the elements increase as they reach the center, and then decrease. Squares with matching element values ​​are displayed in one color. 3r33300.  3r33312. 3r33300.  3r33312. Element values ​​and symmetrical colors for the line in the sprite 7 × 7 [/i] 3r33300.  3r33312. 3r33300.  3r33312. Since create_square gets its parameters from create_invader , it uses the queue and the previous values ​​of the elements to ensure symmetry. When values ​​first appear, their colors are placed in a queue, and mirror squares remove colors. 3r33300.  3r33312. 3r33300.  3r33312. The complete process of creating [/i] 3r33300.  3r33312. 3r33300.  3r33312. I understand how difficult it is to read someone else’s solution to the problem and the code curve, but I hope that you will find use for it. It will be cool if you completely abandon my code and find a completely different solution. 3r33300.  3r33312. 3r33300.  3r33312.
Conclusion 3r3299. 3r33300.  3r33312. Generative art takes time to reach its full potential. In general, there may be more useful projects around than generative art, which is not always worth spending time on. But it's very fun and you never know where it might come in handy. 3r3308. 3r33312. 3r33312. 3r3305. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e. ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r3306. 3r33312. 3r3308. 3r33312. 3r33312. 3r33312. 3r33312.
+ 0 -

Add comment