How Python uses Pygame to draw images and save them as picture files

Source: Internet
Author: User
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.

  • 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.