samila

samila

Python生成艺术库创造随机独特图像

Samila是一个Python生成艺术库,能基于数千个随机计算的点创建独特图像。它支持多种投影方式、标记类型和颜色选项,可生成富有表现力的艺术作品。Samila还提供图像保存、重现和NFT上传功能,方便数字艺术创作。每次生成的图像都呈现不同效果,为艺术家提供了无限可能。

Samila生成艺术Python图像生成数学公式Github开源项目
<div align="center"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/19e319e5-5867-4169-8845-360389b29ca9.png" width=400 height=400> <br/> <h1>Samila</h1> <br/> <a href="https://www.python.org/"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/86b68135-455c-48d6-8785-473876a7adea.svg" alt="使用Python3构建" /> </a> <a href="https://codecov.io/gh/sepandhaghighi/samila"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/a471d44c-968f-48f6-83f9-55883e753ae9.svg" /> </a> <a href="https://badge.fury.io/py/samila"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/0fc45da6-ff02-4a30-813c-031198be01b1.svg" alt="PyPI版本" height="18"> </a> <a href="https://anaconda.org/sepandhaghighi/samila"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/b6a73fa1-f127-4364-aaff-592566b5485f.svg"> </a> <a href="https://colab.research.google.com/github/sepandhaghighi/samila/blob/master"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/97795732-46f8-4cd9-b100-4dc4d18c1fef.svg" alt="Samila-Colab"/> </a> <a href="https://discord.com/invite/94bz5QGZWb"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/999d2862-53a2-458b-94e1-810b29749beb.svg" alt="Discord频道"> </a> </div>

概述

<p align="justify"> Samila是一个用Python编写的生成艺术生成器,它允许你基于数千个点创建图像。每个点的位置都由一个具有随机参数的公式计算得出。由于随机数的存在,每幅图像看起来都不同。 </p> <table> <tr> <td align="center">Open Hub</td> <td align="center"><a href="https://www.openhub.net/p/samila"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/6fadca68-f2ec-4514-915d-bd8a243665af.gif"></a></td> </tr> <tr> <td align="center">PyPI计数器</td> <td align="center"><a href="http://pepy.tech/project/samila"><img src="http://pepy.tech/badge/samila"></a></td> </tr> <tr> <td align="center">Github星标</td> <td align="center"><a href="https://github.com/sepandhaghighi/samila"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/805f81d7-570a-4934-b054-e1dc2e73a87e.svg?style=social&label=Stars"></a></td> </tr> </table> <table> <tr> <td align="center">分支</td> <td align="center">master</td> <td align="center">dev</td> </tr> <tr> <td align="center">CI</td> <td align="center"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/ac9a86cf-1f74-4753-b7c3-2efce52d84ed.svg?branch=master"></td> <td align="center"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/ac9a86cf-1f74-4753-b7c3-2efce52d84ed.svg?branch=dev"></td> </tr> </table> <table> <tr> <td align="center">代码质量</td> <td><a href="https://www.codacy.com/gh/sepandhaghighi/samila/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=sepandhaghighi/samila&amp;utm_campaign=Badge_Grade"><img src="https://app.codacy.com/project/badge/Grade/14df8ed5f8434aaea85889555b0182a9"/></a></td> <td><a href="https://codebeat.co/projects/github-com-sepandhaghighi-samila-dev"><img alt="codebeat badge" src="https://codebeat.co/badges/01e6aa48-4cc2-4d9c-8288-c9fb490ad371" /></a></td> <td><a href="https://www.codefactor.io/repository/github/sepandhaghighi/samila"><img src="https://www.codefactor.io/repository/github/sepandhaghighi/samila/badge" alt="CodeFactor" /></a></td> </tr> </table>

安装

PyPI

源代码

Conda

使用

魔法

>>> import matplotlib.pyplot as plt >>> from samila import GenerativeImage >>> g = GenerativeImage() >>> g.generate() >>> g.plot() >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/5aec661d-9848-4798-a31d-37aa0968deea.png">
  • 你可以通过GenerativeImage中的func_seed参数更改函数生成种子

基础

>>> import random >>> import math >>> def f1(x, y): result = random.uniform(-1,1) * x**2 - math.sin(y**2) + abs(y-x) return result >>> def f2(x, y): result = random.uniform(-1,1) * y**3 - math.cos(x**2) + 2*x return result >>> g = GenerativeImage(f1, f2) >>> g.generate() >>> g.plot() >>> g.seed 188781 >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/09f5babd-7914-40d6-8e11-1a57e3cc985b.png">

投影

