[출처 : http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=10224]


# create a wx.MemoryDC canvas on top of a blank bitmap
# you can then save the canvas drawings to a standard image file
# source: Dietrich   25nov2008  

import wx
import random

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        self.show_bmp = wx.StaticBitmap(self)
        self.draw_image()
        self.save_image()

    def draw_image(self):
        # get the width and height for the blank bitmap
        w, h = self.GetClientSize()
        # create the blank bitmap as a drawing background
        draw_bmp = wx.EmptyBitmap(w, h)
        # create the canvas on top of the draw_bmp
        canvas = wx.MemoryDC(draw_bmp)
        # fill the canvas white
        #canvas.SetBrush(wx.Brush('white'))
        #canvas.Clear()
        
        # pen colour, default is black
        #canvas.SetPen(wx.Pen('red', 1))
        
        # draw a bunch of random circles ...
        bunch = 100
        for k in range(bunch):
            # fill colour
            r = random.randint(0, 255)
            g = random.randint(0, 255)
            b = random.randint(0, 255)
            colour = wx.Colour(r, g, b)
            canvas.SetBrush(wx.Brush(colour))
            x = random.randint(0, w)
            y = random.randint(0, h)
            r = random.randint(10, w/4)
            canvas.DrawCircle(x, y, r)

        # put the canvas drawing into the static bitmap to display it
        # (remember the canvas is on top of a draw_bmp empty bitmap)
        self.show_bmp.SetBitmap(draw_bmp)

    def save_image(self):
        """save the drawing"""
        # get the image object
        circles_image = self.show_bmp.GetBitmap()
        circles_image.SaveFile("mycircles.jpg", wx.BITMAP_TYPE_JPEG)


app = wx.App(0)
mytitle = 'draw random circles and save image'
width = 420
height = 400
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()


Circles1.jpg

+ Recent posts