Python中的简单矢量图形
项目描述
Gizeh - Cairo为游客
Gizeh是一个用于矢量图形的Python库
# Let's draw a red circle !
import gizeh
surface = gizeh.Surface(width=320, height=260) # in pixels
circle = gizeh.circle(r=30, xy=[40,40], fill=(1,0,0))
circle.draw(surface) # draw the circle on the surface
surface.write_to_png("circle.png") # export the surface as a PNG
您可以在这篇博客文章中看到Gizeh的使用示例(与MoviePy结合制作动画)。
Gizeh是在模块cairocffi之上编写的,该模块是流行的C库Cairo的Python绑定。Cairo功能强大,但难以学习和使用。Gizeh在Cairo之上实现了一些类,使其更加直观。
Gizeh应在任何平台上以及Python 2和3上工作。
安装
要使用Gizeh,您必须首先在计算机上安装Cairo(请参阅他们的网站)。
Gizeh依赖于Python包cairocffi和Numpy。它们将自动安装(如果尚未安装)在安装Gizeh的过程中。如果安装有问题,请参阅README的最后一部分进行故障排除。如果这没有帮助,您可以在Github上寻求帮助。
从源代码安装:可以通过在某个目录中解压源代码并使用以下命令在同一目录中安装Gizeh。
(sudo) python setup.py install
使用pip安装:或者,您可以使用以下命令直接从Python包索引安装Gizeh。
(sudo) pip install gizeh
如果您的计算机上未安装ez_setup,则此方法可能会失败。在这种情况下,请首先使用以下命令安装ez_setup:
(sudo) pip install ez_setup
贡献!
用户指南
本指南以及位于 gizeh/examples 文件夹中的示例,将为您提供启动所需的一切。若要深入了解,请阅读函数文档字符串。
表面
表面是一个固定尺寸(以像素为单位)的矩形,您可以在其上绘制元素,并将其保存或导出为图像。
import gizeh
# initialize surface
surface = gizeh.Surface(width=320, height=260) # in pixels
# Now make a shape and draw it on the surface
circle = gizeh.circle(r=30, xy= [40,40], fill=(1,1,1))
circle.draw(surface)
# Now export the surface
surface.get_npimage() # returns a (width x height x 3) numpy array
surface.write_to_png("circle.png")
元素
基本元素包括圆形、矩形、线条、文本等,您可以使用 my_element.draw(surface) 在表面上绘制。您可以在创建时指定这些元素的属性和坐标。
xy:对象中心的坐标。在渲染时间(在 surface.write_to_png 函数中),您可以设置参数 y_origin 为 top(默认)或 bottom。如果将其设置为 top,则(0,0)对应于最终图片的左上角,而右下角的坐标为(width, height)。如果您选择 y_origin=bottom,则(0,0)将在图片的左下角(类似于标准绘图),而(width, height)将在右上角。
angle:元素围绕其中心 xy 的旋转角度(以弧度为单位)。
fill:填充元素的内容(默认不填充)。可以是颜色(R,G,B)、颜色渐变、图像等。请参见下面的章节。
stroke:填充元素轮廓的内容。与 fill 的规则相同。
stroke_width:元素轮廓的宽度(以像素为单位)。默认为 0(无轮廓)。
元素的示例
Pi = 3.14
circ = gizeh.circle(r=30, xy=(50,50), fill=(1,1,1))
rect = gizeh.rectangle(lx=60.3, ly=45, xy=(60,70), fill=(0,1,0), angle=Pi/8)
sqr = gizeh.square(l=20, stroke=(1,1,1), stroke_width= 1.5)
arc = gizeh.arc(r=20, a1=Pi/4, a2=3*Pi/4, fill=(1,1,1))
text = gizeh.text("Hello world", fontfamily="Impact", fontsize=40,
fill=(1,1,1), xy=(100,100), angle=Pi/12)
polygon = gizeh.regular_polygon(r=40, n=5, angle=np.pi/4, xy=[40,50], fill=(1,0,1))
line = gizeh.polyline(points=[(0,0), (20,30), (40,40), (0,10)], stroke_width=3,
stroke=(1,0,0), fill=(0,1,0))
填充和轮廓
当您创建一个形状时,fill 和 stroke 参数可以是以下之一
RGB 颜色,形式为 (r,g,b),其中每个元素介于 0 和 1 之间(1 是 100%)。
RGBA 颜色,形式为 (r,g,b,a),其中 a 介于 0(完全透明)和 1(完全不透明)之间。
gizeh.ColorGradient(请参见文档字符串)。
gizeh.ImagePattern,即图像(请参见文档字符串)。
表示 RGB 或 RGBA 图像的 numpy 数组(尚未实现)。
PNG 图像文件(尚未实现)。
变换
任何元素都可以进行变换(平移、旋转或缩放)。所有变换都是 原地:它们不会修改原始元素,而是创建其修改后的版本。
示例
square_1 = gizeh.square(l=20, xy = [30,35], fill=(1,0,0))
square_2 = square_1.rotate(Pi/8) # rotation around [0,0] by default
square_3 = square_2.rotate(Pi/4, center=[10,15]) # rotation around a center
square_4 = square_1.scale(2) # two times bigger
square_5 = square1.scale(sx=2, sy=3) # width times 2, height times 3
square_6 = square_1.scale(2, center=[30,30]) # zoom: scales around a center
square_7 = square_1.translate(xy=[5,15]) # translation
组
组是一组将一起变换和绘制的元素。这些元素可以是基本元素(正方形、圆形等),甚至可以是组。
示例
square = gizeh.square(l=20, fill=(1,0,0), xy=(40,40))
circle = gizeh.circle(r=20, fill=(1,2,0), xy=(50,30))
group_1 = gizeh.Group([square, circle])
group_2 = group.translate(xy=[30,30]).rotate(Pi/4)
group_3 = gizeh.Group([circle, group_1])
surface = gizeh.Surface(width=300,height=200)
group.draw(surface)
group_1.draw(surface)
group_2.draw(surface)
group_3.draw(surface)
surface.write_to_png("my_masterwork.png")
以上就是所有内容!
这就是所有需要了解的内容。若要进一步了解,请参阅 examples 文件夹中的示例或直接在代码中的文档。
安装支持
有时通过 pip 安装失败,因为
有些人安装 cairocffi 时遇到了问题,以下是解决他们问题的方法
在 Debian/Ubuntu 上
sudo apt-get install python-dev python-pip ffmpeg libffi-dev sudo pip install gizeh
在 macOSX 上
pip install ez_setup brew install pkg-config libffi export PKG_CONFIG_PATH=/usr/local/Cellar/libffi/3.0.13/lib/pkgconfig/ # go to https://xquartz.macosforge.org and download and install XQuartz, # which is needed for cairo, then... brew install cairo pip install gizeh
项目详情
gizeh-0.1.11.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a12422d4d16e8b3dbdf2d51cb6633855c460404ace34c6509db30f7e3d55177b |
|
MD5 | c36642771860047284fabd6a39d49697 |
|
BLAKE2b-256 | 28e458a0811c60f72b6a4d69b780f1b7d718061170042eb112e86f7c5c4981af |