Why can tf.image.decode_jpeg decode a png?

Viewed 6074

Today I was testing some tensorflow (python) codes. It's a neural network on the famous MNIST set.

Everything worked well, so I just read through the codes and studied the structure of that network.

When it came to the image input, I found the following code:

image_string = tf.read_file(filename)

image_decoded = tf.image.decode_jpeg(image_string, channels=3)

The code is using "decode_jpeg" instead of "decode_png". And I didn't see any error.

However, I am 100% sure that the images are in PNG format.

I have used

od -c -b 1.png

to look into those images and they are PNGs.

So, why could "decode_jpeg" work on PNGs ? And is there any potential issue that might be caused by this?

2 Answers

The documentation for tensorflow states:

This op also supports decoding PNGs and non-animated GIFs since the interface is the same, though it is cleaner to use tf.image.decode_image.

So essentially it was a design decision that because the interface is the same to just handle it, rather than throw an error that forces user to use the correct API call.

img = tf.image.decode_png(file_bytes, channels=3) 

you can use decode_png for png files

Related