scikit-image / scikit-image

Image processing in Python
https://scikit-image.org
Other
6.07k stars 2.22k forks source link

better if format parameter overwrites extension #3774

Open simonm3 opened 5 years ago

simonm3 commented 5 years ago

I am using a temporary file context manager in luigi. This saves output in a temporary file until the task is complete then renames it. This won't work with skimage because the file extension is used to determine the format. Although there is a format parameter this seems to only work if there is no extension e.g.

imsave("f.temp123", img, format="png") gives "TypeError: save() got multiple values for keyword argument 'format'"

Would be better if the format parameter took precedence over any extension.

soupault commented 5 years ago

@simonm3 could you provide the output of skimage.io.available_plugins?

simonm3 commented 5 years ago

{'gdal': ['imread', 'imread_collection'], 'gtk': ['imshow'], 'imread': ['imread', 'imsave', 'imread_collection'], 'imageio': ['imread', 'imsave', 'imread_collection'], 'matplotlib': ['imshow', 'imread', 'imshow_collection', 'imread_collection'], 'pil': ['imread', 'imsave', 'imread_collection'], 'qt': ['imshow', 'imsave', 'imread', 'imread_collection'], 'fits': ['imread', 'imread_collection'], 'simpleitk': ['imread', 'imsave', 'imread_collection'], 'tifffile': ['imread', 'imsave', 'imread_collection']}

On Wed, 27 Feb 2019 at 15:42, Egor Panfilov notifications@github.com wrote:

@simonm3 https://github.com/simonm3 could you provide the output of skimage.io.available_plugins?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/scikit-image/scikit-image/issues/3774#issuecomment-467912091, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJN6TPxPmiQh2RE4xtkcjfaCIZgvYNHks5vRqdVgaJpZM4bU0Jk .

hmaarrfk commented 5 years ago

I think this is a reasonable request. Though i'm scared it would make things confusing for users who shoot themselves in the foot saving PNG format in a .jpeg. That said, i'm pretty OK with allowing you to shoot yourself in the foot.

I would have to look at things more closely, but I may be in favour of approving such a change.

That said, as an immediate solution, I suggest you use the plugin you wish directly. We mostly forward things to imageio.

simonm3 commented 5 years ago

I am not sure this is possible but maybe I am misunderstanding.

imsave("temp.png", img) => works fine imsave("temp.png", img, format="png") => save() got multiple values for keyword format imsave("temp", img, format="png") => save() got multiple values for keyword format imsave("temp", img, plugin="imageio", format="png") => No Module named imageio imsave("temp", img, plugin="imread", format="png") => Module not found error. Imread could not be found....

On Wed, 27 Feb 2019 at 16:31, Mark Harfouche notifications@github.com wrote:

I think this is a reasonable request. Though i'm scared it would make things confusing for users who shoot themselves in the foot saving PNG format in a .jpeg. That said, i'm pretty OK with allowing you to shoot yourself in the foot.

I would have to look at things more closely, but I may be in favour of approving such a change.

That said, as an immediate solution, I suggest you use the plugin you wish directly. We mostly forward things to imageio.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/scikit-image/scikit-image/issues/3774#issuecomment-467932686, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJN6QXDOnCFCVlLYSsosYFsvB39PzDCks5vRrL9gaJpZM4bU0Jk .

hmaarrfk commented 5 years ago

Sorry for being unclear @simonm3. I meant to literally import imageio directly instead of using it through scikit-image.

In [1]: from imageio import imwrite                                                                     

In [2]: import numpy as np                                                                              

In [3]: image = np.zeros((10, 10), dtype='uint8')                                                       

In [4]: imwrite?                                                                                        
Signature: imwrite(uri, im, format=None, **kwargs)
Docstring:
imwrite(uri, im, format=None, **kwargs)

Write an image to the specified file.

Parameters
----------
uri : {str, pathlib.Path, file}
    The resource to write the image to, e.g. a filename, pathlib.Path
    or file object, see the docs for more info.
im : numpy.ndarray
    The image data. Must be NxM, NxMx3 or NxMx4.
format : str
    The format to use to read the file. By default imageio selects
    the appropriate for you based on the filename and its contents.
kwargs : ...
    Further keyword arguments are passed to the writer. See :func:`.help`
    to see what arguments are available for a particular format.
File:      ~/miniconda3/envs/owl/lib/python3.7/site-packages/imageio/core/functions.py
Type:      function

In [5]: imwrite('test.jpg', image, format='png') 

image

Now renaming my image to .png

image

You can see that it actually used png format as you wanted it to.

In my mind, file saving formats are so vastly different, that once you go beyond imageio, there is little incentive of making a wrapper for them.