Re: Issue 16 in pydicom: Provide PixelData decompression (JPEG, RLE, MPEG, etc)

59 views
Skip to first unread message

pyd...@googlecode.com

unread,
Jan 5, 2010, 10:47:54 AM1/5/10
to pydic...@googlegroups.com

Comment #6 on issue 16 by NikitaTheSpider: Provide PixelData decompression
(JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Just a suggestion -- libjpeg is widely installed in Unix/Linux land and is
available
for Windows (although it's not installed by default). Why not make pydicom
JPG
decompression conditional on libjpeg?

The same goes for PNG/libpng.


--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

pyd...@googlecode.com

unread,
Jan 5, 2010, 7:28:36 PM1/5/10
to pydic...@googlegroups.com

Comment #7 on issue 16 by darcymason: Provide PixelData decompression

> Just a suggestion -- libjpeg is widely installed in Unix/Linux land and
> is available
> for Windows (although it's not installed by default). Why not make
> pydicom JPG
> decompression conditional on libjpeg?

I'd be happy to use it if it would work. I did look at this a long time ago
(see
comment from Oct 15, 2008), and only got it to work for 8-bit lossy images
which
isn't very complete given that 12 or 16 bit images are common, and there
are many
lossless techniques. It's a long time ago that I looked at this, but IIRC
there were
some newsgroup discussions about many issues that other libraries such as
dcmtk
and PVRG have had to deal with. I believe dcmtk started with libjpeg but
had to
modify it and add in other codes for the various JPEG flavours allowed by
DICOM. It
may make more sense to adopt their C code if any.

pyd...@googlecode.com

unread,
Sep 12, 2010, 10:03:10 PM9/12/10
to pydic...@googlegroups.com

Comment #8 on issue 16 by mikepan: Provide PixelData decompression (JPEG,

For JPEG, how about the Jasper library? I believe that OsiriX (Mac OS X
DICOM viewer) uses it, and it's licensed under the MIT license.
http://www.ece.uvic.ca/~mdadams/jasper/

pyd...@googlecode.com

unread,
Sep 12, 2010, 10:25:53 PM9/12/10
to pydic...@googlegroups.com

Comment #9 on issue 16 by mikepan: Provide PixelData decompression (JPEG,

OsiriX also uses OpenJPEG, which is BSD licensed. Not sure why the need
for both.
http://www.openjpeg.org/

pyd...@googlecode.com

unread,
Mar 6, 2014, 5:54:31 PM3/6/14
to pydic...@googlegroups.com

Comment #10 on issue 16 by neuros...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Any updates on this? I'm currently using pydicom mixed with GDCM, looking
to help incorporate a purely python way to decompress JPG images within
DICOM.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

pyd...@googlecode.com

unread,
Mar 7, 2014, 6:36:28 PM3/7/14
to pydic...@googlegroups.com

Comment #11 on issue 16 by darcy...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

No sorry, no update. I wonder, though ... IIRC I looked at GDCM once (or
was it dcmtk?) and i remember seeing that they handled compressed images by
calling a command line routine to convert to non-compressed in a temporary
file, and then load that.

It would be fairly easy to add a "hook" in pydicom to call something like
dcmtk to convert the file and then load it in. Would that be helpful?

And ... I'd be interested to know which specific compressed files (transfer
syntax) people are encountering in the real world.

pyd...@googlecode.com

unread,
Mar 7, 2014, 6:40:09 PM3/7/14
to pydic...@googlegroups.com

Comment #12 on issue 16 by ezaro...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

I've used dcmtk in conjunction with pydicom. I frequently run into JPEG-LS
and JPEG2000 variants. A downside to dmctk is that there is no free module
for encoding/decoding JPEG2000. That being said, OsiriX
(https://github.com/pixmeo/osirix) uses dcmtk and they have their own way
of handling JPEG2000 which you can look at in the source.

pyd...@googlecode.com

unread,
Mar 7, 2014, 7:32:15 PM3/7/14
to pydic...@googlegroups.com

Comment #13 on issue 16 by darcy...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

I'll try to have a look at OsiriX and see if there might be a way to build
a C extension. If so, I could see that as a separate pydicom library (to
keep the core pydicom pure python).

Just by way of explanation for anyone stumbling on this thread...my take on
this issue (as in issue 16) is that it seems like a large amount of work to
try to add decompression into pydicom (or related package), which has
always had the philosophy of being light and very easy to install. This is
why I've avoided it and recommend people pre-process compressed files with
dcmtk or others.

To me, if the pydicom user has to go through the work of a complex
installation with multiple dependencies, compiling C extensions, etc., then
they might as well do so with one of the existing packages that already
have that all worked out.

If there were a way to provide decompression in pure python or with minimal
C, then I think that would be more in the spirit of pydicom. My hope was to
perhaps handle a few of the most common compression formats that way.

I welcome anyone's thoughts on this subject.

Having said all that, on a more positive note: the various comments in this
issue do seem to be converging on OsiriX as a good source to model, if
anyone had the ambition to try to take this on. Realistically, it's not
something I can see getting around to anytime soon.

pyd...@googlecode.com

unread,
Mar 9, 2014, 10:49:05 AM3/9/14
to pydic...@googlegroups.com

Comment #14 on issue 16 by neuros...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Darcy, I'm using pydicom and GDCM for my prototype webservice dcmdb
(http://dcmdb.org), so you can see I'm attempting to capture as many
transfer syntaxes as possible (http://dcmdb.org/main/transfer_syntax).
Using GDCM I have not have a transfer syntax issue as of yet.

I really want a pure python way to get images out of DICOM files, I know
Ruby DICOM (http://dicom.rubyforge.org/) was able to accomplish that goal,
so I'm going to start really investigating solutions to this problem ... if
you have any documentation that would be helpful on this journey, please
let me know.

pyd...@googlecode.com

unread,
Mar 9, 2014, 2:51:02 PM3/9/14
to pydic...@googlegroups.com

Comment #15 on issue 16 by mj...@nephosity.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Eric,

I'm a bit confused. you're already using GDCM. are you using its Python
extension? if so, what is the reason for a "pure Python" implementation?

in any case, OpenJPEG is really the best library out there, so you would
need to do one of two things
- link to it, and then wrap an extension around it. which is what GDCM
does. this is not pure Python, but is still usable from Python.
- compile it for Python, similar to how kripken has compiled it to JS
(https://github.com/kripken/j2k.js). this would be pure Python

HTH

pyd...@googlecode.com

unread,
Mar 9, 2014, 5:57:36 PM3/9/14
to pydic...@googlegroups.com

Comment #16 on issue 16 by darcy...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

First, thank you to everyone for the discussion. It's good to get these
ideas out there and see what we can come up with.

@neurosnap: I found this comment on ruby dicom:
https://groups.google.com/forum/#!topic/comp.protocols.dicom/NMdlfkzpaII
in which the author says it relies on ImageMagick but it only handles a
subset of the JPEG variants. That comment is almost a year old though, so
perhaps it has changed?

@mjpan: do you know any more about how the conversion to JS? It mentions
using Emscripten, which I see is an LLVM to JS compiler. Is there a similar
package to convert for python? And is there any hope of any of this working
at an acceptable speed?

To all: I've toyed with some ideas on pure python implementations, but if
you start reading through libjpeg (libijg)code (which dcmtk's jpeg is based
on) it is incredibly intertwined. It is so heavily optimized for speed
(and memory use) that it is very difficult to follow or translate to
another language. It has been around a long time, so it comes from an era
where speed and memory were very important. Plus (IIRC) there were many
workarounds for known poor implementations and such. It would be very hard
to replicate this in other code; I think that is why no one (very few?)
have succeeded in doing so. Everywhere I looked, I just saw the same
libijg code copied and pasted and minimally modified. In terms of
difficulty, Jpeg2000 may be a different situation; I haven't really looked
into those codes.

pyd...@googlecode.com

unread,
Mar 9, 2014, 9:06:34 PM3/9/14
to pydic...@googlegroups.com

Comment #17 on issue 16 by neuros...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Thanks for the response, ideally I would like a better mechanism to deploy
my server, something that would be easier to install besides GDCM, but I
may be out of luck. I'll investigate openJPEG.

pyd...@googlecode.com

unread,
Mar 10, 2014, 4:48:27 PM3/10/14
to pydic...@googlegroups.com

Comment #18 on issue 16 by mj...@nephosity.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

Darcy,

I'm sure that compiling OpenJPEG to pure Python can be done via LLVM, but I
don't think anyone has had the motivation to do so, as it's possible to run
C/C++ code in Python via an extension, as opposed to Javascript, which
requires JS code to run in the browser.

for anyone looking for Python -> OpenJPEG, I'd suggest checking out
Glymur. https://glymur.readthedocs.org/en/latest/introduction.html
although I can't imagine it being any easier than building the GDCM Python
extension-- CMake really makes things easy, especially as you go across
operation systems

pyd...@googlecode.com

unread,
Mar 17, 2014, 8:40:41 PM3/17/14
to pydic...@googlegroups.com

Comment #19 on issue 16 by martin.s...@gmail.com: Provide PixelData
decompression (JPEG, RLE, MPEG, etc)
http://code.google.com/p/pydicom/issues/detail?id=16

I just had a go at using Glymur to try to get pixel data out of a
1.2.840.10008.1.2.4.91: JPEG 2000 Image Compression'. I suspect that I am
going about the problems that I have encountered incorrectly. However I am
posting this comment here in case anyone can point out my mistake, or it is
of help to anyone else investigating Glymur.

From the initial error messages it seemed that there was no header data.
This seems consistent with, Part 5 of the DICOM standard section A.4.4
(p77) http://medical.nema.org/Dicom/2011/11_05pu.pdf staes:

"The optional JP2 file format header shall NOT be included. "

So far I have found Annex I of ISO15444-1
http://www.jpeg.org/public/15444-1annexi.pdf. Hoping DICOM pixel data
contained a Contiguous Codestream, I attempted to create the relevant
headings such that it could be simply read by the Glymur command for
opening JPEG2000 files.

import dicom
d =
dicom.read_file("/usr/lib/python2.7/dist-packages/dicom/testfiles/JPEG2000.dcm")
pd = d.PixelData
f = open("jp2000.jp2", "w+b")

f.write(struct.pack(">I4s4s", 12, "jP ", "\r\n\x87\n"))
f.write(struct.pack(">I4s4sI4s", 20, "ftyp", "jp2 ", 0, "jp2 "))
f.write(struct.pack(">I4s", 45, "jp2h"))
f.write(struct.pack(">I4sIIHBbBB", 22, "ihdr", d.Rows, d.Columns,
d.SamplesPerPixel, d.BitsAllocated - 1, 7, 1, 0))#Need to add 128
d.BitsAllocated - 1 if pixel data is signed
f.write(struct.pack(">I4sBBBI", 15, "colr", 1, 0, 0, 16))
f.write(struct.pack(">I4s", len(pd), "jp2c"))
f.write(pd)
f.flush()
f.close()

>>> jp2 = glymur.Jp2k("jp2000.jp2")
/home/martin/.virtualenvs/anat/local/lib/python2.7/site-packages/glymur/jp2box.py:135:
UserWarning:
Unrecognized box (����) encountered.
warnings.warn(msg)
/home/martin/.virtualenvs/anat/local/lib/python2.7/site-packages/glymur/jp2box.py:147:
UserWarning: ����
box has incorrect box length (2822332288)
warnings.warn(msg)
>>> jp2.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>

File "/home/martin/.virtualenvs/anat/local/lib/python2.7/site-packages/glymur/jp2k.py",
line 672, in read
img = self._read_openjpeg(**kwargs)

File "/home/martin/.virtualenvs/anat/local/lib/python2.7/site-packages/glymur/jp2k.py",
line 713, in _read_openjpeg
self._subsampling_sanity_check()

File "/home/martin/.virtualenvs/anat/local/lib/python2.7/site-packages/glymur/jp2k.py",
line 684, in _subsampling_sanity_check
dxs = np.array(codestream.segment[1].xrsiz)
IndexError: list index out of range

pyd...@googlecode.com

unread,
Jul 2, 2014, 7:31:30 PM7/2/14
to pydic...@googlegroups.com

Comment #20 on issue 16 by agrothberg: Provide PixelData decompression
If the image is showing a TransferSyntaxUID=1.2.840.10008.1.2.4.50
(http://www.dicomlibrary.com/dicom/transfer-syntax/) is there any way I can
get access to the raw data and just hand it to PIL to decode?

A naive call to: dicom.contrib.pydicom_PIL.show_PIL(dataset) did not work.
Reply all
Reply to author
Forward
0 new messages