-
Notifications
You must be signed in to change notification settings - Fork 50
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
Incorrect Color Interpretation in OpenSlide When Changing JPEG Q Factor in pyvips #483
Comments
i stumbled across your issue and also found #133 and it seems to say that one can use: x.get_fields() # get list of fields
x.get('orientation') # find relevant field
x.set('orientation', 99) # modify could this somehow be incorporated into your code to "override" the defaults, i.e., either using this to explicitly set the correct TAG or compression subsampling values? wouldn't resolve the underlying issue - but might still enable working around for the time being? |
Hello @JulienMassonnet, libvips will automatically disable chroma subsample for Q >= 90 (imagemagick does this too). For example:
And with 90:
You can see the photometric interpretation has changed. Even though the pixels are now RGB (not YCbCr), openslide will still run a YCbCr->RGB conversion, resulting in crazy colours. The simplest fix is to stay under Q 90. Would this work for you? |
Thanks for your responses. To give a little more context, our research pipeline needs to convert DICOM Whole Slide Images to pyramidal TIFF. We are currently evaluating various JPEG compression levels to determine the optimal balance for our specific use case. Given that the source DICOM files are already compressed, we are aiming to minimize any additional compression artifacts introduced during the conversion process. We have also noticed that a Q factor of 90 performs slightly better than Q factors in the range of 91-95, is there a reason for that?
Are you saying that openslide applies the YCbCr->RGB conversion to the RGB channel? Does it apply twice the conversion? When I change the photometric interpretation tag to YCbCr, openslide can correctly read the region, doesn't this imply that the conversion is needed? Shouldn’t this tag always be YCbCr here? Regarding the openslide warning, I want to fix this metadata for stability purposes. We noticed that the TIFF files generated by pyvips lack the YCbCrSubSampling (530) tag. While the default value of [2,2] works with a Q factor below 90, when Q is above 90, hence with no chroma subsampling, it necessitates a tag value of [1,1]. |
You can copy the JPEG DICOM tiles out directly into libtiff, with a bit of work. Have you considered this? You could save all decompress/recompress, and it should be very quick. libdicom (the lib openslide uses for DICOM read) will let you get the raw JFIF tiles from the DICOM: https://github.com/ImagingDataCommons/libdicom You do need to be careful with photometric interpretation (ie. YCbCr vs, RGB). DICOM has JFIF tiles (not JPEG!) and these don't include the PI as part of the tile. You'll need to get the PI from the DICOM, then use that to set the PI in the tiff you write. To add even more confusion, libtiff has a pseudotag (not a real tag) for the JPEG colour mode which you'll need to be aware of. If you're just copying tiles in, it's probably OK to avoid it.
No, libtiff does not support generic tag write. |
Operating System:
Libvips Version:
Slide Format:
Issue Details:
I am using pyvips to load a slide (A) and save it as a JPEG-compressed TIFF (B). When I use tiffsave with the default Q factor (75), I can correctly load slide (B) with OpenSlide. However, if I change the Q factor value, OpenSlide displays the YCbCr channels instead of RGB.
This issue occurs with slide (A) in both DICOM and TIFF formats.
For slides in TIFF format, the issue arises when loading with pyvips.Image.new_from_file or pyvips.Image.openslideload, but not with vips.Image.tiffload.
The problem in my case is that I want to load a DICOM and save it to TIFF.
Using tifftools to inspect slide (B)'s metadata, I found that the TIFF TAG PhotometricInterpretation (262) is set to 2 (RGB) for problematic slides and 6 (YCbCr) for slides that are read correctly. It appears that OpenSlide is not converting to RGB due to this metadata.
Manually changing this value allows OpenSlide to read the slide correctly, although the following warning is still generated:
OpenSlide Warning:
JPEGFixupTagsSubsamplingSec: Warning, Auto-corrected former TIFF subsampling values [2,2] to match subsampling values inside JPEG compressed data [1,1].
The text was updated successfully, but these errors were encountered: