Python怎么输出图片且不保存
python怎么输出图片且不保存的方法:
一、输出本地图片
使用open()函数来打开图片,使用show()函数来显示图片。
fromPILimportImage
img=Image.open('d:/dog.png')
img.show()
这种图片显示方式是调用操作系统自带的图片浏览器来打开图片,有些时候这种方式不太方便,因此我们也可以使用另上一种方式,让程序来绘制图片。
fromPILimportImage
importmatplotlib.pyplotasplt
img=Image.open('d:/dog.png')
plt.figure("dog")
plt.imshow(img)
plt.show()
这种方法虽然复杂了些,但推荐使用这种方法,它使用一个matplotlib的库来绘制图片进行显示的。
二、输出网络图片
下面的代码可以实现从网络读取一张图片,不需要保存为本地文件,直接通过Image模块对图片进行处理,这里使用到了cStringIO库,主要是把从网络读取到的图片数据模拟成本地文件。
importurllib2
importImage
importcStringIO
defImageScale(url,size):
file=cStringIO.StringIO(urllib2.urlopen(url).read())
img=Image.open(file)
img.show()
附:若想保存图片,调用save即可。
img.save('d:/dog.jpg')
以上内容为大家介绍了Python怎么输出图片且不保存,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
为什么Python爬虫使用MongoDB?
想必大家都知道,在从事网络爬虫工作之前,我们需要掌握很多知识,除了基本的爬虫原理之外,我们还需要学习框架、数据库等,其中就包含MongoDB...详情>>
2023-11-07 23:02:10
python如何进行类型转换的
python提供了将变量或值从一种类型转换成另一种类型的内置函数。1、int:将符合数学格式数字型字符串转换成整数。>>>int('123')1232、s...详情>>
2023-11-07 18:51:20
python怎么生成随机数
以下实例演示了python如何生成一个随机数:#生成0~9之间的随机数#导入random(随机数)模块importrandomprint(random.randint(0,9))执行以上代码...详情>>
2023-11-07 17:09:17
python如何删除空格
python中的字符串方法提供了如下常用的方法来删除空白:strip():删除字符串前后的空白。lstrip():删除字符串前面(左边)的空白。rstrip():删...详情>>
2023-11-07 14:57:44热门推荐
技术干货






