python - 绘制简单的形状并保存到文件 (pdf)

标签 python draw

我正在寻找一个 python 库,我可以用它来绘制简单的形状和字符,然后保存到一个文件(格式可转换为 pdf)。如果我不需要运行 X 服务器,我会更愿意。

例如可能看起来像这样

import drawing_lib
obj = drawing_lib.Object()
for i in range(5):
    obj.draw_line(from=(i*10, 20), to=(i*10+10, 35))
obj.save_pdf('five_inclined_lines.pdf')

有什么想法吗?

最佳答案

您可以使用 cairo 执行此操作。

import math,cairo

width, height = 768,768
surface = cairo.PDFSurface ("circle.pdf", width, height)
ctx = cairo.Context (surface)
ctx.set_source_rgb(1,1,1)
ctx.rectangle(0,0,width,height)
ctx.fill()
ctx.set_source_rgb(1,0,0)
ctx.move_to(width/2,height/2)
ctx.arc(width/2,height/2,512*0.25,0,math.pi*2)
ctx.fill()
ctx.show_page()

另见:

关于python - 绘制简单的形状并保存到文件 (pdf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8792570/

相关文章:

java - 为什么我的屏幕上什么也没有绘制?

wpf - 动画路径就像在 Canvas 上绘制一样

android:运行时调整大小的图像质量

python - 如何在 Python scikit-learn 中输出随机森林中每棵树的回归预测?

python - 获取键对应于字典中的值

java - 如何在java中的图像上绘制符号(正确或错误)?

java - 在简单的 Swing 程序中将正方形更改为矩形

Python加速列表中的元素

python - Tensorflow:张量 Tensor ("Placeholder:0", shape=(?, 3), dtype=float32) 不是该图的元素

python - os.path.exists 和 os.path.isfile 的区别?