sql行數少于10_如何用少于100行的代碼創建生成藝術

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_squarecreate_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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393077.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393077.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393077.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

安裝輸入發

直接在系統 ——系統管理 ——語言支持 選——中文從新啟動 sudo apt-get install scim-pinyin安裝JAVA環境支持 sudo apt-get install sun-java-jre()要是 apt -get 命令不能用 可能是你 的 源有問題 可以 更新一下 在系統 -系統管理 源設置 選這臺灣的 就可以 …

(第2篇)一篇文章教你輕松安裝hadoop

摘要: 這篇文章將會手把手教你安裝hadoop,只要你細心按照文章中的步驟操作,hadoop肯定能正確安裝,絕對不會讓你崩潰 博主福利 給大家贈送一套hadoop視頻課程 授課老師是百度 hadoop 核心架構師 內容包括hadoop入門、hadoop生態架構以及大型ha…

linux cpio到指定目錄,linux cpio命令存取歸檔包中的文件

功能描述使用cpio命令可以通過重定向的方式將文件進行打包備份及還原恢復,它可以解壓縮以“.cpio”或者“.tar”結尾的文件。命令語 法cpio [選項] [目標目錄]選項含義選項含義-o執行 copy-out 模式,建立備份檔。-i執行 copy-in 模式,還原備份…

leetcode劍指 Offer 20. 表示數值的字符串

請實現一個函數用來判斷字符串是否表示數值(包括整數和小數)。例如,字符串"100"、“5e2”、"-123"、“3.1416”、"-1E-16"、“0123"都表示數值,但"12e”、“1a3.14”、“1.2.3”、"…

python接口自動化2-發送post請求

前言 發送post的請求參考例子很簡單,實際遇到的情況卻是很復雜的,首先第一個post請求肯定是登錄了,但登錄是最難處理的。登錄問題解決了,后面都簡單了。 一、查看官方文檔 1.學習一個新的模塊,其實不用去百度什么的&am…

簡介瀏覽器內核與JavaScript引擎

本文介紹了常用瀏覽器內核與JavaScript引擎 一、瀏覽器內核 Rending Engine, 顧名思義,稱之為渲染網頁內容的,將網頁的代碼轉換為你看得見的頁面,因為是排版,所以排版,所以肯定會有排版錯誤等問題。為什么會有排版錯誤…

Linux查看tar實用程序,linux tar指令常用選項

linux的tar指令經常被用到,因為壓縮文件的時候通常需要打包文檔,而tar指令就是打包指令,同時gzip壓縮程序和bzip2壓縮程序都是支持tar指令的,所以tar指令在打包的同時還可以用gzip和bzip進行壓縮,這樣多文件可以打包的…

代碼字體mono_如何構建代碼存儲庫:Multi,Mono或Organic?

代碼字體monoby Chetan Sharma由Chetan Sharma 如何構建代碼存儲庫:Multi,Mono或Organic? (How to Structure Code Repositories: Multi, Mono, or Organic?) The newest debate in town is whether you should keep your services in a si…

leetcode1424. 對角線遍歷 II(排序)

給你一個列表 nums ,里面每一個元素都是一個整數列表。請你依照下面各圖的規則,按順序返回 nums 中對角線上的整數。 示例 1: 輸入:nums [[1,2,3],[4,5,6],[7,8,9]] 輸出:[1,4,2,7,5,3,8,6,9] 代碼 class Solution …

DaVinci各版本安裝指南

鏈接: https://pan.baidu.com/s/1g1kaXZxcw-etsJENiW2IUQ?pwd0531 ? #2024版 1.鼠標右擊【DaVinci_Resolve_Studio_18.5(64bit)】壓縮包(win11及以上系統需先點擊“顯示更多選項”)【解壓到 DaVinci_Resolve_Studio_18.5(64bit)】。 2.打開解壓后的文…

使用 Servlet 讀取表單數據

