# -*- coding: utf-8 -*-
import tensorflow as tf
filename = '2.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image = sess.run(image_data)
print(image.shape)
Here we use the read method of tf.gfile.FastGFile
read
read(n=-1)
Returns the contents of a file as a string.Args:
n: Read ‘n’ bytes if n != -1. If n = -1, reads to end of file.
Returns:
‘n’ bytes of the file (or whole file) in bytes mode or ‘n’ bytes of the string if in string (regular) mode.
The read method returns the content of the entire file as a string by default. Our parameter here is rb
, so return it in bytes
# -*- coding: utf-8 -*-
import tensorflow as tf
filename = '2.jpg'
with tf.gfile.FastGFile(filename,'r') as f:
image_data = f.read()
with tf.Session() as sess:
image = sess.run(image_data)
Here I try to make it return a string directly, but it will report an error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
I'm also not very clear, it should be enough to return bytes without much use. Another read method is
image_data = tf.read_file(filename)
I don't think there is much difference between the two reads, tf.read_file
the one returned is one Tensor
, so sess is needed, tf.gfile.FastGFile
but the bytes or strings returned directly. Both of these methods read the raw data of the picture , so we need to decode it.
tf.image.decode_jpeg(image_data)
tf.image.decode_image(image_data)
tf.decode_raw(image_data,tf.uint8)
The first function is relatively simple, we read the picture in jpeg format, and then use it tf.image.decode_jpeg(image_data)
to decode the picture.
# -*- coding: utf-8 -*-
import tensorflow as tf
import matplotlib.pyplot as plt
filename = '2.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_data)
image = sess.run(image_data)
plt.imshow(image)
print(image.shape)
If it is a color picture, it can be output normally, but a grayscale picture will not work.
TypeError: Invalid dimensions for image data
I didn't give up and looked at the data shape
# -*- coding: utf-8 -*-
import tensorflow as tf
import matplotlib.pyplot as plt
filename = '2.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_data)
image = sess.run(image_data)
print(image.shape)
output
(400, 600, 1)
But why would it report an error? This is because plt cannot draw such a data format, the grayscale image must be (height,width)
, but the data read will get a (height,width,channels)
normal color image (height,width,3)
, the grayscale image is (height,width,1)
, in order for plt to draw it, we need reshap
it.
# -*- coding: utf-8 -*-
import tensorflow as tf
import matplotlib.pyplot as plt
filename = '2.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_data)
image = sess.run(image_data)
h,w,c=image.shape
assert c==1
image = image.reshape(h,w)
plt.imshow(image)
print(image.shape)
In this way, the picture can be drawn, but it is found to be colored, because plt has color by default, you can
plt.imshow(image,cmap='gray')
Sometimes color pictures but we want to be gray
image_data = tf.image.decode_jpeg(image_data,channels=1)
This will turn the original color image into gray
- 0: Use the number of channels in the JPEG-encoded image.
- 1: output a grayscale image.
- 3: output an RGB image.
But note that the data shape is
(height,width,3)->(height,width,1)
To display or need to reshape
# -*- coding: utf-8 -*-
import tensorflow as tf
import matplotlib.pyplot as plt
filename = '1.jpg' # 彩色
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_data,channels=1)
image = sess.run(image_data)
h,w,c=image.shape
assert c==1
image = image.reshape(h,w)
plt.imshow(image,cmap='gray')
print(image.shape)
tf.image.decode_image(image_data)
This function is tf.image.decode_jpeg
similar, mainly look at the return
Tensor with type uint8 with shape [height, width, num_channels] for BMP, JPEG, and PNG images and shape [num_frames, height, width, 3] for GIF images.
jpeg, png, bmp, and even gif can be decoded. I think it 's more convenient tf.image.decode_image
than that. The last one istf.image.decode_jpeg
tf.decode_raw(image_data,tf.uint8)
This function is a bit silly,
# -*- coding: utf-8 -*-
import tensorflow as tf
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_data = f.read()
with tf.Session() as sess:
image_data = tf.decode_raw(image_data,tf.uint8)
image = sess.run(image_data)
print(image)
print(image.shape)
output
[255 216 255 ... 127 255 217]
(47982,)
That is, I will give you what the output looks like, but you need to specify the data type of decoding. We are pictures so use tf.uint8
. My confusion starts with this function,
# -*- coding: utf-8 -*-
import tensorflow as tf
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image1 = f.read()
image2 = tf.read_file(filename)
with tf.Session() as sess:
print(len(image1))
image2 = sess.run(image2)
print(len(image2))
output
47982
47982
But what is the shape of the picture (400,600,3)
, which is 720000, which is different. I don't know how to decode the data into pictures. I don't know the encoding protocol of jpeg, so I guess tf.decode_raw
and tf.gfile.FastGFile
are tf.read_file
not paired. .