返回的对象elaphe.barcode
是一个EpsImageFile
(其中 EPS 表示 Encapsulated PostScript),但在调用barcode
它之后还没有运行 Ghostscript 将代码转换为位图图像。
您可以通过查看fp
属性来转储它生成的代码 - 它有很多,因为它为它支持的所有不同的条形码类型嵌入了完整的 PS 库代码。所以最好把它写到一个文件中:
b = el.barcode('qr', 'slamacow')
with open('code.eps') as outfile:
outfile.write(b.fp.getvalue()) # fp is a StringIO instance
在文件中,您将看到如下内容:
%!PS-Adobe-2.0
%%Pages: (attend)
%%Creator: Elaphe powered by barcode.ps
%%BoundingBox: 0 0 42 42
%%LanguageLevel: 2
%%EndComments
% --BEGIN RESOURCE preamble--
... A whole lot of included library ...
% --END ENCODER hibccodablockf--
gsave
0 0 moveto
1.000000 1.000000 scale
<74686973206973206d792064617461>
<>
/qrcode /uk.co.terryburton.bwipp findresource exec
grestore
showpage
如果您想查看 PIL 或枕头如何运行 Ghostscript,以便您可以在命令行中自己尝试,PIL/枕头代码的关键部分是这样的(来自site-packages/PIL/EpsImagePlugin.py
第 84 行):
# Build ghostscript command
command = ["gs",
"-q", # quiet mode
"-g%dx%d" % size, # set output geometry (pixels)
"-r%d" % (72*scale), # set input DPI (dots per inch)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % outfile, # output file
"-c", "%d %d translate" % (-bbox[0], -bbox[1]),
# adjust for image origin
"-f", infile, # input file
]
但在 Windows 上,该gs
命令将替换为可执行文件的路径。