首页 > 解决方案 > 如何读取 .img 格式的图像?

问题描述

我有一个 .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 的限制。

标签: pythonimagenumpypython-imaging-library

解决方案


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

https://openrpmsgfile.com/cfbf.html

http://fileformats.archiveteam.org/wiki/Microsoft_Compound_File

似乎有一个名为olefile的 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/Unixfile命令来确定文件的类型,你会得到:

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文件,IMG格式,d0cf11e0a1b1


推荐阅读