Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中读取PFM格式_Python_Image Processing - Fatal编程技术网

在python中读取PFM格式

在python中读取PFM格式,python,image-processing,Python,Image Processing,我想用python阅读pfm格式的图像。我尝试使用imageio.read,但它抛出了一个错误。我能有什么建议吗 img=imageio.imread'image.pfm'我对Python一点也不熟悉,但这里有一些阅读pfm可移植浮点映射文件的建议 选择1 ImageIO文档建议您可以下载并使用免费图像阅读器 选择2 我自己在下面拼凑了一个简单的阅读器,它似乎可以很好地处理我在网上找到的几个用ImageMagick生成的示例图像。因为我不会讲Python,所以它可能包含低效或不良实践 #!/us

我想用python阅读pfm格式的图像。我尝试使用imageio.read,但它抛出了一个错误。我能有什么建议吗


img=imageio.imread'image.pfm'

我对Python一点也不熟悉,但这里有一些阅读pfm可移植浮点映射文件的建议

选择1

ImageIO文档建议您可以下载并使用免费图像阅读器

选择2

我自己在下面拼凑了一个简单的阅读器,它似乎可以很好地处理我在网上找到的几个用ImageMagick生成的示例图像。因为我不会讲Python,所以它可能包含低效或不良实践

#!/usr/local/bin/python3
import sys
import re
from struct import *

# Enable/disable debug output
debug = True

with open("image.pfm","rb") as f:
    # Line 1: PF=>RGB (3 channels), Pf=>Greyscale (1 channel)
    type=f.readline().decode('latin-1')
    if "PF" in type:
        channels=3
    elif "Pf" in type:
        channels=1
    else:
        print("ERROR: Not a valid PFM file",file=sys.stderr)
        sys.exit(1)
    if(debug):
        print("DEBUG: channels={0}".format(channels))

    # Line 2: width height
    line=f.readline().decode('latin-1')
    width,height=re.findall('\d+',line)
    width=int(width)
    height=int(height)
    if(debug):
        print("DEBUG: width={0}, height={1}".format(width,height))

    # Line 3: +ve number means big endian, negative means little endian
    line=f.readline().decode('latin-1')
    BigEndian=True
    if "-" in line:
        BigEndian=False
    if(debug):
        print("DEBUG: BigEndian={0}".format(BigEndian))

    # Slurp all binary data
    samples = width*height*channels;
    buffer  = f.read(samples*4)

    # Unpack floats with appropriate endianness
    if BigEndian:
        fmt=">"
    else:
        fmt="<"
    fmt= fmt + str(samples) + "f"
    img = unpack(fmt,buffer)
我就这么做。为便于使用,可以将其转换为numpy阵列

img = numpy.asarray(img)

这是一种非常简单的阅读格式——这里有描述,这里有Python二进制访问,谢谢。imageio.imread'image.pfm'难道不可能吗?
img = imageio.imread('image.pfm')
img = numpy.asarray(img)