>>> from samila import Projection >>> g = GenerativeImage(f1, f2) >>> g.generate() >>> g.plot(projection=Projection.POLAR) >>> g.seed 829730 >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/0dc0dd1a-a092-424f-8328-2eb2a984de9a.png">
  • 支持的投影:RECTILINEAR, POLAR, AITOFF, HAMMER, LAMBERT, MOLLWEIDERANDOM
  • 默认投影为 RECTILINEAR

标记

>>> from samila import Marker >>> g = GenerativeImage(f1, f2) >>> g.generate() >>> g.plot(marker=Marker.CIRCLE, spot_size=10) >>> g.seed 448742 >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/84f71333-4141-46d8-bd25-a607950357ef.png">
  • 支持的标记:POINTPIXELCIRCLETRIANGLE_DOWNTRIANGLE_UPTRIANGLE_LEFTTRIANGLE_RIGHTTRI_DOWNTRI_UPTRI_LEFTTRI_RIGHTOCTAGONSQUAREPENTAGONPLUSPLUS_FILLEDSTARHEXAGON_VERTICALHEXAGON_HORIZONTALXX_FILLEDDIAMONDDIAMON_THINVLINEHLINERANDOM
  • 默认标记为POINT

旋转

你甚至可以使用rotation参数旋转你的艺术作品。输入你想要的图像旋转角度,你就能得到它。

>>> g = GenerativeImage(f1, f2) >>> g.generate() >>> g.plot(rotation=45)
  • 默认旋转角度为0

范围

>>> g = GenerativeImage(f1, f2) >>> g.generate(start=-2*math.pi, step=0.01, stop=0) >>> g.plot() >>> g.seed 234752 >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/53ce842e-f2d9-4b4b-a943-c54764f31321.png">

颜色

