python使用PyGame绘制图像并保存为图片文件的方法


Posted in Python onApril 24, 2015

本文实例讲述了python使用PyGame绘制图像并保存为图片文件的方法。分享给大家供大家参考。具体实现方法如下:

''' pg_draw_circle_save101.py
draw a blue solid circle on a white background
save the drawing to an image file
for result see http://prntscr.com/156wxi
tested with Python 2.7 and PyGame 1.9.2 by vegaseat 16may2013
'''
import pygame as pg
# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)
width = 300
height = 300
# create the display window
win = pg.display.set_mode((width, height))
# optional title bar caption
pg.display.set_caption("Pygame draw circle and 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 of outline
width = 0
# draw.circle(Surface, color, pos, radius, width)
pg.draw.circle(win, blue, center, radius, width)
# now 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

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
基于Python 的进程管理工具supervisor使用指南
Sep 18 Python
Python将DataFrame的某一列作为index的方法
Apr 08 Python
python破解zip加密文件的方法
May 31 Python
Django 视图层(view)的使用
Nov 09 Python
Python合并同一个文件夹下所有PDF文件的方法
Mar 11 Python
python 计算平均平方误差(MSE)的实例
Jun 29 Python
pycharm新建一个python工程步骤
Jul 16 Python
Python 实现数据结构-堆栈和队列的操作方法
Jul 17 Python
pytorch .detach() .detach_() 和 .data用于切断反向传播的实现
Dec 27 Python
python实现爱奇艺登陆密码RSA加密的方法示例详解
May 27 Python
Python基础之条件语句详解
Jun 16 Python
Python 快速验证代理IP是否有效的方法实现
Jul 15 Python
python使用PIL缩放网络图片并保存的方法
Apr 24 #Python
python使用Tkinter显示网络图片的方法
Apr 24 #Python
Python中最常用的操作列表的几种方法归纳
Apr 24 #Python
在Python中使用lambda高效操作列表的教程
Apr 24 #Python
使用Python的判断语句模拟三目运算
Apr 24 #Python
Python的字典和列表的使用中一些需要注意的地方
Apr 24 #Python
整理Python最基本的操作字典的方法
Apr 24 #Python
You might like
用PHP实现的随机广告显示代码
2007/06/14 PHP
基于PHPExcel的常用方法总结
2013/06/13 PHP
用PHP来计算某个目录大小的方法
2014/04/01 PHP
解决PHP里大量数据循环时内存耗尽的方法
2015/10/10 PHP
PHP 中使用ajax时一些常见错误总结整理
2017/02/27 PHP
常见的5个PHP编码小陋习以及优化实例讲解
2021/02/27 PHP
Nigma vs Liquid BO3 第二场2.13
2021/03/10 DOTA
google 搜索框添加关键字实现代码
2010/04/24 Javascript
JS定时器实例
2013/04/17 Javascript
JS方法调用括号的问题探讨
2014/01/24 Javascript
jQuery实现的在线答题功能
2015/04/12 Javascript
即将发布的jQuery 3 有哪些新特性
2016/04/14 Javascript
浅谈js图片前端预览之filereader和window.URL.createObjectURL
2016/06/30 Javascript
jQuery+CSS实现简单切换菜单示例
2016/07/27 Javascript
Vue.js使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用
2017/05/10 Javascript
详解Angular 中 ngOnInit 和 constructor 使用场景
2017/06/22 Javascript
详解如何用webpack打包一个网站应用项目
2017/07/12 Javascript
基于JavaScript表单脚本(详解)
2017/10/18 Javascript
一步步教会你微信小程序的登录鉴权
2018/04/09 Javascript
详解js静态检查工具eslint配置文件
2018/11/23 Javascript
微信小程序textarea层级过高(盖住其他元素)问题的解决办法
2019/03/04 Javascript
CKeditor4 字体颜色功能配置方法教程
2019/06/26 Javascript
在vant 中使用cell组件 定义图标该图片和位置操作
2020/11/02 Javascript
用生成器来改写直接返回列表的函数方法
2017/05/25 Python
http请求 request失败自动重新尝试代码示例
2018/01/25 Python
python爬虫实现中英翻译词典
2019/06/25 Python
解决Django layui {{}}冲突的问题
2019/08/29 Python
Spring Boot中使用IntelliJ IDEA插件EasyCode一键生成代码详细方法
2020/03/20 Python
Python如何设置指定窗口为前台活动窗口
2020/08/12 Python
仿酷狗html5手机音乐播放器主要部分代码
2013/05/15 HTML / CSS
Engel & Bengel官网:婴儿推车、儿童房家具和婴儿设备
2019/12/28 全球购物
卫生系统先进事迹
2014/05/13 职场文书
建筑安全责任书范本
2014/07/24 职场文书
个人年终总结范文
2015/03/09 职场文书
法律进社区活动总结
2015/05/07 职场文书
毕业班工作总结
2015/08/10 职场文书