Idx To Image

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import sys

import numpy as np
from PIL import Image

fileName = ''

if len(sys.argv) is 2:
fileName = sys.argv[1]

elif len(sys.argv) is 1:
fileName = input('file path: ')

else:
print('Usage:\n' + sys.argv[0] + '\n' + sys.argv[0] + ' <idx file path>\n')
exit()

count = 0
magicNumber = 0
numImages = 0
width = 0
height = 0

images = None

with open(fileName, 'rb') as f:


magicNumber = int.from_bytes(f.read(4), byteorder = 'big')
numImages = int.from_bytes(f.read(4), byteorder = 'big')
width = int.from_bytes(f.read(4), byteorder = 'big')
height = int.from_bytes(f.read(4), byteorder = 'big')
images = np.zeros((numImages, width, height, 3), dtype=np.uint8)
for itImages in range(0, numImages):
for itX in range(0, width):
for itY in range(0, height):
v = int.from_bytes(f.read(1), byteorder = 'big')
images[itImages][itX][itY] = [v,v,v]

for image in images:


image = Image.fromarray(np.asarray(image))
image.show()
input('Press Enter for next image...')

You might also like