如何使用python生成杂志封面? [英] How to use python to generate a magazine cover?
问题描述
我正在寻找可以用
图像生成杂志封面的东西,我可以添加一些内容亮点。
做这份工作的最佳图书馆是什么?
这是PIL吗?或者imagemagick?
I'm looking for something that can generate a magazine cover with a image and I can add some content highlights on it. What's the best library to do this job? Is it PIL? or imagemagick?
推荐答案
我很惊讶您想以编程方式设计杂志封面,而不是像Photoshop这样的GUI, Illustrator,Gimp或Inkscape。但是,假设你这样做,我认为最简单的方法是使用Python来构建SVG图像。 SVG 是基于矢量的(行位置可以在制作后进行修改)和人类可读的XML,因此您可以在Python中自动生成图形之间切换,并在 Inkscape 中手动编辑它们。 Python有很好的内置和第三方工具,SVG只是一种特殊情况。
I'm surprised that you want to design a magazine cover programmatically, rather than with a GUI like Photoshop, Illustrator, Gimp, or Inkscape. But, assuming that you do, I think the easiest way would be to use Python to construct an SVG image. SVG is vector based (line positions can be modified after they have been made) and human-readable XML, so you can alternate between auto-generating graphics in Python and editing them by hand in Inkscape. Python has good built-in and third-party tools for manipulating XML, for which SVG is just a special case.
以编程方式生成图像可能涉及很多试错,所以我建议你自己设置一个交互式查看器。这是一个使用GTK的简单例子(例如在Ubuntu中, apt-get install python-rsvg python-cairo
):
Generating an image programmatically will probably involve a lot of trial-and-error, so I recommend setting yourself up with an interactive viewer. Here's a simple one using GTK (e.g. in Ubuntu, apt-get install python-rsvg python-cairo
):
import cairo
import rsvg
import gtk
class Viewer(object):
def __init__(self):
self.string = """<svg width="800" height="600"></svg>"""
self.svg = rsvg.Handle(data=self.string)
self.win = gtk.Window()
self.da = gtk.DrawingArea()
self.win.add(self.da)
self.da.set_size_request(800, 600)
self.da.connect("expose-event", self._expose_cairo)
self.win.connect("destroy", self._destroy)
self.win.show_all()
self.win.present()
def _expose_cairo(self, win, event):
self.svg = rsvg.Handle(data=self.string)
cr = self.da.window.cairo_create()
self.svg.render_cairo(cr)
def _destroy(self, widget, data=None):
gtk.main_quit()
def renderSVG(self, text):
x, y, w, h = self.win.allocation
self.da.window.invalidate_rect((0,0,w,h), False)
self.string = text
现在你可以建立图形了命令如
Now you can build up graphics with commands like
viewer = Viewer() # pops up a window
import lxml.etree as etree
from lxml.builder import E as svg
rectangle = svg.rect(x="10", y="10", width="80", height="80", style="fill: yellow; stroke: black; stroke-width: 2;")
circle = svg.circle(cx="70", cy="70", r="30", style="fill: red; fill-opacity: 0.5; stroke: black; stroke-width: 2;")
document = svg.svg(rectangle, circle, width="100", height="100")
viewer.renderSVG(etree.tostring(document)) # draws the image in the window
并保存它们
open("outputFile.svg", "w").write(etree.tostring(document))
位图PNG图像可以通过在 href:data $中编码嵌入SVG中c $ c>使用Base64编码它们的属性。这需要很长的解释,但我只是让你知道它是可能的。此外,SVG支持你期望的所有光泽渐变和混合效果杂志封面,但你必须深入了解文档,看看它是如何完成的。
Bitmapped PNG images can be embedded in SVG by encoding them in an href:data
attribute by encoding them with Base64. That would require a long explanation, but I'm just letting you know that it's possible. Also, SVG supports all of the glossy gradients and blending effects that you'd expect on a shiny magazine cover, but you'll have to dig into the documentation to see how it's done.
这篇关于如何使用python生成杂志封面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!