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.mzph.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;该方案…