Python 如何将二进制dat文件(.dat)保存为png或jpg格式?
我有一个二进制文件格式.dat。我想将其转换为png或jpg格式,我尝试使用Python 如何将二进制dat文件(.dat)保存为png或jpg格式?,python,image,binary,Python,Image,Binary,我有一个二进制文件格式.dat。我想将其转换为png或jpg格式,我尝试使用numpy.loadtxt()对其进行解码以获得数组列表,然后将其保存为png或jpg格式。但当我执行它时,它会出错 文件“C:\Users\SETIKO\Desktop\test\test6 savedattopng.py”,第13行,在 arr=np.loadtxt(f) loadtxt中的文件“C:\Users\SETIKO\AppData\Local\Programs\Python\35\lib\site pac
numpy.loadtxt()
对其进行解码以获得数组列表,然后将其保存为png或jpg格式。但当我执行它时,它会出错
文件“C:\Users\SETIKO\Desktop\test\test6 savedattopng.py”,第13行,在
arr=np.loadtxt(f)loadtxt中的文件“C:\Users\SETIKO\AppData\Local\Programs\Python\35\lib\site packages\numpy\lib\npyio.py”,第1101行 对于读取数据中的x(\u loadtxt\u chunksize):
读取数据中的文件“C:\Users\SETIKO\AppData\Local\Programs\Python\35\lib\site packages\numpy\lib\npyio.py”,第1028行 items=[zip(转换器,val)中(转换,val)的转换(val)]
文件“C:\Users\SETIKO\AppData\Local\Programs\Python\Python35\lib\site packages\numpy\lib\npyio.py”,第1028行,在 items=[zip(转换器,val)中(转换,val)的转换(val)]
floatconv中的文件“C:\Users\SETIKO\AppData\Local\Programs\Python\35\lib\site packages\numpy\lib\npyio.py”,第746行 返回浮动(x) 这是我使用的完整代码
import numpy as np
f = open('gt.dat', 'rb')
arr = np.loadtxt(f)
我的代码中缺少什么吗?或者可能有人有其他想法而不必使用numpy.loadtxt()
这里是我使用的二进制文件:
最简单的方法是,您可以读取文件并将其制作成PIL/枕头图像,如下所示:
import numpy as np
from PIL import Image
# Read file into Numpy array as uint8
na = np.fromfile('GT.dat', dtype=np.uint8)
# Reshape to 5292 px wide, 744 px tall and RGBA, i.e. 4 bytes/per pixel
na = np.reshape(na,(744,5292,4))
# Drop the alpha channel, retaining just RGB
na = na[:,:,:3]
# Convert to PIL/Pillow Image - whilst also brightening because values are all <=5
im = Image.fromarray(na*50,'RGB')
# Save
im.save('result.png')
更新的答案 看起来所有的数据实际上都在红色通道中,而绿色、蓝色或阿尔法通道中没有任何数据。因此,我们将丢弃这些,然后,由于红色通道中的值都是小整数,我们将创建一个调色板图像,其中这些整数是我们可以轻松操作的调色板的索引
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Read file into Numpy array as uint8
na = np.fromfile('GT.dat', dtype=np.uint8)
# Reshape to 5292 px wide, 744 px tall and RGBA, i.e. 4 bytes/per pixel
na = np.reshape(na,(744,5292,4))
# Construct a palette
palette = [
0, 0, 0, # 0 - black
255, 0, 0, # 1 - red
0, 255, 0, # 2 - green
0, 0, 255 # 3 - blue
]
# There must be exactly 256 RGB colours, so pad palette to length 768 with zeroes
palette += (768-len(palette))*[0]
# Drop everything except the Red channel which contains all the data
na = na[:,:,0]
# Convert to single channel PIL/Pillow Image
im = Image.fromarray(na,'L')
# Put our palette into image
im.putpalette(palette)
# Save
im.save('result.png')
loadtxt()将文本文件转换为数组(默认为浮点)。但很可能您有一个二进制文件(而.dat不是标准格式)。和loadtxt()在文件中找不到任何看起来像数字的文本。您知道图像的尺寸(以像素为单位)吗?我的意思是它是5000x3000像素吗?还是怎样你知道它是彩色的还是灰度的吗?@AlainMerigot你知道将数字作为二进制文件查找的代码吗?@MarkSetchell是的图像的尺寸是5292x744像素,它是彩色图像哇,这非常有用,非常感谢。但我还有一个问题,就像你说的,这些值是我添加的所有代码,用于制作托盘图像,其中红色通道数据用作调色板的索引,你可以很容易地控制。请再看一眼。我能再问一个问题吗?如果我颠倒了前面的问题,我有一个.png文件,并希望将其转换为.dat,就像gt.dat文件一样,我可以使用上面的代码思想吗?与其回答两个问题让人困惑不解,最好还是问一个新的问题-问题(和答案)无论如何都是免费的。如果您这样做,请单击新问题下的
共享
,复制它显示给您的链接,然后粘贴一条评论,将该链接与此问题联系起来,以便将两者“结合起来”。另外,如果你在这里发表评论,我会得到通知,到时候可能会回复。
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Read file into Numpy array as uint8
na = np.fromfile('GT.dat', dtype=np.uint8)
# Reshape to 5292 px wide, 744 px tall and RGBA, i.e. 4 bytes/per pixel
na = np.reshape(na,(744,5292,4))
# Construct a palette
palette = [
0, 0, 0, # 0 - black
255, 0, 0, # 1 - red
0, 255, 0, # 2 - green
0, 0, 255 # 3 - blue
]
# There must be exactly 256 RGB colours, so pad palette to length 768 with zeroes
palette += (768-len(palette))*[0]
# Drop everything except the Red channel which contains all the data
na = na[:,:,0]
# Convert to single channel PIL/Pillow Image
im = Image.fromarray(na,'L')
# Put our palette into image
im.putpalette(palette)
# Save
im.save('result.png')