Skip to content

How should imageio deal with colorspaces? #504

Open

Description

@almarklein

I think that at the moment, imageio generally returns either grayscale, RGB, or RGBA. Though I am less certain about Tiff files.

So when imageio encounters data that is not RGB (e.g. HSV) what should it do?

My inclination is to go with the former. That way, you can read data without knowing the source and work with it. For more control, we can consider adding an argument to the read functions to return another colorspace or the original colorspace.

Activity

  1. ivoflipse commented on Jan 29, 2020

    @ivoflipse

    How does OpenCV handle this?

  2. FirefoxMetzger commented on Mar 5, 2022

    @FirefoxMetzger
    Contributor

    Looking at this 2 years later, we seem to have settled for option two.

    Currently, plugins return the data in the native colorspace by default since this is the least surprising/assuming. Additionally, we offer a kwarg (format for pyav, mode for pillow, ...) that allows users to specify a colorspace that the image will be converted into if it isn't in that format already.

    @almarklein Any objections to me closing this issue?

  3. almarklein commented on Mar 8, 2022

    @almarklein
    MemberAuthor

    Might still be worth explicitly mentioning this in the docs. What do you think?

  4. almarklein commented on Mar 8, 2022

    @almarklein
    MemberAuthor

    Actually, I think I am still in favor of returning RGB/RGBA by default, and provide an option to return as it is stored. One of the principals of imageio has been that you read any format and you get back a numpy array in the expected shape that you can just pass to a visualization tool.

  5. FirefoxMetzger commented on Mar 8, 2022

    @FirefoxMetzger
    Contributor

    One of the principals of imageio has been that you read any format and you get back a numpy array in the expected shape that you can just pass to a visualization tool.

    Yes, I am aware and I think it is a nice idea to convert into a standard colorspace. Unfortunately, I think it only works as a first-order approximation, because there are quite a few situations where this breaks down:

    • What about (binary) mask images? The expectation would be to get a boolean mask, and plugins like pillow indeed return a bool array. We could promote to RGB(A), but I would find that quite surprising. Maybe we want to make an exception for these.
    • What about grayscale images? I think users would be quite surprised to get a color channel or have it promoted to RGB(A) by default. We could, again, make an exception for this colorspace.
    • What about ImageJ hyperstacks? Those are stored channel-first and have usually no obvious relationship to RGB unless they happen to represent staining that uses those colors and that has planes in that order. I'm not sure there is a natural/ intuitive way to convert this to RGB. Again we could (must?) make an exception for these images.
    • What about backends that don't convert to RGB by default? This affects, essentially, all backends. So far our policy was "do what the backend does". We could change this policy to "convert to RGB(A) conditional to ", but this will make plugins more complicated.
    • On the same note, what about backends that don't offer built-in conversion logic? So far, whenever we do (or offer) colorspace conversion this is based on a plugin's native capability. This fits with our general philosophy of exposing backend features where possible, but we would have to come up with a way of dealing with plugins that can't do this. Among the existing popular ones, this would be tifffile and maybe freeimage (not 100% sure). This would imply us adding a canonical conversion module that deals with colorspace conversions. Besides me being unwilling to reinvent that wheel, it comes with a bunch of other questions (should plugins like pillow use the canonical conversion or the native one?). We could, of course, again dodge that bullet by making an exception for those plugins, because it doesn't affect the commonly used ones.
    • What about video formats? I know that the general abstraction of video in python is Sequence(image) where image is RGB, but this is almost never the case inside an actual video file which usually is YUV. Performance implications aside, conversion between RGB and YUV isn't lossless (even for non-subsampled YUV444), and I'm not sure if our default loader should silently degrade image quality. Again, we could consider an exception here.
    • Speaking of image quality, what about formats with more than 8-bit color depth? Would we want to reduce to 8-bit to stick to the expected np.uint8 datatype, or should we keep the other type to preserve quality?

    The list goes on, but I think the problem is already apparent: Our list of exceptions quickly grows so long that promotion to RGB(A) becomes itself an exception and not the default behavior. If this would only need dealing with inside the library I'd be happy to see if we can implement it in a maintainable way but any exception here will directly affect the end-user.

    The alternative of: "We return what is stored in the file unless told not to." is a much simpler mental model for the user (there are no exceptions), and much easier to implement on our side. There is the use-case of "I don't care, I want RGB only because my pipeline works with RGB only" that needs dealing with, but - for images - that can be mostly solved by passing mode="RGB" to pillow and - for video - this can be solved by passing format="rgb24" to pyav once merged. That's essentially why I am currently writing code to return the native colorspace where possible.

    For either scenario, there is a secondary issue and that is discoverability of the colorspace, but I think this is a separate issue and a discussion of coming up with standard names for them and, perhaps, a ImageProperties.colorspace metadata field.

    Might still be worth explicitly mentioning this in the docs. What do you think?

    Sure. Where should this live?

  6. almarklein commented on Mar 8, 2022

    @almarklein
    MemberAuthor

    Ok, maybe I should've said the user should expect to get either grayscale, RGB or RGBA. The dtype depend on the format/plugin. So a boolean mask can be np.bool, sure.

    What about ImageJ hyperstacks?

    Fair point. That is not color data, and we should not try to convert it to RGB.

    What about backends that don't convert to RGB by default?

    I think if it represents a color, but stored in a different colorspace, we should convert to RGB/RGBA if possible.

    What about video formats?

    If I do imread('somevideo.someext') I just want to be able to show that in MPL or whatever and see the frame as intended, not some YUV encoded gibberish. I think the cases were people want the "raw" data are much more rare, so I feel quite strongly for reading as rgb24 as the default here.

    "We return what is stored in the file unless told not to." is a much simpler mental model for the user (there are no exceptions)

    I disagree here. Most users won't know much about colorspaces and just want to read and display an image.

    Maybe the rule should be: we return grayscale, RGB or RGBA data of the appropriate dtype unless: 1) the user asks for another colorspace; 2) the data is not actually color; 3) the plugin does not support conversion to RGB colorspace.

  7. FirefoxMetzger commented on Mar 8, 2022

    @FirefoxMetzger
    Contributor

    Okay, let's play with that idea and assume we go down the rgb /w exceptions route.

    • What would we choose as the default value for conversion-related kwargs like pillow's mode or pyav's format? Presumably, we want to maintain the ability to read in the format's native colorspace (which currently is None, eg. mode=None) but we also want a sentinel for rgb /w exceptions and maintain the ability to specify an explicit colorspace.
    • This would imply some backward-incompatible changes for our pillow plugin, which currently returns whatever pillow returns, so we would need a version-major bump. Considering that this will take time to implement, I guess we'd target ImageIO v4?
    • Should we convert channel-first RGB to channel-last RGB?
    • I guess we want to keep the same ndarray container type, e.g. convert 12-bit YUV (uint16) into 12-bit RGB? What about HDR formats that use floating point? Do we demote to int or keep float?
    • Where would we put the docs that describe our type conversion rules? My suggestion would be in the notes of imread/imiter, but this may be easy to overlook, so having it in the narrative docs somewhere may be better?

    I think the cases were people want the "raw" data are much more rare, so I feel quite strongly for reading as rgb24 as the default here.

    I think this is speculation in either direction. The main reason that nobody uses "raw" data is that none of the frequently used readers allow you to return YUV. We should offer that feature and see if it is adopted. You are right that most everyday users won't care/know about colorspaces, but developers that write library code certainly do, and I'd expect them to care about what happens to their data (extrapolating from a sample-size of 1 - me xD).


    While we are at it, we could also discuss common/standardized names for colorspaces and if we can expose them in ImageProperties. I like the idea of being able to do iio.improps("my/image.ext").colorspace and discover what I am dealing with.

    However, dealing with the results of conversions in iio.improps is messy, because it is costly to actually convert an image an inspect the result and there is no good scaffolding I am aware of to discover this statically. So if this idea is good, I guess we should discuss this in a separate issue?

  8. almarklein commented on Mar 9, 2022

    @almarklein
    MemberAuthor

    What would we choose as the default value for conversion-related kwargs like pillow's mode or pyav's format?

    For video I think I feel strongest, because the format is often not stored in RGB, and being lossy, people are more likely to care just about the "images" rather than the data.

    For Pillow it depends on the internal format. Anything that represents a color should map to either RGB or RGBA (depending on whether an alpha channel is present).

    This would imply some backward-incompatible changes for our pillow plugin

    Mmm ... I expected most of pillow to return gray/rgb/rgba already. What formats do not?

    Should we convert channel-first RGB to channel-last RGB?

    That is a good question, maybe it will be easier to answer this once we resolve the original issue.

    I guess we want to keep the same ndarray container type, e.g. convert 12-bit YUV (uint16) into 12-bit RGB? What about HDR formats that use floating point? Do we demote to int or keep float?

    The plugin should in any case try to avoid data loss. Float is fine too.

    Where would we put the docs that describe our type conversion rules?

    What about in a section in the API docs. Like on the same page of imread and imiter, but a separate section?

    You are right that most everyday users won't care/know about colorspaces, but developers that write library code certainly do [...]

    I agree with that. Do you think that the cases where people are more likely to be interested in the raw data are also specific plugins/formats? I'm starting to think that perhaps the decision on what to return should be made per plugin, as the expectations are not the same.

    While we are at it, we could also discuss common/standardized names for colorspaces and if we can expose them in ImageProperties.

    Maybe it should be in metadata, also because we want to differentiate between the format as it was stored, and the format it was converted to while reading (similar to exif rotation).

  9. FirefoxMetzger commented on Mar 9, 2022

    @FirefoxMetzger
    Contributor

    For video I think I feel strongest, because the format is often not stored in RGB, and being lossy, people are more likely to care just about the "images" rather than the data. [...] I'm starting to think that perhaps the decision on what to return should be made per plugin, as the expectations are not the same.

    I got the same impression. We can (and do) actually allow for plugin-specific default values, so one solution here could be to keep the default mode for pillow as None (return the type that pillow deems correct) and set the default for pyav as rgb24 while documenting that you can set None to get the native colorspace. This also seems quite maintainable, because we dodge a lot of bullets like deciding what file does or doesn't contain a convertible colorspace. I think I can be quite happy with this solution. What do you think?

    Mmm ... I expected most of pillow to return gray/rgb/rgba already. What formats do not?

    Pillow returns what is in the file, so if you have a palette PNG (rare but it does exist) you will get a palette image back (palette indices). Binary images (thinking of PNG here again) are also returned as-is. GIF will always return the palette indices up until pillow9. Currently (pillow9), the first frame is a palette and subsequent ones are RGB/RGBA) and in the future (pillow9.1) you may get gray (L) or gray with alpha (LA) depending on what is inside the file. In practice, however, most files actually choose to store RGB(A), so you don't necessarily notice the existence of or behavior for other colorspaces.

    Maybe it should be in metadata, also because we want to differentiate between the format as it was stored, and the format it was converted to while reading (similar to exif rotation).

    My thinking was to allow a format/mode kwarg in improps similar to how we allow an index kwarg so that the function can work out what the resulting properties should be. This way, we can mirror the behavior of imread/imiter (which ever ends up the one we are settling for).

  10. almarklein commented on Mar 10, 2022

    @almarklein
    MemberAuthor

    Yeah, I think in this discussion we've found that it's hard to come up with a generic approach (for the colorspace of the returned output) that works for everyone. The decision what to output (by default) is probably best made per plugin.

    Since the names of the colorspace/format/channel-spec/whatever is probably also different between different plugins, I can imagine that standardizing this is not trivial (e.g. some formats are specific to video encoding). So I'm leaning towards putting this information in the metadata. That said, we could have a simplified flag that defines whether the data is gray/gray-alpha/rgb/rgba/hsv/cmyk/other, where "other" is either not-a-color or a colorspace not in our list.

    Pillow returns what is in the file, so if you have a palette PNG (rare but it does exist) you will get a palette image back (palette indices).

    I'm pretty sure that for PNG paletted images, the palette is applied to yield an RGB/RGBA image. Same for GIF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions