0

I am trying to convert an uploaded image(JPEG, gif) into uint8/16 type on Google Colab using skimage.util.

# How to Import an image
from google.colab import files
from IPython.display import Image

# Convert to 8-bit uint
from skimage.util import img_as_uint

uploaded = files.upload()

img_as_uint(uploaded)

I am getting error in last line as:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-c19636843da4> in <module>()
----> 1 img_as_uint(uploaded)

1 frames
/usr/local/lib/python3.6/dist-packages/skimage/util/dtype.py in convert(image, dtype, force_copy, uniform)
    248     if not (dtype_in in _supported_types and dtype_out in _supported_types):
    249         raise ValueError("Can not convert from {} to {}."
--> 250                          .format(dtypeobj_in, dtypeobj_out))
    251 
    252     if kind_in in 'ui':

ValueError: Can not convert from object to uint16.

1 Answers1

0

I found a simple solution to the above problem.

We can use an matplotlib.pyplot.imread('filename.jpg') to convert the input image into an image object/pixel intensity values.

The code is as follows:

# How to Import an image
from google.colab import files
import matplotlib.pyplot as plt

# Select file from Local Drive
uploaded = files.upload()

# Convert uploaded image into uint type
img = plt.imread('Lenna.jpg')

# Confirm pixel dtype, shape and range(should be 0-255)
print('Data Type:',img.dtype)
print('Data Shape:',img.shape)
print('Min: %.3f, Max: %.3f' % (img.min(), img.max()))

Output :

Data Type: uint8
Data Shape: (373, 497, 3)
Min: 0.000, Max: 255.000