Python 如何读取.img格式的图像?

Python 如何读取.img格式的图像?,python,image,numpy,python-imaging-library,Python,Image,Numpy,Python Imaging Library,我有一张.img格式的图片。图像大小为1920x1200像素。这是一个8位深度的RGB图像。我正在使用以下python代码来恢复此图像。但是,错误可能会显示图像,但图像内容不正确。我不知道我哪里做错了。有人能帮忙吗 w, h = 1920, 1200 # img image size in px # read img files and save them to png with open(file_add, 'rb') as f: # Seek backwards from en

我有一张.img格式的图片。图像大小为1920x1200像素。这是一个8位深度的RGB图像。我正在使用以下python代码来恢复此图像。但是,错误可能会显示图像,但图像内容不正确。我不知道我哪里做错了。有人能帮忙吗

w, h = 1920, 1200   # img image size in px

# read img files and save them to png
with open(file_add, 'rb') as f:
    # Seek backwards from end of file by 3 bytes per pixel
    f.seek(-w*h*3, 2)
    img = np.fromfile(f, dtype=np.uint8).reshape((h, w, 3))

# Save as PNG, and retain 8-bit resolution
PIL.Image.fromarray(img).save('result.png')

我想上传
img
文件,但是,它超过了2Mb的限制。

这篇文章似乎完成了你想要的。它使用matplotlib读取数据,但它仍然可以执行您想要的操作。

您的文件采用了某种可怕的、Microsoft设计的“复合文件二进制格式”,如下所述。我不运行Windows,因此无法解压缩它。显然有可用的工具,但我不能保证其中任何一种:

似乎有一个名为的Python模块可以读取这些内容。我安装了它,能够测试您的文件并在其中找到您的图像,如下所示:

#!/usr/bin/env python3

import olefile
import numpy as np
from PIL import Image

# Open file
ole = olefile.OleFileIO('image.img')

# Get a directory listing
ole.dumpdirectory()                                                                        

# Open image stream within file and read
stream = ole.openstream('image/__102/DataObject')
data   = stream.read()

# Define image width, height and bytes per pixel
w, h, bpp = 1920, 1200, 3
imsize    = w * h * bpp

# Check data size and image size
print(f'Data size: {len(data)}, Image size: {imsize}')

# There are 192 bytes difference, assume it is a header and take our bytes from the tail of the file
data = data[-imsize:]

# Make into Numpy array
na = np.frombuffer(data, dtype=np.uint8).reshape((h*3,w))

# Convert from interleaved by line to interleaved by plane
R = na[0::3]
G = na[1::3]
B = na[2::3]
na = np.dstack((R,G,B))

# Make into PIL Image and save, but you could equally use OpenCV or scikit-image here
Image.fromarray(na).save('result.jpg')


运行脚本的示例输出:

'Root Entry' (root) 192 bytes 
  'NonDataObjects' (stream) 26 bytes 
  'Signature' (stream) 12 bytes 
  'image' (storage) 
    '__102' (storage) 
      'DataObject' (stream) 6912192 bytes 
      'DataObjectChilds' (stream) 4 bytes 
      'DataObjectStub' (stream) 6760 bytes 
Data size: 6912192, Image size: 6912000

我从下面的代码中计算出它是一个CFBF文件。首先,如果您运行Linux/Unix
file
命令来确定文件的类型,您会得到以下结果:

file image.img
image.img: Composite Document File V2 Document, Cannot read section info
其次,如果使用
xxd
转储文件,您将看到上面链接中提到的CFBF签名字节:

xxd image.img
00000000: d0cf 11e0 a1b1 1ae1 0000 0000 0000 0000  ................

关键字:OLE文件、CFBF、复合文档文件V2 Document、IMG格式、d0cf11e0a1b1

您是否尝试过使用PIL直接加载它,例如
PIL.Image.open(file_add)
?“我想上载IMG文件,但它大于2Mb限制。”你能不能制作一个更小的文件来引起同样的问题?“然而,它不能被正确地恢复。”这是什么意思?运行代码时会发生什么,这与应该发生的有什么不同?例如,如果它成功运行,但
result.png
看起来不正确,那么定性地说,它有什么问题?
.img
并不能真正告诉您文件格式。例如,通道的顺序可以变化;或者,它可以存储单独的颜色平面,而不是将每个像素的数据存储在一起。文件源是否提供了一些文档?有几种不同类型的文档,但没有一种是针对图形图像本身的,这似乎是您所期望的。请回答您的问题,并指定这些文件中的数据格式。感谢所有建议。是的,可以使用我发布的代码打开img文件,但图像不正确@KarlKnechtel,我没有文件来源的文档。我也试着只读出一个颜色平面,但它仍然给我一个错误的图像。img文件已经上传到那里。有人可以帮助正确地恢复它吗?谢谢你的建议。我看到了这篇文章,但它不能正确地处理我的文件。我认为这适用于没有头的文件?我已经发布了我的.img文件的链接。如果可能,请尝试恢复它。谢谢你哇!!非常感谢你的帮助!那太令人印象深刻了。我真的学到了很多。你是如何确定它是CFBF的?@KarlKnechtel嗨,Karl,我加了一张便条解释我是如何计算出它是CFBF的。