python使用PyGame绘制图像并保存为图片文件的方法
作者:feiwen 发布时间:2023-05-13 16:17:03
标签:python,PyGame,图片
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- Apache 从2.2升级到 Apache2.4.x 后配置文件 httpd.conf 的设置方法有了大变化,以前是将 deny from
- 本文实例讲述了PHP排序二叉树基本功能实现方法。分享给大家供大家参考,具体如下:这里演示了排序二叉树节点的插入,中序遍历,极值的查找和特定值
- 一、缓存目的1、减小过载2、避免重复计算3、提高系统性能二、如何进行缓存三、缓存类型四、缓存粒度分类五、缓存的设置与使用示例一:CACHES
- 1. 首先VC++的DLL的导出函数定义成标准C的导出函数: #ifdef LRDLLTEST_EXPORTS #define LRDLLT
- 我们知道现实中的数据通常是杂乱无章的,需要大量的预处理才能使用。Pandas 是应用最广泛的数据分析和处理库之一,它提供了多种对原始数据进行
- IFRAME 元素 | iframe 对象创建内嵌浮动框架。成员表下面的表格列出了 iframe 对象引出的成员。请单击左侧的标签来选择你想
- pandas代码如下:import pandas as pdimport numpy as npsalaries = pd.DataFram
- 1 栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(Las
- 一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函
- 从我们论坛中收集了这段HTML制作页面需要最大化、最小化时可以借鉴参考。最大化效果:<OBJECT id="max
- 这篇文章主要介绍了一种简单的MySQL数据库安装方法,详细内容请大家参考下文:虽然安装MySQL数据库的文章很多,但是我看后感觉对于初学者来
- 创建时间序列函数pd.date_range()根据指定的范围,生成时间序列DatetimeIndex,每隔元素的类型为Timestamp。该
- 需求细化:1.身份证必须能够通过身份证校验程序。2.通过查询,发现身份证号码是有国家标准的,标准号为 GB 11643-1999 可以从百度
- 这将为我们的团队节省每天重复的数据处理时间......简介如果你目前在一个数据或商业智能团队工作,你的任务之一可能是制作一些每日、每周或每月
- Expires 属性 Expires 属性指定了在浏览器上缓冲存储的页距过期还有多少时间。如果用户在某个页过期之前又回到此页,就会显示缓冲区
- Flask框架介绍Flask诞生于2010年,是Armin ronacher用Python语言基于Werkzeug工具箱编写的轻量级Web开
- 系统管理员通常从svn/git中检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比
- 在 Python 中,* 和 ** 具有语法多义性,具体来说是有四类用法。1. 算数运算* 代表乘法** 代表乘方>>>
- 这里我们通过请求网页例子来一步步理解爬虫性能当我们有一个列表存放了一些url需要我们获取相关数据,我们首先想到的是循环简单的循环串行这一种方
- 在HTML中,常见的URL有多种表示方式:相对URL: example.php demo