bitmap_plot.py

Sat, 07 Nov 2015 13:35:32 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:35:32 +0100
changeset 3
a519e3ac3849
parent 1
0c9798d91427
permissions
-rwxr-xr-x

added parser for polygon elements

1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
2
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
3 ENGRAVE_SPEED = 20 * 60 # mm/min
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
4 TRAVEL_SPEED = 130 * 60
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
5 E_FACTOR = 0.1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
6 INVERT_PALETTE = True
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
7
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
8 DPI = 300
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
9 GREY_THRESHOLD = 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
10 CHANGE_DIRECTION = True
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
11
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
12
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
13 # DO NOT CHANGE WORLD's RULES!
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
14 INCH = 25.4 # mm
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
15 MM_PIXEL = round(INCH / DPI, 4)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
16 STEPS_PIXEL = MM_PIXEL * 80 # mine is 80 steps/mm on XY
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
17 print "Resolution: %f mm per pixel" % MM_PIXEL
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
18 print "Steps per pixel (needs to be > 5, otherwise marlin joins lines): %f" % STEPS_PIXEL
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
19
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
20 from PIL import Image
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
21 import sys
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
22
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
23 def pixel_to_bit(pixel, threshold=128):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
24 """Convert the pixel value to a bit."""
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
25 # some really weird stuff here ;-P
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
26
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
27 # RGB to greyscale
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
28 #print pixel
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
29 #print type(pixel)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
30 if isinstance(pixel, tuple):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
31 #rgb
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
32 pixel = pixel[0]*0.2989 + pixel[1]*0.5870 + pixel[2]*0.1140
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
33 threshold = 128
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
34 if pixel > threshold:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
35 return 1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
36 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
37 return 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
38
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
39
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
40 # color palette
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
41 if (pixel > 0):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
42 return 1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
43 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
44 return 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
45
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
46 #
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
47 # Open the image file and get the basic information about it.
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
48 #
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
49 try:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
50 im = Image.open(sys.argv[1])
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
51 except:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
52 print "Unable to open %s" % sys.argv[1]
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
53 exit(-1)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
54
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
55 print "format: %s mode: %s palette: %s" % (im.format,im.mode,im.palette)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
56 width,height = im.size
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
57 print "The image is %d x %d" % im.size
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
58
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
59 pix = im.load()
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
60
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
61
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
62 fo = open(sys.argv[1] + ".g", "w")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
63 #fo.write("G10 P1 X-30.0 Y1.5 Z0.0 R0 S0 ; Laser tool offset\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
64 #fo.write("M218 T1 X.30.0 Y1.5 ; Laser tool offset\")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
65 #fo.write("G28 X Y ; Home position\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
66 # G90 = absolute positioning, G91 = relative
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
67 #fo.write("M83 ; Set extruder (laser) to relative mode\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
68 fo.write("""
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
69 ; Filename: %s
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
70 ; GCode generated by bitplotter one-night-quick-hack script (marlin code flavour)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
71
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
72 G21 ; Metric
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
73 ; We assume Z is in focus height and laser head is focus at bottom left of image!
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
74 G92 X0 Y0 E0; set zero position - new origin
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
75 G90 ; absolute positioning
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
76 M82 ; Set extruder (laser) to absolute positioning
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
77 M201 X1000 Y1000 E500 ; Set acceleration
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
78 M203 X1000 Y1000 Z4 E10 ; Set max feedrate
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
79 M209 S0 ; disable firmware retraction, we dont want to burn holes...
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
80 M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
81 M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
82 G0 X0 Y0 F%d ; Set moving speed TRAVEL_SPEED
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
83 G1 X0 Y0 F%d ; Set linear engraving speed ENGRAVE_SPEED
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
84
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
85 """ % (sys.argv[1], TRAVEL_SPEED, ENGRAVE_SPEED) )
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
86
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
87 fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm" % (
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
88 im.size[0], im.size[1], DPI, im.size[0]*MM_PIXEL, im.size[1]*MM_PIXEL) )
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
89
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
90 INVERT_Y = MM_PIXEL * (im.size[1] -1) * (-1)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
91
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
92 DIR = 1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
93 for X in range(im.size[0]):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
94 fo.write("; X=%d printing row: direction %i\n" % (X, DIR))
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
95 fo.write("G92 E0\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
96 E = 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
97 last_bit = 1 # we engrave on black pixel = 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
98 START_Y = 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
99 if DIR > 0:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
100 range_start = 0
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
101 range_stop = im.size[1]
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
102 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
103 range_start = im.size[1] -1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
104 range_stop = -1
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
105
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
106 for Y in range(range_start, range_stop, DIR):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
107 YMM = abs((Y * MM_PIXEL) + INVERT_Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
108 XMM = X * MM_PIXEL
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
109 #print "X %d Y %d" % (X, Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
110 bit = pixel_to_bit(pix[X, Y], GREY_THRESHOLD)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
111 if last_bit == bit:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
112 if bit == 1:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
113 # nothing to do,
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
114 continue
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
115 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
116 # are we at the end of Y range?
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
117 #print Y
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
118 if (Y == (im.size[1] - 1)) or (Y == 0):
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
119 # draw line
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
120 if DIR > 0:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
121 E = E + MM_PIXEL * (Y - START_Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
122 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
123 E = E + MM_PIXEL * (START_Y - Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
124 fo.write("G1 X%.4f Y%.4f E%.4f F%d\n" % (XMM, YMM, E * E_FACTOR, ENGRAVE_SPEED))
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
125 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
126 # bit value has changed!
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
127 if bit == 0:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
128 # jump to start of line to write
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
129 START_Y = Y
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
130 fo.write("G0 X%.4f Y%.4f F%d\n" % (XMM, YMM, TRAVEL_SPEED))
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
131 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
132 # end of line to write
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
133 if DIR > 0:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
134 E = E + MM_PIXEL * (Y - START_Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
135 else:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
136 E = E + MM_PIXEL * (START_Y - Y)
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
137 fo.write("G1 X%.4f Y%.4f E%.4f F%d\n" % (XMM, YMM, E * E_FACTOR, ENGRAVE_SPEED))
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
138 last_bit = bit
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
139 if CHANGE_DIRECTION:
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
140 DIR = DIR * (-1) # change y direction on every X
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
141
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
142
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
143 fo.write("M571 S0 E0\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
144 fo.write("M501 ; undo all settings made\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
145 #fo.write("G28 X0 Y0 ; Home position\n")
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
146
0c9798d91427 first tests with bitmap plotting, working :)
mbayer
parents:
diff changeset
147 fo.close()

mercurial