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程序设计有所帮助。
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西