欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python读取图片的方式,以及将图片以三维数组的形式输出方法

时间:2021-05-19 09:40:24|栏目:Python代码|点击:

近期做个小项目需要用到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()

结果为:

打印图片

输出的数组部分截图

上一篇:Python实现识别手写数字 Python图片读入与处理

栏    目:Python代码

下一篇:pandas read_excel()和to_excel()函数解析

本文标题:python读取图片的方式,以及将图片以三维数组的形式输出方法

本文地址:http://www.codeinn.net/misctech/124407.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有