Rocketq
Rocketq

Reputation: 5791

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

Upvotes: 0

Views: 2618

Answers (2)

Muthukrishnan
Muthukrishnan

Reputation: 2197

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

Upvotes: 1

Luis Bleda Torres
Luis Bleda Torres

Reputation: 25

I guess is some problem with your image or code.

Here a free image to try: https://pixabay.com/vectors/copyright-free-creative-commons-98566/

Maybe you have problem with libpng, check this answers: libpng warning: iCCP: known incorrect sRGB profile

Check this simple code that works on PNG images.

     import cv2 as cv
     image = cv.imread("foto.png")
     if __name__ == "__main__":
          while True:
                image = cv.resize(image,(200,200))
                cv.imshow("prueba",image)

                key = cv.waitKey(10)
                if key == 27:
                    cv.destroyAllWindows()
                    break   

     cv.destroyAllWindows()

Upvotes: 1

Related Questions