开发者

Using PIL to draw gradients with clipping masks?

Does PIL (Python Imaging Library) have methods to draw linear gradi开发者_开发知识库ents or/and set clipping paths?

I've looked everywhere but I can't find any examples that demonstrate how to use either. Or are there any other graphics libraries that can do similar tasks?


I'm not sure about PIL, but since you asked about alternatives as well, here's an example of both linear gradients and clipping regions using wxPython.

wxPython isn't strictly for image manipulation but it can get the job done.

import wx

def render(dc):
    dc.Clear()
    region = wx.RegionFromPoints([(256, 64), (448, 448), (64, 448)])
    dc.SetClippingRegionAsRegion(region)
    dc.GradientFillLinear((0, 0, 512, 512), wx.RED, wx.BLACK, wx.NORTH)

def main():
    app = wx.PySimpleApp()
    bitmap = wx.EmptyBitmap(512, 512)
    render(wx.MemoryDC(bitmap))
    bitmap.SaveFile('output.png', wx.BITMAP_TYPE_PNG)

if __name__ == '__main__':
    main()

Output of this program:

Using PIL to draw gradients with clipping masks?

This example uses a standard drawing context, but there's also GraphicsContext which provides additional functionality and better rendering (anti-aliasing):

http://www.wxpython.org/docs/api/wx.GraphicsContext-class.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