简体   繁体   中英

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 在此处输入图片说明

But imshow(image)

在此处输入图片说明

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()

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM