1. 圖像顯示、解碼、寫入
image_data = tf.image.decode_jpeg(image_raw_data) 解碼原始圖像,解碼輸出是tensor
image_data.eval() 對解碼後tensor轉換爲ndarray, 顯示和寫入圖像時均需要轉換爲ndarray
#--coding--:utf-8
import tensorflow as tf
import matplotlib.pyplot as plt
# 讀取圖像的原始數據
image_raw_data = tf.gfile.FastGFile('./0302test/1.jpg', 'rb').read()
with tf.Session() as sess:
# 將原始數據解碼成多維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval()) #decode輸出是Tensor,eval後是ndarray
plt.imshow(img_data.eval())
plt.show() #顯示
# 將圖像的矩陣編碼成圖像並存入文件
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('./0302test/output.jpg', 'wb') as f:
f.write(encoded_image.eval())
2. 對圖像數據處理,方便修改
# 將圖像數據的類型轉爲實數類型,便於對圖像進行處理
#歸一化,轉換爲浮點數據
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
print(img_data.eval())