【问题标题】:How to use python to generate a magazine cover? [closed]如何使用python生成杂志封面? [关闭]
【发布时间】:2013-05-31 13:29:51
【问题描述】:

我正在寻找可以生成杂志封面的东西 一张图片,我可以在上面添加一些内容亮点。 做这项工作的最佳图书馆是什么? 是PIL吗?还是 imagemagick?

【问题讨论】:

  • 请定义“杂志封面”。
  • 请定义“生成”,就此而言...
  • 如果你只是想在图像上叠加一些文本,那么 PIL 就可以了。我对 ImageMagick 了解不多。对于更复杂的情况,最好使用适当的 DTP 包。
  • 在我的例子中,杂志封面是一张图片,上面有内容的亮点。 @Tichodroma

标签: python image imagemagick


【解决方案1】:

我很惊讶您想以编程方式设计杂志封面,而不是使用 Photoshop、Illustrator、Gimp 或 Inkscape 等 GUI。但是,假设你这样做,我认为最简单的方法是使用 Python 来构造 SVG 图像。 SVG 是基于矢量的(行位置可以在制作后修改)和人类可读的 XML,因此您可以在 Python 中自动生成图形和在 Inkscape 中手动编辑它们之间切换。 Python 有很好的 built-inthird-party 工具来操作 XML,其中 SVG 只是一个特例。

以编程方式生成图像可能会涉及大量试错,因此我建议您使用交互式查看器进行设置。这是一个使用 GTK 的简单示例(例如在 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

现在您可以使用以下命令构建图形

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 图像可以嵌入到 SVG 中,方法是使用 Base64 对它们进行编码,将它们编码为 href:data 属性。这需要很长的解释,但我只是让你知道这是可能的。此外,SVG 支持您在闪亮杂志封面上所期望的所有光泽渐变和混合效果,但您必须深入研究文档才能了解它是如何完成的。

【讨论】:

  • +1。这是一种深思熟虑的答案,需要更多的支持。
  • 非常感谢您提供如此全面的答案,这正是我正在寻找的。​​span>
猜你喜欢
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2021-04-06
  • 1970-01-01
相关资源
最近更新 更多