>>> g = GenerativeImage(f1, f2) >>> g.generate() >>> g.plot(color="yellow", bgcolor="black", projection=Projection.POLAR) >>> g.seed 1018273 >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/d93829d7-7c88-4623-adce-ed79a3f5be3b.png">
  • 可用的颜色在VALID_COLORS列表中

  • colorbgcolor参数支持的格式:

    1. 颜色名称(例如:color="yellow"
    2. RGB/RGBA(例如:color=(0.1,0.1,0.1)color=(0.1,0.1,0.1,0.1)
    3. 十六进制(例如:color="#eeefff"
    4. 随机(例如:color="random"
    5. 补色(例如:color="complement", bgcolor="blue"
    6. 透明(例如:bgcolor="transparent"
    7. 列表(例如:color=["black", "#fffeef",...]

⚠️ 透明模式仅适用于背景

⚠️ 列表模式仅适用于颜色

⚠️ 在列表模式下,此列表的长度必须等于data1和data2的长度

点颜色

你可以制作自定义颜色映射并在Samila中使用它

>>> colorarray = [ ... [0.7, 0.2, 0.2, 1], ... [0.6, 0.3, 0.2, 1], ... "black", ... [0.4, 0.4, 0.3, 1], ... [0.3, 0.4, 0.4, 1], ... "#ff2561"] >>> g.generate() >>> g.seed 454893 >>> g.plot(cmap=colorarray, color=g.data2, projection=Projection.POLAR) >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/abd86c43-c62f-4320-8225-5de07ef1481e.png">

重新生成

>>> g = GenerativeImage(f1, f2) >>> g.generate(seed=1018273) >>> g.plot(projection=Projection.POLAR) >>> plt.show()
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/4ffdac20-48fe-4d84-bcd9-7de14d973b41.png">

NFT.storage

直接将生成的图像上传到NFT.storage

>>> g.nft_storage(api_key="YOUR_API_KEY", timeout=5000) {'status': True, 'message': 'FILE_LINK'}

你也可以将配置/数据上传到NFT.storage,如下所示:

>>> g.nft_storage(api_key="API_KEY", upload_config=True) {'status': {'image': True, 'config':True}, 'message': {'image':'IMAGE_FILE_LINK', 'config':'CONFIG_FILE_LINK'}

>>> g.nft_storage(api_key="API_KEY", upload_data=True) {'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'}

你可以选择特定的IPFS网关:

>>> from samila import Gateway >>> g.nft_storage(api_key="API_KEY", upload_data=True, gateway=Gateway.DWEB) {'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'}
  • 默认超时时间为3000
  • 默认网关为IPFS_IO

保存图像

保存生成的图像

>>> g.save_image(file_adr="test.png") {'status': True, 'message': 'FILE_PATH'}

以更高分辨率保存生成的图像

>>> g.save_image(file_adr="test.png", depth=5) {'status': True, 'message': 'FILE_PATH'}

保存数据

保存生成的图像数据

>>> g.save_data(file_adr="data.json") {'status': True, 'message': 'FILE_PATH'}

这样你之后可以通过以下方式将其加载到GenerativeImage实例中

>>> g = GenerativeImage(data=open('data.json', 'r'))

数据结构:

{ "plot": { "projection": "polar", "bgcolor": "black", "color": "snow", "spot_size": 0.01 }, "matplotlib_version": "3.0.3", "data1": [ 0.3886741692042526, 22.57390286376703, -0.1646310981668766, 66.23632344600155 ], "data2": [ -0.14588750183600108, 20.197945942677833, 0.5485453260942901, -589.3284610518896 ] }

保存配置

保存生成的图像配置。它包含函数的字符串格式,也是人类可读的。

>>> g.save_config(file_adr="config.json") {'status': True, 'message': 'FILE_PATH'}

这样你之后可以通过以下方式将其加载到GenerativeImage实例中

>>> g = GenerativeImage(config=open('config.json', 'r'))

配置结构:

{ "matplotlib_version": "3.0.3", "generate": { "seed": 379184, "stop": 3.141592653589793, "step": 0.01, "start": -3.141592653589793 }, "f2": "random.uniform(-1,1)*math.cos(x*(y**3))+random.uniform(-1,1)*math.ceil(y-x)", "f1": "random.uniform(-1,1)*math.ceil(y)-random.uniform(-1,1)*y**2+random.uniform(-1,1)*abs(y-x)", "plot": { "color": "snow", "bgcolor": "black", "projection": "polar", "spot_size": 0.01 } }

数学细节

Samila 实质上是将笛卡尔坐标系中的方形空间转换为任意坐标系(如极坐标系)的变换。

示例

<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/f6ff927b-0398-441d-96ff-fde897b54cc0.png">

我们在第一个空间(左侧方形)中有一组点,可以定义如下:

<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/17d2a928-286e-4ce1-a804-9ca737dda2af.jpg">

以下函数用于转换:

>>> def f1(x, y): result = random.uniform(-1,1) * x**2 - math.sin(y**2) + abs(y-x) return result >>> def f2(x, y): result = random.uniform(-1,1) * y**3 - math.cos(x**2) + 2*x return result
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/c50de4d3-8b7a-431e-a766-fd32ce86d6ea.jpg">

这里我们使用 Projection.POLAR,所以后面的空间将是极坐标空间,我们有:

>>> g = GenerativeImage(f1, f2) >>> g.generate(seed=10) >>> g.plot(projection=Projection.POLAR)
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/dcf1ccfc-9f16-4356-840e-7563f7d65149.jpg"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/bb3db8c7-ba00-4d99-a39c-8ccca51fa10b.png">

在浏览器中试用 Samila!

Samila 可以通过 Binder 或 Colab 服务在线使用交互式 Jupyter Notebooks!现在就来试试吧!:

Binder

Google Colab

  • 查看 examples 文件夹

问题与错误报告

只需填写一个 issue 并描述问题。我们会尽快检查!或发送电子邮件至 info@samila.site

  • 请完整填写 issue 模板

你也可以加入我们的 Discord 服务器

<a href="https://discord.com/invite/94bz5QGZWb"> <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/999d2862-53a2-458b-94e1-810b29749beb.svg?style=for-the-badge" alt="Discord 频道"> </a>

社交媒体

  1. Instagram
  2. Telegram
  3. Twitter
  4. Discord

参考文献

<blockquote>1- Schönlieb, Carola-Bibiane, and Franz Schubert. "Random simulations for generative art construction–some examples." Journal of Mathematics and the Arts 7.1 (2013): 29-39.</blockquote> <blockquote>2- <a href="https://github.com/cutterkom/generativeart">使用 R 创建生成艺术</a></blockquote> <blockquote>3- <a href="https://nft.storage/">NFT.storage : 免费去中心化存储和 NFT 带宽</a></blockquote>

致谢

本项目由 Protocol Labs 设立的 Next Step Microgrant 计划资助。

表示支持

<h3>为这个仓库点星</h3>

如果这个项目对你有帮助,请给一个 ⭐️!

<h3>为我们的项目捐款</h3>

如果你喜欢我们的项目,我们希望你能支持我们。我们的项目现在不是也永远不会以盈利为目的。我们需要这笔钱只是为了继续我们正在做的事情 ;-) 。

<a href="http://www.samila.site/donate.html" target="_blank"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/3c6601f2-4f06-4b75-a430-5586d6a441bc.png" height="90px" width="270px" alt="Samila 捐款"></a>

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

扣子-AI办公

扣子-AI办公

AI办公助手,复杂任务高效处理

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
下拉加载更多