Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPEG encoding #57

Open
kurmukovai opened this issue Mar 31, 2021 · 6 comments
Open

JPEG encoding #57

kurmukovai opened this issue Mar 31, 2021 · 6 comments
Labels
question Further information is requested

Comments

@kurmukovai
Copy link

kurmukovai commented Mar 31, 2021

Hello, I am trying to implement jpeg encoding for dicom within a python (without saving on disk, thus gdcmconv is not an option). I tried to use PIL, but unfortunately some of the required (by PIL) libraries has versions conflicts (with versions I use).
It would be great if you can give any starting point on how to add such functionality.

@scaramallion
Copy link
Member

scaramallion commented Mar 31, 2021

Are you writing a standalone implementation or binding to an existing library (and if so, which one)? What JPEG formats are you aiming for?

@kurmukovai
Copy link
Author

I was thinking of a wrapper around existing library which implements JPEG/JPEG2000 (to be honest I am not aware of the differences) encoding + functionality to automate modification of all required dicom tags.

There are several python libraries which wraps around C/Java libraries implementing JPEG, I've tried simplejpeg (works with uint8, no grayscale16 support), turbojpeg (dunno the exact issue, but it also failed to encode int16 grayscale numpy arrays), glymur (only supports 3.6,3.7 and this project I am working on is 3.8).

@scaramallion
Copy link
Member

I would suggest reading up on the differences in the JPEG standards that DICOM uses:

  • JPEG - ISO/IEC 10918-1 (ITU T.81), 10918-2 is also super helpful in figuring out the JPEG "Process" numbers.
  • JPEG-LS - ISO/IEC 14495-1 (ITU T.87)
  • JPEG2000 - ISO/IEC 15444-1 (ITU T.800)

The first two are available online via the ITU, however the J2K standard isn't (you might get lucky and find a PDF by googling 😉). Even the general introductions in Wikipedia or the JPEG group's website is a good start, but if you want to know what bit-depth and options (lossy/lossless, colour spaces, signed/unsigned) are available you'll have to dig through the standards.

Then you have to decide which of those various bit-depth/lossy/sign options you want to support and find a backend library that can handle them (and don't forget to check the licensing each library offers). For example, the IJG's libpeg (written in C) can only handle non 8- or 16-bit images after changing a config flag and recompiling, which is pretty impractical if you want to encode both 12-bit for the JPEG Extended transfer syntax and 8-bit for JPEG Baseline (although it can be done, see GDCM).

The nice thing about most libraries, though, is that they usually come with an encoding application, which you can look at to figure out how the library is supposed to be used (e.g. the openjpeg library has a J2K convert tool, which includes encoding). You could take a look at the two pylibjpeg plugins to see how to get them working via Cython (and my very amateurish C/C++). Or at Pillow to see how they do it, or at the various wrapper Python libraries as well.

@kurmukovai
Copy link
Author

Thank you for this comprehensive answer, I'll dig into the standards.

@cgohlke
Copy link

cgohlke commented Apr 11, 2021

Have you tried the imagecodecs package? It's more geared towards supporting TIFF and CZI files, but it can handle a several "JPEG" formats (JPEG 8 and 12 bit, LJPEG, JPEG LS, JPEG XR, JPEG 2000) via the libjpeg-turbo, liblj92, charls, jxrlib, and openjpeg libraries.

@scaramallion scaramallion added the question Further information is requested label Apr 11, 2021
@kurmukovai
Copy link
Author

kurmukovai commented Apr 11, 2021

Have you tried the imagecodecs package? It's more geared towards supporting TIFF and CZI files, but it can handle a several "JPEG" formats (JPEG 8 and 12 bit, LJPEG, JPEG LS, JPEG XR, JPEG 2000) via the libjpeg-turbo, liblj92, charls, jxrlib, and openjpeg libraries.

Thanks, I will give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants