This article describes the way Python uses Pygame to draw images and save them as picture files. Share to everyone for your reference. The implementation method is as follows:
"" pg_draw_circle_save101.py draw a blue solid circle on a white background save the drawing to a image file for result Http://prntscr.com/156wxi tested with Python 2.7 and Pygame 1.9.2 by vegaseat 16may2013 ' ' Import Pygame as PG # PYG Ame uses (R, G, b) color tuples white = (255, 255, 255) Blue = (0, 0, 255) width = + Height = # Create the display W Indow win = Pg.display.set_mode ((width, height)) # Optional title bar caption Pg.display.set_caption ("Pygame Draw Circle A
nd save ") # Default background is black, so make it white Win.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 o F Outline width = 0 # draw.circle (Surface, color, pos, radius, width) pg.draw.circle (win, Blue, center, RADIUS, width) # n ow save the drawing # can save As. bmp. TGA. png or. jpg fname = "circle_blue.png" Pg.image.save (Win, fname) print ("File { } has been saved ". Format(fname)) # Update the display window to show the Drawing Pg.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
I hope this article will help you with your Python programming.