python使用PyGame绘制图像并保存为图片文件的方法
本文实例讲述了python使用PyGame绘制图像并保存为图片文件的方法。分享给大家供大家参考。具体实现方法如下:
'''pg_draw_circle_save101.py drawabluesolidcircleonawhitebackground savethedrawingtoanimagefile forresultseehttp://prntscr.com/156wxi testedwithPython2.7andPyGame1.9.2byvegaseat16may2013 ''' importpygameaspg #pygameuses(r,g,b)colortuples white=(255,255,255) blue=(0,0,255) width=300 height=300 #createthedisplaywindow win=pg.display.set_mode((width,height)) #optionaltitlebarcaption pg.display.set_caption("Pygamedrawcircleandsave") #defaultbackgroundisblack,somakeitwhite win.fill(white) #drawabluecircle #centercoordinates(x,y) center=(width//2,height//2) radius=min(center) #widthof0(default)fillsthecircle #otherwiseitisthicknessofoutline width=0 #draw.circle(Surface,color,pos,radius,width) pg.draw.circle(win,blue,center,radius,width) #nowsavethedrawing #cansaveas.bmp.tga.pngor.jpg fname="circle_blue.png" pg.image.save(win,fname) print("file{}hasbeensaved".format(fname)) #updatethedisplaywindowtoshowthedrawing pg.display.flip() #eventloopandexitconditions #(pressescapekeyorclickwindowtitlebarxtoexit) whileTrue: foreventinpg.event.get(): ifevent.type==pg.QUIT: #mostreliableexitonxclick pg.quit() raiseSystemExit elifevent.type==pg.KEYDOWN: #optionalexitwithescapekey ifevent.key==pg.K_ESCAPE: pg.quit() raiseSystemExit
希望本文所述对大家的Python程序设计有所帮助。