Technorati 標簽: servlet;java 一、概述 Servlet 有一個比較好的功能就是可以自動處理表單提交的數據。我們只需要調用HttpServletRequest#getParameter(String name),就可以獲得指定參數的值(String),注意此方法是大小寫敏感的。…

linux文檔權限

1、登錄 root 用戶:su - mac一開始進入創建的用戶是具有管理員權限的用戶,但是密碼卻不是進入 root 用戶的密碼,可以使用 sudo su - 免密進入 root 用戶。 2、退出 root 用戶:exit 3、列出檔案(包括隱藏的檔案&#xf…

小程序 仿麥當勞_手機上的麥當勞和星巴克:為什么現在首先使用移動應用程序...

小程序 仿麥當勞by James Hsu由徐H 手機上的麥當勞和星巴克:為什么現在首先使用移動應用程序 (McDonald’s and Starbucks on Your Phone: Why Mobile Apps Are Now First on the Menu) One Friday this July, I got excited about McDonalds. So excited that I s…

MyLinkedList

/*** 節點類* author JP* */ class Node {Object value;//節點元素值Node pre;//上一個節點Node next;//下一個節點public Node(Object value) {this.value value;} }/*** 鏈表類* author JP**/ public class MyLinkedList {Node cur;//目前指向的節點Node head;//頭結點Node …

linux開啟ping服務,Linux 云服務器禁止和開啟Ping

原標題:Linux 云服務器禁止和開啟Ping在使用Linux服務器的時候,一般系統默認是開啟ping的,比如我們可以ping測試網絡的延遲質量。當然也有部分服務商是可以通過安全組設置禁止ping的,我們可以設置安全組對應項目開啟或禁止ping&am…

leetcode1432. 改變一個整數能得到的最大差值(貪心)

給你一個整數 num 。你可以對它進行如下步驟恰好 兩次 &#xff1a; 選擇一個數字 x (0 < x < 9). 選擇另一個數字 y (0 < y < 9) 。數字 y 可以等于 x 。 將 num 中所有出現 x 的數位都用 y 替換。 得到的新的整數 不能 有前導 0 &#xff0c;得到的新整數也 不能…

oracle之 Oracle歸檔日志管理

在Oracle中&#xff0c;數據一般是存放在數據文件中&#xff0c;不過數據庫與Oracle最大的區別之一就是數據庫可以在數據出錯的時候進行恢復。這個也就是我們常見的Oracle中的重做日志(REDO FILE)的功能了。在重做日志分成2部分&#xff0c;一個是在線重做日志文件&#xff0c;…

linux 安裝rmp服務,Linux LAMP服務的rpm包安裝與配置

1.apache(httpd) 所在光盤裝包apr-xxx.rpm (二)postgresql-libs-xxx.rpm (二)apr-util-xxx.rpm (二)http-xxx.rpm (二)2.mysql (二)裝包perl-DBI-xxx.rpm (二)mysql-xxx.rpm (二)3.mysqlserver裝包perl-DBD-mysql-xxx.rpm (二)mysql-server-xxx.rpm (二)4.PHP裝包gmp-xxx.rpm (…

紅外感應模塊+蜂鳴器實現簡易報警(轉)

拿到了一個紅外感應模塊HC-SR501&#xff0c;于是就用它和蜂鳴器簡單試驗了下。主要是試驗一下這個紅外感應模塊的功能&#xff0c;所以代碼也寫的很隨便啦&#xff0c;邏輯上也欠考慮。實現基本功能&#xff1a;運行腳本后&#xff0c;感應模塊每隔一定時間檢測&#xff0c;如…

leetcode51. N 皇后(回溯算法)

n 皇后問題研究的是如何將 n 個皇后放置在 nn 的棋盤上&#xff0c;并且使皇后彼此之間不能相互攻擊。 上圖為 8 皇后問題的一種解法。 給定一個整數 n&#xff0c;返回所有不同的 n 皇后問題的解決方案。 每一種解法包含一個明確的 n 皇后問題的棋子放置方案&#xff0c;該方案…