#this program converts .png (24 bits) to .mif (black & white)
#
#Elaborado por
# Elizabeth Fonseca Chavez
# Mario Alfredo Ibarra Carrillo
# Septiembre 2018
import sys
from PIL import Image
import numpy

#checking for the number of arguments
if len(sys.argv)!=3 :
	print("Two arguments were expected");
	sys.exit()

#checking the origin's file format
origin=sys.argv[1]
if not('.png' in origin):
	print(".png file format expected as first argument")
	sys.exit

#checking the destination's file format
destiny=sys.argv[2]
if not('.mif' in destiny):
	print(".mif file format expected as second argument")
	sys.exit

#Opening the image file
image=Image.open(origin).convert('L').resize((20,20),Image.ANTIALIAS)
image=numpy.array(image)
[rows,cols]=image.shape

#Writing the .mif file
f=open(destiny,"wt")

f.write("DEPTH = "+str (rows*cols) +";\n")
f.write("WIDTH = "+str (8) +";\n")

f.write("ADDRESS_RADIX = DEC;\n")
f.write("DATA_RADIX = DEC;\n")

f.write("CONTENT\n")
f.write("BEGIN\n")

indx=0
for k in image.reshape(rows*cols):
	f.write(str(indx) + " : " + str(k) + ";\n")
	indx+=1

f.write("END;\n")
f.close

print("Conversion finished")






