python读取图片的方式,以及将图片以三维数组的形式输出方法
作者:贪狼切 时间:2023-08-05 04:09:26
近期做个小项目需要用到python读取图片,自己整理了一下两种读取图片的方式,其中一种用到了TensorFlow,(TensorFlow是基于python3 的)。代码及运行结果如下所示:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open(r'C:\Users\Administrator\Desktop\data\train\forest_001.jpg') #读取图片文件
plt.imshow(image)
plt.show() #将图片输出到屏幕
image_arr = np.array(image) #将图片以数组的形式读入变量
print (image_arr)
另一种读取图片的方式
# coding=utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
image_contents = tf.read_file(r'C:\Users\Administrator\Desktop\data\train\forest_001.jpg') #读取文件
image = tf.image.decode_jpeg(image_contents, channels=3) #解码jpeg
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
img=sess.run((image)) #img为三维数组
print (img.shape) #输出数组形状
print (img) #打印数组
plt.imshow(img) #显示数组
plt.show()
结果为:
打印图片
输出的数组部分截图
来源:https://blog.csdn.net/tanlangqie/article/details/79560296
标签:python,读取,图片,三维数组,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php反序列化之魔术方法超详细讲解
2023-11-16 07:24:59
![](https://img.aspxhome.com/file/2023/1/55621_0s.png)
Python的字符串操作简单实例
2021-03-13 07:16:00
python 变量初始化空列表的例子
2022-09-03 08:14:42
ASP中将Excel数据导入到Access
2009-02-02 09:15:00
Python lambda 匿名函数优点和局限性深度总结
2023-07-25 19:22:00
Python中的装饰器使用
2023-05-25 13:14:09
Python中的进程操作模块(multiprocess.process)
2022-09-17 23:10:32
禁止在网页里面是用搜狗的云输入法
2009-11-29 15:50:00
Frontpage轻松下载网页或站点
2007-10-22 13:14:00
![](https://img.aspxhome.com/file/UploadPic/200710/22/20071022131645701s.gif)
JS事件在IE与FF中的区别详细解析
2023-09-24 23:02:35
内容呈现的减法
2007-11-16 16:22:00
![](https://img.aspxhome.com/file/UploadPic/200711/16/2007111616240175s.gif)
asp阻止中国ip访问访问
2011-09-13 12:55:37
Web开发者的百科全书——Google DocType
2008-07-03 13:06:00
Python使用scrapy采集数据过程中放回下载过大页面的方法
2021-10-22 12:39:22
模仿PHP写的ASP分页函数
2008-04-13 06:11:00
在python中按照特定顺序访问字典的方法详解
2023-02-09 07:14:34
PHP如何从txt文件中读取数据详解
2023-11-15 02:37:32
![](https://img.aspxhome.com/file/2023/5/55725_0s.png)
Win10下安装并使用tensorflow-gpu1.8.0+python3.6全过程分析(显卡MX250+CUDA9.0+cudnn)
2021-06-29 20:05:21
![](https://img.aspxhome.com/file/2023/0/80620_0s.jpg)
玩转Python发短信的实现
2021-09-17 20:38:54
![](https://img.aspxhome.com/file/2023/1/79801_0s.jpg)
python中pass语句用法实例分析
2021-08-12 01:49:13