
from PIL import Image


#get variables
print	"-"	*	20

image_input	=	raw_input("Image (with extension)	to use?	")
file_input = raw_input("New	g-code file name	(with	extension)?	")
feed_rate	=	float(raw_input("Move speed LASER (in/min)? (6000mm/s)	"))
move_rate	=	float(raw_input("Move speed MOVE (in/min)? (10000mm/s)	"))
z_level	=	float(raw_input("Z level of laser focus (mm)? (140)"))
fan_speed	=	float(raw_input("Laser power (0-255)? (255)"))
dpi	=	float(raw_input("Resolution dpi? (1000 dpi)"))
start_x	=	float(raw_input("Start X? (30)"))
start_y	=	float(raw_input("Start Y? (30)"))

#setup text	file to	store	Gcode
text_work	=	open(file_input, "w")
text_work.truncate

#open	image	to file	and	convert	to black and white
image_work = Image.open(image_input).convert("1")
#image_transpose	=	image_work.transpose(Image.FLIP_TOP_BOTTOM)

fan_no = 0

text_work.write("G28 G90 M107 M83\n")		# all home, absolute coordinates, fan off
text_work.write("G01 F10000 Z"	+	str(z_level) + "\n")		# position to focus level
text_work.write("G01 F10000 X" + str(start_x) + " Y" + str(start_y) + "\n\n")		# position to starting point

#show print area
text_work.write("G01 " + "F" + str(move_rate)	+	" X" + str(start_x-5) +                                    " Y" + str(start_y-5)	+	"\n")	
text_work.write("M106 P" + str(fan_no) + " S" + str(fan_speed) + "\n");		# black pixel -> fan on
text_work.write("G01 " + "F" + str(feed_rate)	+	" X" + str(start_x-5) +                                    " Y" + str(start_y+5 + image_work.size[0] * 25.4 / dpi )	+	"\n")
text_work.write("G01 " + "F" + str(feed_rate)	+	" X" + str(start_x+5 + image_work.size[1] * 25.4 / dpi ) + " Y" + str(start_y+5 + image_work.size[0] * 25.4 / dpi )	+	"\n")
text_work.write("G01 " + "F" + str(feed_rate)	+	" X" + str(start_x+5 + image_work.size[1] * 25.4 / dpi ) + " Y" + str(start_y-5 )	+	"\n")
text_work.write("G01 " + "F" + str(feed_rate)	+	" X" + str(start_x-5) +                                    " Y" + str(start_y-5)	+	"\n")

text_work.write("M106 P" + str(fan_no) + " S0\n");		# black pixel -> fan on

#loop	through	x	values
for	i	in range(0,	(image_work.size[0])):
		
		new_y_pos = start_y + i * 25.4 / dpi;

		text_work.write("M107\n");		# fan off
		text_work.write("G01 " + "F" + str(move_rate)	+	" X" + str(start_x-20) + " Y" + str(new_y_pos)	+	"\n")		# go to line start 
		text_work.write("G01 " + "F" + str(feed_rate)	+	" X" + str(start_x)	+	"\n")		# go to line start 

		lastPixel = image_work.getpixel((i,0))		# initialize

		#loop	through	each y (j) value at	the	x	value	(i)
		for	j	in range(0,	(image_work.size[1])):

				if (j < image_work.size[1]-1):
					pixelValue	=	image_work.getpixel((i,j+1))

				new_x_pos = start_x + (j+1) * 25.4 / dpi

				if (j == image_work.size[1]-1) or (pixelValue != lastPixel):
						if lastPixel !=	0:
								text_work.write("M106 P" + str(fan_no) + " S" + str(fan_speed) + "\n");		# black pixel -> fan on
						text_work.write("G01 " + "F" + str(feed_rate)	+	"	X" + str(new_x_pos)	+	"\n")
						text_work.write("M106 P" + str(fan_no) + " S0\n");		# white pixel -> fan off
						lastPixel = pixelValue

		text_work.write("G01 " + "F" + str(move_rate)	+	" X" + str(start_x+10+image_work.size[1] *25.4 / dpi) + " Y" + str(new_y_pos)	+	"\n")		# go to line start 

text_work.write("M106 P" + str(fan_no) + " S0\n");		# fan off
						
print	"-"	*	20
image_work.show()
					 
text_work.close()						 

