Python uses Pygame to draw an image and save it as a picture file _python

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.