IOError: broken data stream when reading image file

created at 06-25-2021 views: 6

There is a strange mistake when running the code today. If this is not this error, it is estimated that you may want to think about half a day, because the wrong code is a few lines, come back to return, I have changed many times, I want to think about the problem. The essential.

We can easily know that this is an error that occurs when reading an image, but the program of this reading I have just run is no problem. There is no problem with the code run as shown below.

<span style="font-size:12px;">

import matplotlib.imageas mpimg

filename ="MarshOrchid.jpg"

image = mpimg.imread(filename)
</span>

There is no problem in running this, why problems appear when switching to Tensorflow? Here is the problem code:

<span style="font-size:12px;">import tensorflowas tf

import matplotlib.imageas mpimg

import matplotlib.pyplotas plt



#load image

filename ="MarshOrchid.jpg"

image = mpimg.imread(filename)



# create tensorflow variable

x = tf.Variable(image,name='x')



model = tf.initialize_all_variables()
</span>

Come back and forth, I was afraid that I was spelling the wrong copy paste several times, the characters were spelling it again, and the online check is that the JPG package is not installed, but this obvious is not established here. Because it has just been loaded successfully. After all, there are not many misunderstandings for Tensorflow, I have to do it. I found it, I found that IMREAD will have an error when importing the Tensorflow library, as long as Tensorflow is removed, it will not prompt. OK, now wrong position, is the problem of Tensorflow. But we can't delete the Tensorflow, it is not that it is inverted. We now want to use Tensorflow to solve the problem, and you will have nothing.

go back to the problem, found to be an error caused by the phrase of the import tensorflow as tf. It can not be removed, and the instinct of engineering students is - the continuous trial solution. So at the time, the attitude of trying to transfer the position of the import tensorflow as tf  by the first position, as shown below:

<span style="font-size:12px;">import matplotlib.imageas mpimg

import matplotlib.pyplotas plt

import tensorflowas tf
</span>

After completing, you will not report that error. But this is curious, in order to explain my curios, I stick all my code all, I know, please refer to advice.

<span style="font-size:12px;">import matplotlib.imageas mpimg

import matplotlib.pyplotas plt

import tensorflowas tf



#加载图像

filename ="MarshOrchid.jpg"

image = mpimg.imread(filename)



#创建tensorflow变量

x = tf.Variable(image,name='x')



model = tf.initialize_all_variables()



with tf.Session()as session:

    x = tf.transpose(x, perm=[1,0,2])

    session.run(model)

    result = session.run(x)



plt.imshow(image)

plt.show()
</span>

According to the resolution logic of the wrong, it is arranged in accordance with the runtime properties, then the runtime order of the program is: image library ---> tensorflow library ----> pyplot library, then I adjusted its Import In front of Pyplot in front of Pyplot, Tensorflow will be reported. It doesn't matter behind it. The only thing I can make you think is that image and pyplot belong to a family, all of which are matplotlib. This problem is to be answered after the big cattle or I will go deep into the answer.

In the point of view, Import is actually equivalent to the header file in the C language, which is affected by C, and intuitive in writing python also believes that the library imported in the import is no order. This is the foundation of this problem. In Python, it is currently that the library imported by its import is in order.

created at:06-25-2021
edited at: 06-25-2021: