网络编程
位置:首页>> 网络编程>> Python编程>> python读取图片的方式,以及将图片以三维数组的形式输出方法

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

作者:贪狼切  发布时间:2023-08-05 04:09:26 

标签: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读取图片的方式,以及将图片以三维数组的形式输出方法

来源:https://blog.csdn.net/tanlangqie/article/details/79560296

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com