sql行數少于10
by Eric Davidson
埃里克·戴維森(Eric Davidson)
如何用少于100行的代碼創建生成藝術 (How to Create Generative Art In Less Than 100 Lines Of Code)
Generative art, like any programming topic, can be intimidating if you’ve never tried it before. I’ve always been interested in it because I love finding new ways that programming can be utilized creatively. Furthermore, I think anyone can appreciate the concept of artwork that literally creates itself.
如果您從未嘗試過生成藝術,那么與任何編程主題一樣,它可能會令人生畏。 我一直對此感興趣,因為我喜歡找到可以創造性地利用編程的新方法。 此外,我認為任何人都可以欣賞真正創造自己的藝術品的概念。
什么是生成藝術? (What is generative art?)
Generative art is the output of a system that makes its own decisions about the piece, rather than a human. The system could be as simple as a single Python program, as long as it has rules and some aspect of randomness.
生成藝術是系統對作品(而不是人類)做出自己的決定的結果。 只要具有規則和某種程度上的隨機性 ,該系統就可以像單個Python程序一樣簡單。
With programming, it’s pretty straightforward to come up with rules and constraints. That’s all conditional statements are. Having said that, finding ways to make these rules create something interesting can be tricky.
通過編程,提出規則和約束非常簡單。 這就是所有條件語句。 話雖如此,尋找使這些規則產生有趣效果的方法可能很棘手。
The Game of Life is a famous set of four simple rules that determine the “birth” and “death” of each cell in the system. Each of the rules play a part in advancing the system through each generation. Although the rules are simple and easy to understand, complex patterns quickly begin to emerge and ultimately form fascinating results.
生命游戲是一組著名的四個簡單規則,它們確定系統中每個單元的“出生”和“死亡”。 每條規則在每一代的系統升級中都發揮著作用。 盡管規則簡單易懂,但復雜的模式很快就開始出現并最終形成了令人著迷的結果。
Rules may be responsible for creating the foundation of something interesting, but even something as exciting as Conway’s Game of Life is predictable. Since the four rules are the determining factors for each generation, the way to produce unforeseeable results is to introduce randomization at the starting state of the cells. Beginning with a random matrix will make each execution unique without needing to change the rules.
規則可能會為有趣的事物打下基礎,但即使像康威的人生游戲這樣令人興奮的事物也是可以預見的。 由于這四個規則是每一代的決定因素,因此產生不可預知結果的方法是在細胞的起始狀態引入隨機化。 以隨機矩陣開頭將使每次執行都是唯一的,而無需更改規則。
The best examples of generative art are the ones that find a combination of predictability and randomness in order to create something interesting that is also statistically irreproducible.
生成藝術的最好例子是那些發現可預測性和隨機性相結合的例子,以創造出一些在統計上也無法再現的有趣事物。
為什么要嘗試呢? (Why should you try it?)
Not all side projects are created equal, and generative art may not be something you’re inclined to spend time on. If you decide to work on a project however, then you can expect these benefits:
并非所有的副項目都是平等創建的,生成藝術可能不是您傾向于花費時間的東西。 但是,如果您決定從事一個項目,那么您可以期望獲得以下好處:
Experience — Generative art is just another opportunity to hone some new and old skills. It can serve as a gateway to practicing concepts like algorithms, data structures, and even new languages.
經驗 -生成藝術只是磨練一些新技能和舊技能的另一個機會。 它可以充當實踐算法,數據結構甚至新語言等概念的門戶。
Tangible Results — In the programming world we rarely get to see any thing physical come out of our efforts, or at least I don’t. Right now I have a few posters in my living room displaying prints of my generative art and I love that programming is responsible for that.
有形的結果 —在編程世界中,我們很少能看到從我們的努力中產生的任何實際結果,或者至少我沒有發現。 現在,我的客廳里有幾張海報,展示著我的生成藝術作品,我喜歡編程對此負責。
Attractive Projects — We’ve all had the experience of explaining a personal project to someone, possibly even during an interview, without an easy way to convey the effort and results of the project. Generative art speaks for itself, and most anyone will be impressed by your creations, even if they can’t fully understand the methods.
有吸引力的項目 -我們都有過向某人解釋個人項目的經驗,甚至在面試過程中,都沒有簡單的方法來傳達項目的成果和努力。 生成藝術本身就可以說明一切,即使他們不能完全理解這些方法,大多數人也會對您的創作印象深刻。
你應該從哪里開始? (Where should you start?)
Getting started with generative art is the same process as any project, the most crucial step is to come up with an idea or find one to build upon. Once you have a goal in mind, then you can start working on the technology required to achieve it.
生成藝術的入門與任何項目都是相同的過程,最關鍵的一步是想出一個主意或找到一個要建立的主意。 一旦有了目標,就可以開始研究實現該目標所需的技術。
Most of my generative art projects have been accomplished in Python. It’s a fairly easy language to get used to and it has some incredible packages available to help with image manipulation, such as Pillow.
我的大部分生成藝術項目都是使用Python完成的。 這是一種相當容易使用的語言,并且它提供了一些令人難以置信的軟件包來幫助圖像處理,例如Pillow 。
Luckily for you, there’s no need to search very far for a starting point, because I’ve provided some code down below for you to play with.
幸運的是,您無需在很遠的地方尋找起點,因為我在下面提供了一些代碼供您使用。
精靈生成器 (Sprite Generator)
This project started when I saw a post showing off a sprite generator written in Javascript. The program created 5x5 pixel art sprites with some random color options and its output resembled multi-colored space invaders.
當我看到一個帖子展示了用Java語言編寫的Sprite生成器時,該項目開始了。 該程序創建了具有一些隨機顏色選項的5x5像素美術圖片,其輸出類似于多色空間入侵者。
I knew that I wanted to practice image manipulation in Python, so I figured I could just try to recreate this concept on my own. Additionally, I thought that I could expand on it since the original project was so limited in the size of the sprites. I wanted to be able to specify not only the size, but also the number of them and even the size of the image.
我知道我想用Python練習圖像操作,所以我想自己可以嘗試重新創建這個概念。 另外,我認為我可以擴展它,因為原始項目的精靈大小非常有限。 我希望不僅可以指定大小,還可以指定它們的數量,甚至圖像的大小。
Here’s a look at two different outputs from the solution I ended up with:
這是我最終得到的解決方案的兩個不同輸出:
These two images don’t resemble each other at all, but they’re both the results of the same system. Not to mention, due to the complexity of the image and the randomness of the sprite generation, there is an extremely high probability that even with the same arguments, these images will forever be one of a kind. I love it.
這兩張圖片一點都不相似,但是它們都是同一系統的結果。 更不用說,由于圖像的復雜性和子畫面生成的隨機性 ,即使具有相同的論點,這些圖像也將永遠是其中之一的可能性非常高。 我喜歡它。
環境 (The environment)
If you want to start playing around with the sprite generator, there’s a little foundation work that has to be done first.
如果您想開始使用Sprite生成器,那么首先需要完成一些基礎工作。
Setting up a proper environment with Python can be tricky. If you haven’t worked with Python before, you’ll probably need to download Python 2.7.10. I initially had trouble setting up the environment, so if you start running into problems, you can do what I did and look into virtual environments. Last but not least, make sure you have Pillow installed as well.
使用Python設置適當的環境可能很棘手。 如果您以前從未使用過Python,則可能需要下載Python 2.7.10。 最初,我在設置環境時遇到了麻煩,因此,如果您開始遇到問題,則可以做我所做的事情,并研究虛擬環境 。 最后但并非最不重要的一點,請確保還安裝了枕頭 。
Once you have the environment set up, you can copy my code into a file with extension .py and execute with the following command:
設置好環境后,可以將我的代碼復制到擴展名為.py的文件中,并使用以下命令執行:
python spritething.py [SPRITE_DIMENSIONS] [NUMBER] [IMAGE_SIZE]
For example, the command to create the first matrix of sprites from above would be:
例如,從上方創建子畫面的第一個矩陣的命令為:
python spritething.py 7 30 1900
代碼 (The code)
import PIL, random, sysfrom PIL import Image, ImageDraw
origDimension = 1500
r = lambda: random.randint(50,215)rc = lambda: (r(), r(), r())
listSym = []
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): x0, y0, x1, y1 = border squareSize = (x1-x0)/size randColors = [rc(), rc(), rc(), (0,0,0), (0,0,0), (0,0,0)] i = 1
for y in range(0, size): i *= -1 element = 0 for x in range(0, 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): i *= -1; 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(0, invaders): for y in range(0, 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")
if __name__ == "__main__": main(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]))
This solution is a long way from perfect, but it shows that creating generative art doesn’t take a ton of code. I’ll try my best to explain the key pieces.
這個解決方案離完美還差很遠,但是它表明創建生成藝術并不需要大量代碼。 我將盡力解釋關鍵部分。
The main function starts by creating the initial image and determining the size of the sprites. The two for loops are responsible for defining a border for each sprite, basically dividing the dimensions of the image by the number of sprites requested. These values are used to determine the coordinates for each one.
主要功能從創建初始圖像并確定子畫面的大小開始。 兩個for循環負責為每個sprite定義一個邊界,基本上將圖像的尺寸除以請求的sprite的數量。 這些值用于確定每個坐標。
Let’s ignore padding and take a look at the image below. Imagine that each of the four squares represents a sprite with a size of 1. The border that is being passed to the next function refers to the top left and bottom right coordinates. So the tuple for the top left sprite would be (0,0,1,1) whereas the tuple for the top right would be (1,0,2,1). These will be used as the dimensions and base coordinates for the squares of each sprite.
讓我們忽略填充,看看下面的圖片。 想象一下,四個正方形中的每個正方形代表一個大小為1的精靈。傳遞給下一個函數的邊框是指左上角坐標和右下角坐標。 因此,左上方精靈的元組為(0,0,1,1),而右上方精靈的元組為(1,0,2,1)。 這些將用作每個精靈的尺寸和基本坐標。
The function create_invader determines the border for each square within the sprite. The same process for determining the border is applied here and represented below, only instead of the full image we’re using a pre-determined border to work inside. These final coordinates for each square will be used in the next function to actually draw the sprite.
函數create_invader確定精靈中每個正方形的邊界。 確定邊界的過程與此處相同,并在下面進行了介紹,只是在內部使用預先確定的邊界代替完整圖像。 每個正方形的這些最終坐標將在下一個函數中用于實際繪制精靈。
To determine the color, a simple array of three random RGB tuples and three blacks are used to simulate a 50% chance of being drawn. The lambda functions near the top of the code are responsible for generating the RGB values.
為了確定顏色,使用了一個由三個隨機RGB元組和三個黑色組成的簡單數組來模擬被繪制的機會為50%。 代碼頂部附近的lambda函數負責生成RGB值。
The real trick of this function is creating symmetry. Each square is paired with an element value. In the image below you can see the element values increment as they reach the center and then decrement. Squares with matching element values are drawn with the same color.
此功能的真正技巧是創建對稱性。 每個正方形都與一個元素值配對。 在下圖中,您可以看到元素值在到達中心時增加,然后減少。 具有匹配元素值的正方形用相同的顏色繪制。
As create_square receives its parameters from create_invader, it uses a queue and the element values from before to ensure symmetry. The first occurrence of the values have their colors pushed onto the queue and the mirrored squares pop the colors off.
當create_square從create_invader接收其參數時,它將使用隊列和之前的元素值來確保對稱。 值的第一次出現將其顏色壓入隊列,并且鏡像的正方形彈出顏色。
I realize how difficult it is to read through and understand someone else’s solution for a problem, and the roughness of the code certainly does not help with its complexity, but hopefully you’ve got a pretty good idea for how it works. Ultimately it would be incredible if you are able to scrap my code altogether and figure out an entirely different solution.
我意識到通讀并理解別人對問題的解決方案有多么困難,并且代碼的粗糙性當然不會幫助解決它的復雜性,但是希望您對它的工作原理有了一個很好的認識。 最終,如果您能夠完全刪除我的代碼并找出完全不同的解決方案,那將是不可思議的。
結論 (Conclusion)
Generative art takes time to fully appreciate, but it’s worth it. I love being able to combine programming with a more traditional visual, and I have definitely learned a lot in every one of my projects.
生成藝術需要時間來充分欣賞,但這是值得的。 我喜歡能夠將編程與更傳統的視覺效果相結合,并且我在每個項目中都學到了很多東西。
Overall there may be more useful projects to pursue and generative art may not be something you need experience with, but it’s a ton of fun and you never know how it might separate you from the crowd.
總體而言,可能會有更多有用的項目需要追求,而生成藝術可能并不是您需要的經驗,但這是一大堆樂趣,而且您永遠都不知道它如何使您與眾不同。
Thank you for reading!
感謝您的閱讀!
翻譯自: https://www.freecodecamp.org/news/how-to-create-generative-art-in-less-than-100-lines-of-code-d37f379859f/
sql行數少于10