Your data looks like the raw bytes from a real image file (JPG?). The problem with your data is that it should be bytes, not unicode. You have to figure out how to convert from unicode to bytes. There is a whole can of worms full of encoding traps you have to deal with, but you may be lucky using img.encode('iso-8859-1')
. I don't know and I will not deal with that in my answer.
The raw data for a PNG image looks like this:
rawdata = 'x89PNG
x1a
x00x00...x00x00IENDxaeB`x82'
Once you have it in bytes, you can create a PIL image from the raw data, and read it as a nparray:
>>> from StringIO import StringIO
>>> from PIL import Image
>>> import numpy as np
>>> np.asarray(Image.open(StringIO(rawdata)))
array([[[255, 255, 255, 0],
[255, 255, 255, 0],
[255, 255, 255, 0],
...,
[255, 255, 255, 0],
[255, 255, 255, 0],
[255, 255, 255, 0]]], dtype=uint8)
All you need to make it work on Spark is SparkContext.binaryFiles
:
>>> images = sc.binaryFiles("path/to/images/")
>>> image_to_array = lambda rawdata: np.asarray(Image.open(StringIO(rawdata)))
>>> images.values().map(image_to_array)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…