Sunday, July 5, 2020

python binary write/read example

import numpy as np
import json

def read32(bs):
    data = bs.read(4)
    return int.from_bytes(data, byteorder='big', signed=False)

def write32(bs, int_data):
    bs.write(int_data.to_bytes(4, byteorder='big', signed=False))

def writeTest():
    trainImageFile = open('eyedata_set/train-images.ubyte', 'wb')
    count = 4
    width = 1024
    height = 768

    write32(trainImageFile, count)
    write32(trainImageFile, width)
    write32(trainImageFile, height)
   
    trainImageFile.close()
   
    print('[DONE] write test')
   
def readTest():
    trainImageFile = open('eyedata_set/train-images.ubyte', 'rb')
    count = read32(trainImageFile)
    width  = read32(trainImageFile)
    height = read32(trainImageFile)
   
    trainImageFile.close()
       
    print('[DONE] read test')
    print('{}, {}, {}'.format(count, width, height))


No comments:

Post a Comment

Task in UnrealEngine

 https://www.youtube.com/watch?v=1lBadANnJaw