用Python打印图形

Har*_*ark 5 python printing postscript

我需要从python中打印"Wheel Tags".车轮标签将包含图像,线条和文字.

Python教程有两段关于使用图像lib创建postscript文件.看完之后我还是不知道如何布局数据.我希望有人可能有如何布局图像,文字和线条的样本?

谢谢你的帮助.

Hug*_*ell 3

请参阅http://effbot.org/imagingbook/psdraw.htm

注意:

  1. 自 2005 年以来,PSDraw 模块似乎没有得到积极维护;我猜想大部分的努力都被转移到了支持 PDF 格式上。你可能更喜欢使用 pypdf 来代替;

  2. 它在源代码中有诸如“# FIXME:不完整”和“尚未实施”之类的注释

  3. 它似乎没有任何设置页面大小的方法 - 我记得这意味着它默认为 A4 (8.26 x 11.69 英寸)

  4. 所有测量值均以点为单位,每英寸 72 点。

您将需要执行以下操作:

import Image
import PSDraw

# fns for measurement conversion    
PTS = lambda x:  1.00 * x    # points
INS = lambda x: 72.00 * x    # inches-to-points
CMS = lambda x: 28.35 * x    # centimeters-to-points

outputFile = 'myfilename.ps'
outputFileTitle = 'Wheel Tag 36147'

myf = open(outputFile,'w')
ps = PSDraw.PSDraw(myf)
ps.begin_document(outputFileTitle)
Run Code Online (Sandbox Code Playgroud)

ps 现在是一个 PSDraw 对象,它将把 PostScript 写入指定的文件,并且文档标题已被写入 - 您已经准备好开始绘制东西了。

添加图像:

im = Image.open("myimage.jpg")
box = (        # bounding-box for positioning on page
    INS(1),    # left
    INS(1),    # top
    INS(3),    # right
    INS(3)     # bottom
)
dpi = 300      # desired on-page resolution
ps.image(box, im, dpi)
Run Code Online (Sandbox Code Playgroud)

添加文本:

ps.setfont("Helvetica", PTS(12))  # PostScript fonts only -
                                  # must be one which your printer has available
loc = (        # where to put the text?
    INS(1),    # horizontal value - I do not know whether it is left- or middle-aligned
    INS(3.25)  # vertical value   - I do not know whether it is top- or bottom-aligned
)
ps.text(loc, "Here is some text")
Run Code Online (Sandbox Code Playgroud)

要添加一行:

lineFrom = ( INS(4), INS(1) )
lineTo   = ( INS(4), INS(9) )
ps.line( lineFrom, lineTo )
Run Code Online (Sandbox Code Playgroud)

...而且我没有看到任何改变笔划粗细的选项。

完成后,您必须关闭文件,如下所示:

ps.end_document()
myf.close()
Run Code Online (Sandbox Code Playgroud)

编辑:我正在阅读一些关于设置笔画粗细的内容,并且遇到了一个不同的模块,psfile: http: //seehuhn.de/pages/psfile#sec :2.0.0 该模块本身看起来相当小 - 他正在写很多原始的后记 - 但它应该让你更好地了解幕后发生的事情。