Examples of reading raw binary images and extracting statistical information in Python

Time:2021-4-19

Using Python language to read binary image files and extract non-zero data statistics (for example: Max, min, skewness and kurtosis)

Python novice, notes less, welcome to teach


import struct
import math
import numpy
import scipy.stats
 
filename = input('enter file name')
f = open(filename, 'rb')
f.seek(0, 0)
 
c = 0
numOfZero = 0
s = 0
num = []
m = 0
 
while True:
  temp = f.read(4)
  if len(temp) == 0:
    break
  else:
    c = c + 1
print(c)
print(numOfZero)
 
sum = 0
squSum = 0
min = 2000
max = 0
list = []
num = []
f.seek(0, 0)
 
for i in range(0, c):
  a = f.read(4)
  b = struct.unpack('<f', a)
  list.append(b[0])
  if list[i] == 0:
    numOfZero = numOfZero + 1
  else:
    num.append(b[0])
  if list[i] > max:
    max = list[i]
  if list[i] < min and list[i] != 0:
    min = list[i]
  sum = sum + list[i]
 
stan_Dev = numpy.std(num)
median = numpy.median(num)
sk = scipy.stats.skew(num)
ku = scipy.stats.kurtosis(num)
 
print('numOfZero = ', numOfZero)
print('sum = ', sum)
print('meanValue = ',sum / (c - numOfZero))
print('maxValue = ', max)
print('minValue = ', min)
print('median = ', median)
print('stdev = ', stan_Dev)
print('skewness = ', sk)
print('kurtosis = ', ku)
 
f.close()

The above example of reading raw binary pictures and extracting statistical information by Python is the whole content shared by Xiaobian. I hope it can give you a reference and support developer.