有关python的杂志_如何使用python生成杂志封面?

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?

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值