This example describes how Python uses Pygame to draw an image and save it as a picture file. Share to everyone for your reference. The implementation method is as follows:
"' Pg_draw_circle_save101.pydraw a blue solid circle on a white backgroundsave the drawing to an image filefor result see Http://prntscr.com/156wxitested with Python 2.7 and PyGame 1.9.2 by vegaseat 16may2013 "' Import PyGame as pg# PyGame uses (R, G, b) color tupleswhite = (255, 255, 255) Blue = (0, 0, 255) width = 300height = 300# Create the display Windowwin = PG . Display.set_mode (width, height) # optional title bar captionpg.display.set_caption ("Pygame Draw Circle and save") # Default background is black, so make it Whitewin.fill (white) # Draw a Blue circle# center coordinates (x, y) center = (width 2, HEIGHT//2) radius = min (center) # Width of 0 (default) fills the circle# otherwise it is thickness of outlinewidth = 0# Draw.circle (Surface, color, pos, radius, width) pg.draw.circle (win, Blue, center, RADIUS, width) # Now save the drawing# CA n Save as BMP. TGA. png or. jpgfname = "Circle_blue.png" Pg.image.save (Win, fname) print ("file {} has been saved". Format (fn AME) # Update the display winDow to show The Drawingpg.display.flip () # event loop and Exit conditions# (press Escape key or click Window title bar X to Exit) While True:for event in Pg.event.get (): if Event.type = = pg. QUIT: # Most reliable exit on X click Pg.quit () Raise systemexit elif Event.type = pg. KEYDOWN: # Optional exit with Escape key if Event.key = = pg. K_ESCAPE:pg.quit () Raise Systemexit
Hopefully this article will help you with Python programming.