Skip to content

Irrelevant kwarg tags cause imread to fail #454

Open

Description

@xkortex

I have a dataset of many image formats. I cannot map over imageio.imread(uri, exifrotate=False) easily, because I get this error on non-jpg images:

~/.virtualenvs/ml/lib/python3.6/site-packages/imageio/core/functions.py in imread(uri, format, **kwargs)
    219 
    220     # Get reader and read first
--> 221     reader = read(uri, format, "i", **kwargs)
    222     with reader:
    223         return reader.get_data(0)

~/.virtualenvs/ml/lib/python3.6/site-packages/imageio/core/functions.py in get_reader(uri, format, mode, **kwargs)
    141 
    142     # Return its reader object
--> 143     return format.get_reader(request)
    144 
    145 

~/.virtualenvs/ml/lib/python3.6/site-packages/imageio/core/format.py in get_reader(self, request)
    162                 "Format %s cannot read in mode %r" % (self.name, select_mode)
    163             )
--> 164         return self.Reader(self, request)
    165 
    166     def get_writer(self, request):

~/.virtualenvs/ml/lib/python3.6/site-packages/imageio/core/format.py in __init__(self, format, request)
    212             self._request = request
    213             # Open the reader/writer
--> 214             self._open(**self.request.kwargs.copy())
    215 
    216         @property

TypeError: _open() got an unexpected keyword argument 'exifrotate'

Is this expected behavior? I was a bit surprised, as I would expect irrelevant kwargs to simply be ignored.

Activity

  1. almarklein commented on Jun 17, 2019

    @almarklein
    Member

    Yes, it is expected. Ignoring irrelevant kwargs would mean a risk to ignore kwargs that simply had a typo. I do see how this causes pain in your use-case though. Something like #455 would help for the rotation-case.

    I also welcome ideas to work with this in a more generic (and preferably elegant) way ...

  2. xkortex commented on Feb 19, 2020

    @xkortex
    Author

    I am back on this task and the solution I ended up going with is a light wrapper function that receives a ignore_orientation flag, which if True, will perform the inverse rotation of _rotate(). Not the most elegant, but since my logic to detect Orientation returns None on failure, and my custom rotate() is a NOP when passed None, I can map over my messy dataset.

    I have an idea on how to possibly harmonize the above issue with another shortcoming I find with the imread interface, which is that it is challenging to know what options I can pass if I do not know what type of image I am dealing with ahead of time. It would be nice to capture "If PNG, do these options, if JPEG, do these other options" in a succinct and strongly typed way.

    Just throwing this idea out there. What if Requests were given an additional keyword parameter, let's call it plugin_args, of type Dict[str, Dict[str, Any]]. This would be a dict of FormatClassName: kwargs, e.g.:

    plugin_args = {
        'JPEGFormat': {'exifrotate': False},
        'PNGFormat': {'ignoregamma': False}
    }

    If plugin_args were present, the reader/writer obtained during imread/imwrite would have those kwargs applied, iff the string matches the class name. This might be pushing the limits of complexity of the intended use of imread, and might be better handled with an additional function ("imread_adaptive" or some more fitting name). I might hack around with this.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions