import os, sys

# Import the Gamera core and initialize it
import PIL.Image as pi
import gamera.core as gc
from gamera.plugins import string_io
from gamera.toolkits.background_estimation.plugins import background_estimation

#for infile in sys.argv[1:]:
try:
    img = pi.open('../notes/notateme52.bmp')
except IOError:
    print("wrong format")


gc.init_gamera()

if (img.mode == "RGB"):
    imconv = img
else:
    imconv = img.convert(mode="RGB", matrix=None, dither=None, palette=0, colors=256)
imgrow, imgcol = imconv.size
imgDim = gc.Dim(imgrow, imgcol)
imgPoint = gc.Point(0,0)
byt = imconv.tobytes()
image = string_io._from_raw_string(imgPoint, imgDim, 3, 0, byt)


# The variable 'image' now is a reference to the image

# Convert the image to onebit using the default Otsu method
imggrayp = image.to_greyscale()
imggray = imggrayp.min_max_filter()


res1 = imggray.abutaleb_threshold()
res2 = imggray.bernsen_threshold()
#res3 = imggray.brink_threshold()
#res4 = imggray.gatos_threshold()
res5 = imggray.niblack_threshold()
res6 = imggray.otsu_threshold()
res7 = imggray.sauvola_threshold()
res8 = imggray.shading_subtraction()
res9 = imggray.soft_threshold()
res10 = imggray.tsai_moment_preserving_threshold()
res11 = imggray.white_rohrer_threshold()

resx = image.binarization()


# Save the result to a PNG file
imggray.save_PNG("output.png")
res1.save_PNG("res1.png")
res2.save_PNG("res2.png")
#res3.save_PNG("res3.png")
#res4.save_PNG("res4.png")
res5.save_PNG("res5.png")
res6.save_PNG("res6.png")
res7.save_PNG("res7.png")
res8.save_PNG("res8.png")
res9.save_PNG("res9.png")
res10.save_PNG("res10.png")
res11.save_PNG("res11.png")
resx.save_PNG("resx.png")