How to use python to generate a magazine cover? [closed]

心已入冬 提交于 2019-12-25 18:23:46

问题


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?


回答1:


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.

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

and save them with

open("outputFile.svg", "w").write(etree.tostring(document))

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.



来源:https://stackoverflow.com/questions/16858482/how-to-use-python-to-generate-a-magazine-cover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!