加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

用Python打印图形

发布时间:2020-12-16 22:20:43 所属栏目:Python 来源:网络整理
导读:我需要从python中打

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

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

谢谢你的帮助.

最佳答案
见http://effbot.org/imagingbook/psdraw.htm

注意:

>自2005年以来,PSDraw模块似乎没有得到积极维护;我猜大多数努力都被重定向到支持PDF格式.你可能会更喜欢使用pypdf;
>它在源代码中有’#FIXME:不完整’和’NOT YET IMPLEMENTED’之类的评论
>它似乎没有任何设置页面大小的方法 – 我记得它默认为A4(8.26 x 11.69英寸)
>所有测量均以点为单位,每英寸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)

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)

要添加文字:

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")

要添加一行:

lineFrom = ( INS(4),INS(1) )
lineTo   = ( INS(4),INS(9) )
ps.line( lineFrom,lineTo )

……我没有看到任何改变中风重量的选择.

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

ps.end_document()
myf.close()

编辑:我正在做一些关于设置笔触权重的阅读,我遇到了一个不同的模块,psfile:http://seehuhn.de/pages/psfile#sec:2.0.0模块本身看起来很小 – 他正在写很多原始的后记 – 但它应该让你更好地了解什么是在幕后进行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读