Search code examples
pythonimagepngscikit-imageopencv

How to reshape png image read by skimage.imread


Then I read some jpg file, this way

image = imread('aa.jpg')

As result I get dataframe with numbers from 1 to 255

I can resize it this way:

from cv2 import resize
image = resize(image, (256, 256)

But then I doing same think with png, result not desired.

image = imread('aa2.png')  # array with number within 0-1 range
resize(image, (256,256)) # returns 1 channel image
resize(image, (256,256, 3))   # returns 3 channel image

Weird image enter image description here

But imshow(image)

enter image description here


Solution

  • cv2.imread reads the image in 3 channel by default instead of 4. Pass the parameter cv.IMREAD_UNCHANGED to read your PNG file and then try to resize it as shown in the code below.

    import numpy as np
    import cv2 as cv
    import matplotlib.pyplot as plt
    
    img = cv.imread('Snip20190412_12.png', cv.IMREAD_UNCHANGED)
    print(img.shape) #(215, 215, 4)
    
    height, width = img.shape[:2]
    res = cv.resize(img,(2*width, 2*height))
    print(res.shape)#(430, 430, 4)
    plt.imshow(res)
    

    enter image description here