PyCairo – 绘制轮廓
在本文中,我们将学习如何在Python中使用 PyCairo 绘制实体表面的轮廓。轮廓基本上是对象的边界,用于突出显示对象。
PyCairo :Pycairo 是一个Python模块,为 Cairo 图形库提供绑定。这个库用于在Python中创建 SVG 即矢量文件。打开 SVG 文件以查看它(只读)的最简单快捷的方法是使用现代 Web 浏览器,如 Chrome、Firefox、Edge 或 Internet Explorer,几乎所有浏览器都应该为 SVG 格式提供某种渲染支持。
SVG:文件是一种图形文件,它使用由万维网联盟 (W3C) 创建的二维矢量图形格式。它使用基于 XML 的文本格式描述图像。 SVG 文件是作为在 Web 上显示矢量图形的标准格式而开发的。
实施步骤:
- 导入 Pycairo 模块。
- 创建一个 SVG 表面对象并向其添加上下文。
- 设置上下文的颜色和线宽
- 创建形状
- 保留对象的内部颜色,以便可以更改边框颜色,可以用作轮廓
- 设置边框的颜色和宽度,即轮廓
示例 1:
Python3
# importing pycairo
import cairo
# creating a SVG surface
# here geek95 is file name &
# 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
# creating a cairo context
#object for SVG surface
# using Context method
context= cairo.Context(surface)
# Creating shape
context.rectangle(25, 50, 50, 120)
# setting color of the context
context.set_source_rgb(1, 0, 0)
# Fill the color inside
context.fill()
# Creating shape
context.rectangle(125, 50, 50, 120)
# setting color of the context
context.set_source_rgb(0, 1, 1)
# Setting outline width
context.set_line_width(4)
# stroke out the color and width property
context.stroke()
# Creating shape
context.rectangle(225, 50, 50, 120)
# setting color of the context for inside
context.set_source_rgb(0, 0, 1)
# Preserving inside color of object
context.fill_preserve()
# setting color of the context for outline
context.set_source_rgb(1, 1, 0)
context.set_line_width(4)
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print("File Saved")
Python3
# importing pycairo
import cairo
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
# creating a cairo context
# object for SVG surface
# using Context method
context= cairo.Context(surface)
# Creating shape
context.arc(500, 60, 40, 0, 2*22/7)
# setting color of the context for inside
context.set_source_rgb(0, 0, 0)
# Preserving inside color of object
context.fill_preserve()
# setting color of the context for outline
context.set_source_rgb(1, 1, 0)
# Setting outline width
context.set_line_width(4)
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print("File Saved")
输出 :
示例 2:
蟒蛇3
# importing pycairo
import cairo
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
# creating a cairo context
# object for SVG surface
# using Context method
context= cairo.Context(surface)
# Creating shape
context.arc(500, 60, 40, 0, 2*22/7)
# setting color of the context for inside
context.set_source_rgb(0, 0, 0)
# Preserving inside color of object
context.fill_preserve()
# setting color of the context for outline
context.set_source_rgb(1, 1, 0)
# Setting outline width
context.set_line_width(4)
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print("File Saved")
输出 :