1

I am trying to read image data into Python as a matrix.

To this extent, I am trying to use scipy.misc.imread('image.jpg').astype(np.float).

When I execute the proper sequence of steps from a python3 interpreter, everything works swimmingly, and I get a matrix as expected.

When I invoke the command from a script (python3 foo.py...), however, I get an error complaining that the argument to convert via float cannot be of type JpegImageFile. I've ensured that I've pip install -U pillow to ensure that PIL is available.

What gives? How could this be possible? I've verified over and over that the same lines of code are executed in each case, the only difference seems to be that the invocation inside of a script happens inside of a defined function, but even if I pdb.set_trace() from elsewhere in the script the same results happen.

What could be causing fluctuation in results from the interpreter to the script?

EDIT: OK, to be precise, I am running the neural_style.py script from here: https://github.com/anishathalye/neural-style . scipy, numpy, tensorflow, and pillow must be installed. I am using python3. Any parameters for --content, --styles, and --output should work to dupe the bug.

Full error traceback:

Traceback (most recent call last):
  File "neural_style.py", line 150, in <module>
    main()
  File "neural_style.py", line 84, in main
    content_image = imread(options.content)
  File "neural_style.py", line 141, in imread
    return scipy.misc.imread(path).astype(np.float)
TypeError: float() argument must be a string or a number, not 'JpegImageFile'

But, a small simple script such as the following actually works:

import numpy as np
import scipy.misc
print(scipy.misc.imread('content').astype(np.float))
3
  • Your question lacks a minimal reproducible example and doesn't even have a full error traceback, so it's going to be hard to track down the problem.
    – DSM
    Commented Mar 16, 2016 at 4:33
  • Added more information. Commented Mar 16, 2016 at 5:01
  • Still not a reproducible, self-contained example. I downloaded the 549MB (!) support file needed to run neural_style.py, ran it in both python 2.7 and 3.4, and it worked just fine. Either give a specific MCVE, or put add some prints to neural_style.py to find out what it thinks all the components of scipy.misc.imread(path).astype(np.float) are at the time, or both.
    – DSM
    Commented Mar 16, 2016 at 15:21

1 Answer 1

1

I figured out the solution to this. The neural_style.py script seems to be getting a different version of scipy module (or submodule) due to a side effect of importing stylize.py and, in turn, vgg.py. Adding this line:

import scipy.misc

To the very top of stylize.py (in front of import vgg) fixes it.

I'm really not sure why, though.

Not the answer you're looking for? Browse other questions tagged or ask your own question.