加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bin_to_RGB.py 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
Night_Breeze 提交于 2023-05-22 10:47 . update bin_to_RGB.py.
# Open the binary file and read it into a string
import os
from PIL import Image
# bin文件路径
filenames = os.listdir(r'F:\USTC-TK2016-master\normal-traffic\bin')
i=1
for filename in filenames:
# bin文件绝对路径
with open('F:\\USTC-TK2016-master\\normal-traffic\\bin\\'+filename, 'rb') as f:
#with open('F:\\USTC-TK2016-master\\bin\\110.bin', 'rb') as f:
data = f.read()
try:
print(filename)
# Create a list of pixels with the desired colors
pixels = []
for b in data:
if b >= 32 and b <= 127:
# Printable ASCII bytes are blue
pixels.append((0, 0, 255))
elif b >= 1 and b <= 31:
# Control bytes are green
pixels.append((0, 255, 0))
elif b >= 128 and b <= 255:
# Extend ASCII bytes are red
pixels.append((255, 0, 0))
elif b == 0:
# 0x00 is black
pixels.append((0, 0, 0))
elif b == 255:
# 0xFF is white
pixels.append((255, 255, 255))
image = Image.new('RGBA', (500, 1000))
# Put the pixel data into the image
image.putdata(pixels)
# 图片保存路径
image.save('F:\\USTC-TK2016-master\\normal-traffic\\bin\\' + str(i) + '.png')
i+=1
except:
data = data[:500*1000]
pixels = []
for b in data:
if b >= 32 and b <= 127:
# Printable ASCII bytes are blue
pixels.append((0, 0, 255))
elif b >= 1 and b <= 31:
# Control bytes are green
pixels.append((0, 255, 0))
elif b >= 128 and b <= 255:
# Extend ASCII bytes are red
pixels.append((255, 0, 0))
elif b == 0:
# 0x00 is black
pixels.append((0, 0, 0))
elif b == 255:
# 0xFF is white
pixels.append((255, 255, 255))
image = Image.new('RGBA', (500, 1000))
# Put the pixel data into the image 将像素数据放入图片
image.putdata(pixels)
# 图片保存路径
image.save('F:\\USTC-TK2016-master\\normal-traffic\\bin\\' + str(i) + '.png')
i+=1
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化