PyCairo – 我们如何复制扁平化路径?
在本文中,我们将学习如何在Python中使用 PyCairo 复制对象的扁平路径。此函数类似于 copy_path() 方法,不同之处在于路径中的任何曲线都将使用分段线性近似值进行近似。
PyCairo :Pycairo 是一个Python模块,为 cairo 图形库提供绑定。该库用于在Python中创建 SVG,即矢量文件。打开 SVG 文件进行查看(只读)的最简单快捷的方法是使用现代 Web 浏览器,如 Chrome、Firefox、Edge 或 Internet Explorer——几乎所有这些浏览器都应该为 SVG 格式提供某种渲染支持。
SVG文件是使用万维网联盟 (W3C) 创建的二维矢量图形格式的图形文件。它使用基于 XML 的文本格式描述图像。 SVG 文件是作为在 Web 上显示矢量图形的标准格式而开发的。
In order to this we will use copy_path_flat() method with the Context object
Syntax : context.copy_path_flat()
Argument : It takes no arguments
Return : It returns path with piecewise-linear approximations
例子 :
Python3
# importing pycairo
import cairo
# creating a SVG surface
# here geek1 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek1.svg", 700, 700) as surface:
# creating a cairo context object
context = cairo.Context(surface)
# creating a arc without using closing path method
context.arc(100, 60, 40, 0, 1*22/7)
# stroke the context to remove the moved pen
context.stroke()
# creating a arc with using close path method
context.arc(300, 60, 40, 0, 1*22/7)
# makeing close path
context.close_path()
# Gets a flattened copy of the current path
c = context.copy_path_flat()
# stroke the context to remove the moved pen
context.stroke()
# printing message when file is saved
print(c)
输出 :
move_to 340.000000 60.000000
line_to 339.800781 63.953125
line_to 339.218750 67.839844
line_to 338.261719 71.632813
line_to 336.949219 75.312500
line_to 335.277344 78.835938
line_to 333.269531 82.199219
line_to 330.929688 85.355469
line_to 328.273438 88.292969
line_to 325.332031 90.945313
line_to 322.175781 93.285156
line_to 318.812500 95.289063
line_to 315.285156 96.960938
line_to 311.609375 98.273438
line_to 307.816406 99.226563
line_to 303.929688 99.800781
line_to 299.976563 100.000000
line_to 296.011719 99.796875
line_to 292.125000 99.214844
line_to 288.332031 98.253906
line_to 284.660156 96.941406
line_to 281.128906 95.265625
line_to 277.773438 93.253906
line_to 274.617188 90.914063
line_to 271.687500 88.257813
line_to 269.027344 85.316406
line_to 266.691406 82.152344
line_to 264.683594 78.789063
line_to 263.023438 75.261719
line_to 261.710938 71.582031
line_to 260.761719 67.789063
line_to 260.187500 63.902344
line_to 260.000000 59.949219
close path
move_to 340.000000 60.000000