如何重塑png图像略读.imread

2024-09-26 22:49:33 发布

您现在位置:Python中文网/ 问答频道 /正文

然后我读了一些jpg文件,这样

image = imread('aa.jpg')

结果,我得到了一个从1到255的数据帧

我可以这样调整大小:

^{pr2}$

但后来我用png做同样的思考,结果不理想。在

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

奇怪的图像 enter image description here

但是imshow(image)

enter image description here


Tags: 文件数据imagepngwithchannelarrayreturns
2条回答

默认情况下,cv2.imread读取3个通道的图像,而不是4个通道。传递参数cv.IMREAD_UNCHANGED以读取PNG文件,然后尝试调整其大小,如下面的代码所示。在

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

或者是你的代码有问题。在

这里有一个免费的图片可以试试:https://pixabay.com/vectors/copyright-free-creative-commons-98566/

可能您对libpng有问题,请检查以下答案:libpng warning: iCCP: known incorrect sRGB profile

检查一下这个简单的代码,它可以处理PNG图像。在

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

相关问题 更多 >

    热门问题