1

There are several examples of loading an image file into a numpy array, but I do not have an image in a file on the local file system. Instead, I have a string variable img that contains the data that makes up an image. For example:

# Currently here:
with open("imageFile.png") as f:
    img = f.read()

# Would like to accomplish this:
npArray = loadStringToArray(img)

Writing this file back to the disk and calling one of the more common load functions seems impractical, so how do I "load" this image-as-a-string into a numpy array?

A bit of background - in my application, this image-as-a-string is delivered from a client via python sockets. The image isn't on the local machine; the example above is just that - an example. While writing the file to the local file system and then reading it back in is possible, I'd like to avoid doing that if I can.

Community
  • 1
  • 1
ADS103
  • 85
  • 1
  • 8
  • 1
    Maybe `io.StringIO` or (probably) `io.BytesIO` may help? – Paul Panzer Jan 24 '18 at 15:44
  • Paul, your comment and the documentation for the Python Imaging Library helped point me in the right direction. After placing the data into a StringIO object, PIL was able to import it, and numpy was able to generate an array out of that PIL object. I'd like to encourage you to make an answer, rather than a comment, so I can select it. – ADS103 Jan 24 '18 at 18:38
  • Done. Feel free to edit if you would like more detail. – Paul Panzer Jan 24 '18 at 18:57

2 Answers2

0

I think what you want to do is to convert the string into a numpy array as you already have the image as a string.

You can use numpy.fromstring() for that task.

Examples:-

np.fromstring('\x01\x02', dtype=np.uint8) -----> array([1, 2], dtype=uint8)

np.fromstring('1 2', dtype=int, sep=' ') -----> array([1, 2])

np.fromstring('1, 2', dtype=int, sep=',') -----> array([1, 2])

np.fromstring('\x01\x02\x03\x04\x05', dtype=np.uint8, count=3) -----> array([1, 2, 3], dtype=uint8)

  • I think you're on the right track. The issue is, these results produce 1d arrays. I'm expecting to see at least a 2d array (length*height) for grayscale images and a 3d array (length*height*channel) for color ones. I'm starting to realize that there may be more to this than I initially realized. Formats need to be converted and whatnot. – ADS103 Jan 24 '18 at 18:18
  • That is correct. If your image is getting stored as a string, then it is getting stored in 1D. That can happen by concatenating the rows next to each other etc. You need to see how that encoding is being done. If it is being done in the trivial way I just gave, the 1D numpy array can very easily be converted to a 2D array once we know the row size. – Shobhit Bhatnagar Jan 25 '18 at 18:40
0

Without going into too much detail the io module provides "file-like" objects, in particular StringIO and BytesIO that equip in-memory objects such as strings with a file-like interface. Many functions that expect a file argument will happily accept a StringIO or BytesIO instead.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • 1
    This lead me in the right direction. When reading the documentation for PIL, I discovered that PIL.Image could accept either file names as strings or file-like objects. My solution is to load my string into a StringIO, and pass that object to PIL. Converting a PIL image to a numpy array is as simple as calling im.getdata() as seen here: [link] (https://stackoverflow.com/questions/384759/how-to-convert-a-pil-image-into-a-numpy-array) – ADS103 Jan 24 '18 at 19:11