python - 如何读取 .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 的限制。
解决方案
您的文件采用 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
推荐阅读
- java - 在 Android Studio 中将 Double 转换为颜色整数
- swift - 登录后快速加载仪表板的用户数据
- postgresql - java - 如何使用hibernate(springData)将java类型转换为postgres的域?
- string-concatenation - 在 String.Join() 之后,包含英语和希伯来字母的字符串变得混乱 - C# .NET
- python - 在 python 中使用 psycopg2 在参数化查询中输入字符串作为参数时出现问题
- r - omp 导致 R 崩溃
- html - 图像在移动设备(iPhone)上不保留纵横比
- javascript - div间滑动动画
- python - 如何处理普通python程序上的程序关闭事件?
- python - Heliohost.org 上的 Django 应用程序部署问题