0

I'm trying to convert a .tif image in python using the module skimage. It's not working properly.

from skimage import io
img = io.imread('/content/IMG_0007_4.tif')
io.imsave('/content/img.jpg', img)

Here is the error:

/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py in get_writer(uri, format, mode, **kwargs)
 if format is None:
   raise ValueError(
     "Could not find a format to write the specified file " "in mode %r" % mode)

ValueError: Could not find a format to write the specified file in mode 'i'

EDIT 1:

A method I found to do this was to open using skimage, convert it to 8bits and then save it as png. Anyway I can't save it as .jpg

img = io.imread('/content/IMG_0007_4.tif',as_gray=True)
img8 = (img/256).astype('uint8')
matplotlib.image.imsave('/content/name.png', img8)
hopieman
  • 399
  • 7
  • 22
  • There's already a question about saving jpg images using sklearn. This isn't a duplicate, but you may be able to find your answer on how to achieve your results here: https://stackoverflow.com/questions/47361966/scikit-image-write-a-ndarray-to-image-with-imsave-read-back-with-imread-data –  Sep 05 '19 at 17:21
  • Please also provide the test image, so that we can see if there is anything special about this specific file. – Stefan van der Walt Sep 05 '19 at 17:29
  • Does the TIF have either layers or pages? – fmw42 Sep 06 '19 at 04:49

2 Answers2

0

You have not provided an image plugin in the save command. See https://scikit-image.org/docs/dev/api/skimage.io.html#skimage.io.imsave where it says:

When saving a JPEG, the compression ratio may be controlled using the quality keyword argument which is an integer with values in [1, 100] where 1 is worst quality and smallest file size, and 100 is best quality and largest file size (default 75). This is only available when using the PIL and imageio plugins.

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

I found a good tool called ImageMagick it can be installed in linux. To call it inside python code i just did this.

os.system("convert image.png -colorspace RGB image.jpg ")
hopieman
  • 399
  • 7
  • 22
  • You can use Python Wand, which uses ImageMagick, if you do not want to use os.system. See http://docs.wand-py.org/en/0.5.7 – fmw42 Sep 11 '19 at 18:35