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()

结果为:

打印图片
这里写图片描述

输出的数组部分截图
这里写图片描述

  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值