arc_lupus
arc_lupus

Reputation: 4114

How to get a picture out of raw hex data in python?

I am using pydicom for extracting image data out of a dicom file. Unfortunately pydicom fails to directly extract a numpy array of data I can directly use, but I get a data string containing all values in hex (i.e. f.eks. \x03\x80\x01\x0c\xa0\x00\x02P\x00\x04@\x00\t\x80\x00\x03.... I know that the image data is encoded in a JPEG2000-format. Is there a way to reconstruct an image out of these data? I already tried via

img = Image.fromstring('RGB', len(pixelData), pixelData)

but there I get the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2064, in fromstring
    return frombytes(*args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2049, in frombytes
    im = new(mode, size)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2015, in new
    return Image()._new(core.fill(mode, size, color))
TypeError: must be 2-item sequence, not int

Is there another way to create an image out of these data?

Upvotes: 0

Views: 574

Answers (2)

DrBwts
DrBwts

Reputation: 3657

Unfortunately pydiacom has issues with JPEG compression. Is there no way of making the images TIFF or some other uncompressed format? Is it scan data?

Upvotes: 0

ndpu
ndpu

Reputation: 22571

Second parameter (size) to Image.fromstring should be 2-tuple with height and width:

  :param size: A 2-tuple, containing (width, height) in pixels.

Upvotes: 1

Related Questions