/
BYTEStoIMG.py
40 lines (39 loc) · 1014 Bytes
/
BYTEStoIMG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pandas as pd
import numpy,scipy.misc, os, array
from scipy.misc import imsave
x=pd.read_csv('trainLabels.csv')
for i in range (0,2700):
f=open("train/"+str(x['Id'][i])+".bytes",'rb')
ln=os.path.getsize("train/"+str(x['Id'][i])+".bytes")
kbs=ln/1024
width= 1024
if(kbs>0 and kbs<10):
width= 32
elif(kbs>=10 and ln<30):
width= 64
elif(kbs>=30 and ln<60):
width= 128
elif(kbs>=60 and ln<100):
width= 256
elif(kbs>=100 and ln<200):
width= 384
elif(kbs>=200 and ln<500):
width= 512
elif(kbs>=500 and ln<1000):
width= 768
#width=int(ln**0.5)
rem= ln%width
a=array.array("B")
a.fromfile(f,ln-rem)
f.close()
print(a)
print(a.size)
g=numpy.reshape(a,(int(len(a)/width),width))
print(g)
print(g.shape)
g=numpy.uint8(g)
print(g)
print(g.shape)
#g=numpy.uint8(g)
#imsave("bytes_img/"+str(x['Id'][i])+'.png',g)
imsave(str(x['Id'][i])+'.png',g)