Skimage - strange results from the resize function
I am trying to resize an image .jpg
using skimage.transform.resize function
. The function gives me a strange result (see image below). I'm not sure if this is a bug or just a misuse of the function.
import numpy as np
from skimage import io, color
from skimage.transform import resize
rgb = io.imread("../../small_dataset/" + file)
# show original image
img = Image.fromarray(rgb, 'RGB')
img.show()
rgb = resize(rgb, (256, 256))
# show resized image
img = Image.fromarray(rgb, 'RGB')
img.show()
Original Image:
Modified image:
I've already tested the skin resizing, giving a weird result , but I think my error has different propositions.
Update: also the rgb2lab function has a similar bug.
source to share
The problem is that skimage is converting the pixel data type of your array after the image is resized. The original image is 8 bits per pixel, type numpy.uint8
, and the modified pixels are variables numpy.float64
.
The resize operation is correct, but the result is not displayed correctly. To solve this problem, I suggest two different approaches:
-
To change the data structure of the resulting image. Before changing the uint8 values, the pixels must be converted to a 0-255 scale, as they are at a normalized 0-1 scale:
# ... # Do the OP operations ... resized_image = resize(rgb, (256, 256)) # Convert the image to a 0-255 scale. rescaled_image = 255 * resized_image # Convert to integer data type pixels. final_image = rescaled_image.astype(np.uint8) # show resized image img = Image.fromarray(final_image, 'RGB') img.show()
-
To use another library to display the image. Taking a look at the image library documentation , there is no mode that supports 3xfloat64 pixel images. The scipy.misc library, however, has the appropriate tools to convert the array format to display correctly:
from scipy import misc # ... # Do OP operations misc.imshow(resized_image)
source to share