Repository navigation
Description
See #362 for the meta-data overview issue.
This relates to #362, #382 and #371 . I would like to see an endpoint in the API which provides any orientation information which was used to transform the image on loading.
Here is my use case. I have a dataset of ~30k images of just about every data format under the sun, but mostly jpg. 75% have the exif Orientation tag, and a third of those have a non-upright rotation. I have a corresponding set of png images which are binary masks of regions of interest. All of the masks have the same orientation of the raw image array, i.e. ignoring the orientiation tag. But, I also need the images oriented upright, so that they can be fed into various detectors which are rotationally variant.
I cannot map over imageio.imread(uri, exifrotate=False) easily, because I get this error on non-jpg images, see #454, otherwise I could load everything with consistently no rotation, and rotate myself. Right now, I am using this function to pull the orientation value in a manner similar to the JPEGFormat(PillowFormat) reader.
def get_orientation(metary):
# hastily assigning integer tags to invalid/unread orientation
meta = getattr(metary, 'meta', {})
exif = meta.get("EXIF_MAIN", {})
if not exif:
return -1
ori = exif.get("Orientation", -2)
return oriwhich works, but I do not have any guarantees about the stability of those keys. Image orientation seems to be a thorn in many sides, so I think it would help further imageio's goal of being a one-stop-shop python frontend for image I/O if the ability to manipulate orientation gracefully were a part of the public API, or at least a consistent key in the metadata.
One way I would suggest accomplishing this would be to, whenever a function like Reader._rotate is called, set img.meta['orientation'] = ori. Therefore the presence of that key indicates the image was rotated on load. If you wanted to save as jpg without orientation, you would del img.meta['orientation']. Saving to a non-jpg format would save the array as displayed. One could even throw a warning that it is saving with an orientation tag present, to a format that lacks orientation support.
Another option would be a helper function get_orientation provided with the library, which would update if the EXIF_MAIN were to ever change.
Possibly, it might make sense to have an origin property, which is expected to be a transformation matrix. This would be generally useful in the ITK sphere, for DICOM slices, etc. JPG exif would be mapped to one of eight 3x3 affine matrixes corresponding to the Orientation tags on load. This approach might be bringing a bazooka to a knife fight though. :)
Activity
xkortex commentedon Feb 19, 2020
Here's a convenience function I've been using. I have some other stuff that I might wanna PR, but I have to check with my employer first.
hmaarrfk commentedon Sep 24, 2020
Have you figured out how to write the exif orientation using imageio?
FirefoxMetzger commentedon Jun 22, 2021
@hmaarrfk We have a unit test for pillow that tests the exif orientation tag written by ImageIO
imageio/tests/test_pillow.py
Lines 227 to 260 in b4f186f
It uses pillows EXIF class until there is some more concrete idea on how metadata can be handled consistently. Because of this, you might consider it more of a hack than a solution depending on what you were looking for.
FirefoxMetzger commentedon Jan 26, 2022
I'm going through metadata issues at the moment, to see which ones are still relevant.
@almarklein I think the main problem here is that the pillow plugin doesn't auto-rotate. However, most other image-related projects I am aware of do. Do you think it makes sense to change this default in ImageIO v3 to closer match users' expectations?
@xkortex Not sure if you still remember the situation you had (or if this is still relevant). If you do, could you comment whether auto-rotating all images (if such a tag is present) would solve your problem?
Regarding a standardized name for any rotation that may exist, we could start discussing it as a candidate for
ImageProperties(see #739 and also the big metadata discussion in #362 ). My main problem is that it becomes a bit confusing which value to set: Assume a plugin auto-rotates. Should it now set the orientation field to the value that is stored in the file or to the value that represents the orientation after the data was loaded? What about loading with auto-rotation disabled? Both make sense, but neither seems more "natural" than the other.almarklein commentedon Jan 28, 2022
+1