image2gcode2.py


1
from PIL import Image
2
3
4
#get variables
5
print  "-"  *  20
6
7
image_input  =  raw_input("Image (with extension)  to use?  ")
8
file_input = raw_input("New  g-code file name  (with  extension)?  ")
9
feed_rate  =  float(raw_input("Move speed LASER (in/min)? (6000mm/s)  "))
10
move_rate  =  float(raw_input("Move speed MOVE (in/min)? (10000mm/s)  "))
11
z_level  =  float(raw_input("Z level of laser focus (mm)? (140)"))
12
fan_speed  =  float(raw_input("Laser power (0-255)? (255)"))
13
dpi  =  float(raw_input("Resolution dpi? (1000 dpi)"))
14
start_x  =  float(raw_input("Start X? (30)"))
15
start_y  =  float(raw_input("Start Y? (30)"))
16
17
#setup text  file to  store  Gcode
18
text_work  =  open(file_input, "w")
19
text_work.truncate
20
21
#open  image  to file  and  convert  to black and white
22
image_work = Image.open(image_input).convert("1")
23
#image_transpose  =  image_work.transpose(Image.FLIP_TOP_BOTTOM)
24
25
fan_no = 0
26
27
text_work.write("G28 G90 M107 M83\n")    # all home, absolute coordinates, fan off
28
text_work.write("G01 F10000 Z"  +  str(z_level) + "\n")    # position to focus level
29
text_work.write("G01 F10000 X" + str(start_x) + " Y" + str(start_y) + "\n\n")    # position to starting point
30
31
#show print area
32
text_work.write("G01 " + "F" + str(move_rate)  +  " X" + str(start_x-5) +                                    " Y" + str(start_y-5)  +  "\n")  
33
text_work.write("M106 P" + str(fan_no) + " S" + str(fan_speed) + "\n");    # black pixel -> fan on
34
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")
35
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")
36
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")
37
text_work.write("G01 " + "F" + str(feed_rate)  +  " X" + str(start_x-5) +                                    " Y" + str(start_y-5)  +  "\n")
38
39
text_work.write("M106 P" + str(fan_no) + " S0\n");    # black pixel -> fan on
40
41
#loop  through  x  values
42
for  i  in range(0,  (image_work.size[0])):
43
    
44
    new_y_pos = start_y + i * 25.4 / dpi;
45
46
    text_work.write("M107\n");    # fan off
47
    text_work.write("G01 " + "F" + str(move_rate)  +  " X" + str(start_x-20) + " Y" + str(new_y_pos)  +  "\n")    # go to line start 
48
    text_work.write("G01 " + "F" + str(feed_rate)  +  " X" + str(start_x)  +  "\n")    # go to line start 
49
50
    lastPixel = image_work.getpixel((i,0))    # initialize
51
52
    #loop  through  each y (j) value at  the  x  value  (i)
53
    for  j  in range(0,  (image_work.size[1])):
54
55
        if (j < image_work.size[1]-1):
56
          pixelValue  =  image_work.getpixel((i,j+1))
57
58
        new_x_pos = start_x + (j+1) * 25.4 / dpi
59
60
        if (j == image_work.size[1]-1) or (pixelValue != lastPixel):
61
            if lastPixel !=  0:
62
                text_work.write("M106 P" + str(fan_no) + " S" + str(fan_speed) + "\n");    # black pixel -> fan on
63
            text_work.write("G01 " + "F" + str(feed_rate)  +  "  X" + str(new_x_pos)  +  "\n")
64
            text_work.write("M106 P" + str(fan_no) + " S0\n");    # white pixel -> fan off
65
            lastPixel = pixelValue
66
67
    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 
68
69
text_work.write("M106 P" + str(fan_no) + " S0\n");    # fan off
70
            
71
print  "-"  *  20
72
image_work.show()
73
           
74
text_work.close()