avatar

二进制文件转换为灰度图的python代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: UTF-8 -*-
import numpy
from PIL import Image
import binascii
def getMatrixfrom_bin(filename,width):
with open(filename, 'rb') as f:
content = f.read()
hexst = binascii.hexlify(content) #将二进制文件转换为十六进制字符串
fh = numpy.array([int(hexst[i:i+2],16) for i in range(0, len(hexst), 2)]) #按字节分割
rn = len(fh)/width
fh = numpy.reshape(fh[:rn*width],(-1,width)) #根据设定的宽度生成矩阵
fh = numpy.uint8(fh)
return fh
filename = r"fff.exe"
im = Image.fromarray(getMatrixfrom_bin(filename,512)) #转换为图像
im.save(filename + ".png")