关于图像的一些易搞混点

PIL

1 from PIL import Image
2 import matplotlib.pyplot as plt
3 import numpy as np
4 %matplotlib inline
5 
6 image = Image.open('1.png')
7 plt.imshow(image)
8 print(image.size)

输出的是(宽,高)

读取的格式需要将image转换成np.array才可以:

image_array = np.array(image)
print(image.shape)
>>输出的是 (高,宽,通道数)

tensorflow

 1 import tensorflow as tf 
 2 import numpy as np 
 3 import matplotlib.pyplot as plt 
 4 image_contents = tf.read_file('1.png') #读取文件 
 5 image = tf.image.decode_png(image_contents, channels=3) #解码jpeg 
 6 with tf.Session() as sess: 
 7     sess.run(tf.global_variables_initializer()) 
 8     img=sess.run((image)) #img为三维数组 
 9     print (img.shape) #输出数组形状 
10     print (img) #打印数组 
11     plt.imshow(img) #显示数组 
12     plt.show()

输出的是(高,宽,channel)

原文地址:https://www.cnblogs.com/pacino12134/p/9778035.html