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

2024-04-26 01:26:05 发布

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

我有一张.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限制


Tags: and代码图像内容imgpngsaveas
2条回答

您的文件采用了某种可怕的Microsoft设计的“复合文件二进制格式”,如here所述。我不运行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')

enter image description here


运行脚本的示例输出:

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

Thispost似乎正在实现您所寻找的目标。它改为使用matplotlib读取数据,但它仍然可以执行您想要的操作

相关问题 更多 >