Repository navigation
Description
In #563 I made a first proposal for a new API. There were some interesting discussions, which have lead to some new insights, e.g. that we may be able to pull this off without the mode argument. Here's a the tweaked version.
The base API is similar to what I proposed earlier. It provides access to a plugin object, providing complete functionality in a concise way, and supports keeping the file open to support random/lazy access. As a consequence, use of the with-statement is recommended.
# Get a read object (this is an instance of the actual plugin class for the appropriate format)
r = imopen(filename, mode='r/w') # default mode is 'r'
# Read an image
with imopen(...) as r:
im = r.read()
# Write an image
with imopen(..., mode='w') as w:
w.write(im)
# Read image and meta data
with imopen(...) as r:
meta = r.get_meta()
im = r.read()
# Read an image from a series (sampling in the first dimension)
# Some formats (e.g. video) may require the user to either this or use the iterator
with imopen(...) as r:
im = r.read(index)
# Iterate over the images in a sequence
with imopen(...) as r:
for im, meta in r.iter():
passSince forcing users to use a with statement everywhere does not seem like a good idea, so let's add convenient function(s):
# This one at the least.
im = imread(...)
# Perhaps this too, but already more special.
for im in imiter(...):
pass # do stuff with the image
# Is this common enough to warrant a convenience function?
imwrite(filename, im)With regards to backwards compatibility, we have a bit of a problem because we're introducing a new imread function with a different behavior. Also the API is very similar, but still different in subtle ways.
At the very least we should put the API in a new namespace, and since we're also contemplating a new plugin system, it may be worth considering creating a brand new library.
Thoughts?
cc @jni
Activity
FirefoxMetzger commentedon Nov 20, 2020
Two thoughts:
Would bumping the version major suffice instead of having to do a brand new library? At first glance, it seems possible to write wrapper code for both, the old API calling the new one (with deprecation warning), and the new API calling legacy plugins (not 100% sure on this one) and then successively update the codebase.
@lschr has proposed a sliceable API in Add sliceable interface class akin to PIMS #564 and the decision there is/was that this should live in his package for now, with
imageioreferring to it. Could the same thought apply to the simplified / no with-statement API andskimage?skimagealready has discussions on writing a (razor-)thin wrapper aroundimageio. (sneakily looking at @jni )The new
imopenAPI seems like a library mainly targeting software engineers for use in packages; not so much by people scripting or hacking something together. In this context enforcing code quality and the use of thewithstatement to make resource usage explicit actually seems like a good idea. It is a little more verbose, but being explicit here may help with long-term maintenance (and allows lazy-loading, etc.).If you are primarily scripting/hacking instead of (software) engineering, then your focus likely isn't I/O anyway, and it is but a means to enable other code (likely another library/package) to consume an image. That package will then know how to best expose and consume the image. To me, it seems logical to let that package depend on
imageioand decide how to expose the reading/writing mechanism similar to what Ischr is doing already.almarklein commentedon Nov 20, 2020
I started writing a response with pro's and con's, but I'm starting to feel that the answer is: Yes.
The thing is, I mentioned that the fact that we're changing the behavior of
imreadis a problem for backwards compatibility. However, the current implementation ofimread()can be considered broken, because it may sometimes not read all the available data. Unless I'm mistaken, the only cases where the newimreadwould behave differently from the old, is in such cases where data would be omitted. So we should change it anyway!I'm not 100% sure whether we can make the other API functions behave exactly the same, but probably close enough for by far the most cases.
In theory yes, but I think that having a simple API in imageio is valuable, otherwise there is a risk that each image processing lib implements its own slightly different version of imread.
Yes, and users who need a bit more, e.g. metadata. I feel this is another reason to keep the API's close together - to make it easy for someone using
imreadto transition to usingimopen. That should not feel like a big step.