1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import math, gd, random |
---|
4 | |
---|
5 | # Tiny image class to make examples short and readable |
---|
6 | class Image(gd.image): |
---|
7 | def __new__(args, truecolor = False): |
---|
8 | gd.gdMaxColors = 256 * 256 * 256 |
---|
9 | if truecolor: |
---|
10 | return gd.image.__new__(args, True) |
---|
11 | else: |
---|
12 | return gd.image.__new__(args) |
---|
13 | def getGray(self, x, y): |
---|
14 | p = self.getPixel((x, y)) |
---|
15 | c = self.colorComponents(p)[0] / 255.0 |
---|
16 | return c |
---|
17 | def getRgb(self, x, y): |
---|
18 | p = self.getPixel((x, y)) |
---|
19 | rgb = self.colorComponents(p) |
---|
20 | return [rgb[0] / 255.0, rgb[1] / 255.0, rgb[2] / 255.0] |
---|
21 | def setGray(self, x, y, t): |
---|
22 | p = (int)(t * 255.999) |
---|
23 | c = self.colorResolve((p, p, p)) |
---|
24 | self.setPixel((x, y), c) |
---|
25 | def setRgb(self, x, y, r, g, b): |
---|
26 | r = (int)(r * 255.999) |
---|
27 | g = (int)(g * 255.999) |
---|
28 | b = (int)(b * 255.999) |
---|
29 | c = self.colorResolve((r, g, b)) |
---|
30 | self.setPixel((x, y), c) |
---|
31 | |
---|
32 | # Manipulate gamma values |
---|
33 | class Gamma: |
---|
34 | def CtoI(x): |
---|
35 | return math.pow(x, 2.2) |
---|
36 | def ItoC(x): |
---|
37 | return math.pow(x, 1 / 2.2) |
---|
38 | CtoI = staticmethod(CtoI) |
---|
39 | ItoC = staticmethod(ItoC) |
---|
40 | |
---|
41 | # Load the 256x256 grayscale Lenna image |
---|
42 | lenna256bw = Image("lenna256bw.png") |
---|
43 | |
---|
44 | # Create a 32x256 grayscale gradient |
---|
45 | gradient256bw = Image((32, 256)) |
---|
46 | for x in range(32): |
---|
47 | for y in range(256): |
---|
48 | gradient256bw.setGray(x, 255 - y, y / 255.) |
---|
49 | gradient256bw.writePng("gradient256bw.png") |
---|
50 | |
---|
51 | # Output 1.1.1: 50% threshold |
---|
52 | # Output 1.1.2: 40% threshold |
---|
53 | # Output 1.1.3: 60% threshold |
---|
54 | def test11x(src, threshold, name): |
---|
55 | (w, h) = src.size() |
---|
56 | dest = Image((w, h)) |
---|
57 | for y in range(h): |
---|
58 | for x in range(w): |
---|
59 | c = src.getGray(x, y) > threshold |
---|
60 | dest.setGray(x, y, c) |
---|
61 | dest.writePng(name) |
---|
62 | |
---|
63 | test11x(lenna256bw, 0.5, "out1-1-1.png") |
---|
64 | test11x(lenna256bw, 0.4, "out1-1-2.png") |
---|
65 | test11x(lenna256bw, 0.6, "out1-1-3.png") |
---|
66 | test11x(gradient256bw, 0.5, "grad1-1-1.png") |
---|
67 | test11x(gradient256bw, 0.4, "grad1-1-2.png") |
---|
68 | test11x(gradient256bw, 0.6, "grad1-1-3.png") |
---|
69 | |
---|
70 | # Output 1.2.1: 3-colour threshold |
---|
71 | # Output 1.2.2: 5-colour threshold |
---|
72 | def test12x(src, colors, name): |
---|
73 | (w, h) = src.size() |
---|
74 | dest = Image((w, h)) |
---|
75 | p = -0.0001 + colors |
---|
76 | q = colors - 1 |
---|
77 | for y in range(h): |
---|
78 | for x in range(w): |
---|
79 | c = src.getGray(x, y) |
---|
80 | c = math.floor(c * p) / q |
---|
81 | dest.setGray(x, y, c) |
---|
82 | dest.writePng(name) |
---|
83 | |
---|
84 | test12x(lenna256bw, 3, "out1-2-1.png") |
---|
85 | test12x(lenna256bw, 5, "out1-2-2.png") |
---|
86 | test12x(gradient256bw, 3, "grad1-2-1.png") |
---|
87 | test12x(gradient256bw, 5, "grad1-2-2.png") |
---|
88 | |
---|
89 | # Pattern 2.1.1: a 50% halftone pattern with various block sizes |
---|
90 | dest = Image((320, 80)) |
---|
91 | for x in range(320): |
---|
92 | d = 8 >> (x / 80) |
---|
93 | for y in range(80): |
---|
94 | c = (x / d + y / d) & 1 |
---|
95 | dest.setGray(x, y, c) |
---|
96 | dest.writePng("pat2-1-1.png") |
---|
97 | |
---|
98 | # Pattern 2.1.2: 25% and 75% halftone patterns with various block sizes |
---|
99 | dest = Image((320, 80)) |
---|
100 | for x in range(320): |
---|
101 | d = 8 >> (x / 80) |
---|
102 | for y in range(40): |
---|
103 | c = ((x / d + y / d) & 1) or (y / d & 1) |
---|
104 | dest.setGray(x, y, c) |
---|
105 | for y in range(40, 80): |
---|
106 | c = ((x / d + y / d) & 1) and (y / d & 1) |
---|
107 | dest.setGray(x, y, c) |
---|
108 | dest.writePng("pat2-1-2.png") |
---|
109 | |
---|
110 | # Output 2.1.1: 20/40/60/80% threshold with 25/50/75% patterns inbetween: |
---|
111 | def test211(src, name): |
---|
112 | (w, h) = src.size() |
---|
113 | dest = Image((w, h)) |
---|
114 | for y in range(h): |
---|
115 | for x in range(w): |
---|
116 | c = src.getGray(x, y) |
---|
117 | if c < 0.2: |
---|
118 | c = 0. |
---|
119 | elif c < 0.4: |
---|
120 | c = ((x + y) & 1) and (y & 1) |
---|
121 | elif c < 0.6: |
---|
122 | c = (x + y) & 1 |
---|
123 | elif c < 0.8: |
---|
124 | c = ((x + y) & 1) or (y & 1) |
---|
125 | else: |
---|
126 | c = 1. |
---|
127 | dest.setGray(x, y, c) |
---|
128 | dest.writePng(name) |
---|
129 | |
---|
130 | test211(lenna256bw, "out2-1-1.png") |
---|
131 | test211(gradient256bw, "grad2-1-1.png") |
---|
132 | |
---|
133 | # Pattern 2.2.1: vertical, mixed and horizontal black-white halftones |
---|
134 | dest = Image((240, 80)) |
---|
135 | for y in range(80): |
---|
136 | for x in range(80): |
---|
137 | c = x & 1 |
---|
138 | dest.setGray(x, y, c) |
---|
139 | for x in range(80, 160): |
---|
140 | c = (x / d + y / d) & 1 |
---|
141 | dest.setGray(x, y, c) |
---|
142 | for x in range(160, 240): |
---|
143 | c = y & 1 |
---|
144 | dest.setGray(x, y, c) |
---|
145 | dest.writePng("pat2-2-1.png") |
---|
146 | |
---|
147 | # Pattern 2.2.2: two different 25% patterns |
---|
148 | dest = Image((320, 80)) |
---|
149 | for y in range(80): |
---|
150 | for x in range(80): |
---|
151 | c = (x / 2 & 1) and (y / 2 & 1) |
---|
152 | dest.setGray(x, y, c) |
---|
153 | for x in range(80, 160): |
---|
154 | c = (x & 1) and (y & 1) |
---|
155 | dest.setGray(x, y, c) |
---|
156 | for x in range(160, 240): |
---|
157 | c = (x & 1) and ((y + x / 2) & 1) |
---|
158 | dest.setGray(x, y, c) |
---|
159 | for x in range(240, 320): |
---|
160 | c = (x / 2 & 1) and ((y / 2 + x / 4) & 1) |
---|
161 | dest.setGray(x, y, c) |
---|
162 | dest.writePng("pat2-2-2.png") |
---|
163 | |
---|
164 | # Output 2.3.1: 4x4 Bayer dithering |
---|
165 | # Output 2.3.2: 4x4 cluster dot |
---|
166 | # Output 2.3.3: 5x3 line dithering |
---|
167 | def test23x(src, mat, name): |
---|
168 | (w, h) = src.size() |
---|
169 | dest = Image((w, h)) |
---|
170 | dx = len(mat[0]) |
---|
171 | dy = len(mat) |
---|
172 | for y in range(h): |
---|
173 | for x in range(w): |
---|
174 | c = src.getGray(x, y) |
---|
175 | threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1) |
---|
176 | c = c > threshold |
---|
177 | dest.setGray(x, y, c) |
---|
178 | dest.writePng(name) |
---|
179 | |
---|
180 | mat = [[ 0, 8, 3, 11], |
---|
181 | [ 15, 4, 12, 7], |
---|
182 | [ 2, 10, 1, 9], |
---|
183 | [ 13, 6, 14, 5]] |
---|
184 | test23x(lenna256bw, mat, "out2-3-1.png") |
---|
185 | test23x(gradient256bw, mat, "grad2-3-1.png") |
---|
186 | |
---|
187 | mat = [[ 12, 5, 6, 13], |
---|
188 | [ 4, 0, 1, 7], |
---|
189 | [ 11, 3, 2, 8], |
---|
190 | [ 15, 10, 9, 14]] |
---|
191 | test23x(lenna256bw, mat, "out2-3-2.png") |
---|
192 | test23x(gradient256bw, mat, "grad2-3-2.png") |
---|
193 | |
---|
194 | mat = [[ 13, 7, 0, 4, 10], |
---|
195 | [ 9, 3, 1, 8, 14], |
---|
196 | [ 11, 5, 2, 6, 12],] |
---|
197 | test23x(lenna256bw, mat, "out2-3-3.png") |
---|
198 | test23x(gradient256bw, mat, "grad2-3-3.png") |
---|
199 | |
---|
200 | # Output 2.4.1: uniform random dithering |
---|
201 | def test241(src, name): |
---|
202 | random.seed(0) |
---|
203 | (w, h) = src.size() |
---|
204 | dest = Image((w, h)) |
---|
205 | for y in range(h): |
---|
206 | for x in range(w): |
---|
207 | c = src.getGray(x, y) |
---|
208 | d = c > random.random() |
---|
209 | dest.setGray(x, y, d) |
---|
210 | dest.writePng(name) |
---|
211 | |
---|
212 | test241(lenna256bw, "out2-4-1.png") |
---|
213 | test241(gradient256bw, "grad2-4-1.png") |
---|
214 | |
---|
215 | # Output 2.4.2: random dithering |
---|
216 | def test242(src, name): |
---|
217 | random.seed(0) |
---|
218 | (w, h) = src.size() |
---|
219 | dest = Image((w, h)) |
---|
220 | for y in range(h): |
---|
221 | for x in range(w): |
---|
222 | c = src.getGray(x, y) |
---|
223 | d = c > random.gauss(0.5, 0.15) |
---|
224 | dest.setGray(x, y, d) |
---|
225 | dest.writePng(name) |
---|
226 | |
---|
227 | test242(lenna256bw, "out2-4-2.png") |
---|
228 | test242(gradient256bw, "grad2-4-2.png") |
---|
229 | |
---|
230 | # Output 3.1.1: standard Floyd-Steinberg |
---|
231 | # Output 3.1.2: serpentine Floyd-Steinberg |
---|
232 | # FIXME: serpentine only works if rows == offset * 2 + 1 |
---|
233 | # Output 3.2.1: Fan (modified Floyd-Steinberg) |
---|
234 | # Output 3.2.2: Jarvis, Judice and Ninke |
---|
235 | # Output 3-2-3: Stucki |
---|
236 | # Output 3-2-4: Burkes |
---|
237 | # Output 3-2-5: Sierra |
---|
238 | # Output 3.2.6: Two-line Sierra |
---|
239 | # Output 3.2.7: Sierra's Filter Lite |
---|
240 | # Output 3-2-8: Atkinson |
---|
241 | # Output 3-2-9: Stevenson-Arce |
---|
242 | def test3xx(src, mat, serpentine, name): |
---|
243 | (w, h) = src.size() |
---|
244 | dest = Image((w, h)) |
---|
245 | lines = len(mat) |
---|
246 | rows = len(mat[0]) |
---|
247 | offset = mat[0].index(-1) |
---|
248 | ey = [[0.] * (w + rows - 1) for x in range(lines)] |
---|
249 | for y in range(h): |
---|
250 | ex = [0.] * (rows - offset) |
---|
251 | if serpentine and y & 1: |
---|
252 | xrange = range(w - 1, -1, -1) |
---|
253 | else: |
---|
254 | xrange = range(w) |
---|
255 | for x in xrange: |
---|
256 | # Set pixel |
---|
257 | c = src.getGray(x, y) + ex[0] + ey[0][x + offset] |
---|
258 | d = c > 0.5 |
---|
259 | dest.setGray(x, y, d) |
---|
260 | error = c - d |
---|
261 | # Propagate first line of error |
---|
262 | for dx in range(rows - offset - 2): |
---|
263 | ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx] |
---|
264 | ex[rows - offset - 2] = error * mat[0][rows - 1] |
---|
265 | # Propagate next lines |
---|
266 | if serpentine and y & 1: |
---|
267 | for dy in range(1, lines): |
---|
268 | for dx in range(rows): |
---|
269 | ey[dy][x + dx] += error * mat[dy][rows - 1 - dx] |
---|
270 | else: |
---|
271 | for dy in range(1, lines): |
---|
272 | for dx in range(rows): |
---|
273 | ey[dy][x + dx] += error * mat[dy][dx] |
---|
274 | for dy in range(lines - 1): |
---|
275 | ey[dy] = ey[dy + 1] |
---|
276 | ey[lines - 1] = [0.] * (w + rows - 1) |
---|
277 | dest.writePng(name) |
---|
278 | |
---|
279 | mat = [[ 0., -1, 7./16], |
---|
280 | [ 3./16, 5./16, 1./16]] |
---|
281 | test3xx(lenna256bw, mat, False, "out3-1-1.png") |
---|
282 | test3xx(gradient256bw, mat, False, "grad3-1-1.png") |
---|
283 | test3xx(lenna256bw, mat, True, "out3-1-2.png") |
---|
284 | test3xx(gradient256bw, mat, True, "grad3-1-2.png") |
---|
285 | |
---|
286 | mat = [[ 0., 0., -1, 7./16], |
---|
287 | [ 1./16, 3./16, 5./16, 0.]] |
---|
288 | test3xx(lenna256bw, mat, False, "out3-2-1.png") |
---|
289 | test3xx(gradient256bw, mat, False, "grad3-2-1.png") |
---|
290 | |
---|
291 | mat = [[ 0., 0., -1, 7./48, 5./48], |
---|
292 | [ 3./48, 5./48, 7./48, 5./48, 3./48], |
---|
293 | [ 1./48, 3./48, 5./48, 3./48, 1./48]] |
---|
294 | test3xx(lenna256bw, mat, False, "out3-2-2.png") |
---|
295 | test3xx(gradient256bw, mat, False, "grad3-2-2.png") |
---|
296 | |
---|
297 | mat = [[ 0., 0., -1, 8./42, 4./42], |
---|
298 | [ 2./42, 4./42, 8./42, 4./42, 2./42], |
---|
299 | [ 1./42, 2./42, 4./42, 2./42, 1./42]] |
---|
300 | test3xx(lenna256bw, mat, False, "out3-2-3.png") |
---|
301 | test3xx(gradient256bw, mat, False, "grad3-2-3.png") |
---|
302 | |
---|
303 | mat = [[ 0., 0., -1, 8./32, 4./32], |
---|
304 | [ 2./32, 4./32, 8./32, 4./32, 2./32]] |
---|
305 | test3xx(lenna256bw, mat, False, "out3-2-4.png") |
---|
306 | test3xx(gradient256bw, mat, False, "grad3-2-4.png") |
---|
307 | |
---|
308 | mat = [[ 0., 0., -1, 5./32, 3./32], |
---|
309 | [ 2./32, 4./32, 5./32, 4./32, 2./32], |
---|
310 | [ 0., 2./32, 3./32, 2./32, 0.]] |
---|
311 | test3xx(lenna256bw, mat, False, "out3-2-5.png") |
---|
312 | test3xx(gradient256bw, mat, False, "grad3-2-5.png") |
---|
313 | |
---|
314 | mat = [[ 0., 0., -1, 4./16, 3./16], |
---|
315 | [ 1./16, 2./16, 3./16, 2./16, 1./16]] |
---|
316 | test3xx(lenna256bw, mat, False, "out3-2-6.png") |
---|
317 | test3xx(gradient256bw, mat, False, "grad3-2-6.png") |
---|
318 | |
---|
319 | mat = [[ 0., -1, 2./4], |
---|
320 | [ 1./4, 1./4, 0.]] |
---|
321 | test3xx(lenna256bw, mat, False, "out3-2-7.png") |
---|
322 | test3xx(gradient256bw, mat, False, "grad3-2-7.png") |
---|
323 | |
---|
324 | mat = [[ 0., -1, 1./8, 1./8], |
---|
325 | [ 1./8, 1./8, 1./8, 0.], |
---|
326 | [ 0., 1./8, 0., 0.]] |
---|
327 | test3xx(lenna256bw, mat, False, "out3-2-8.png") |
---|
328 | test3xx(gradient256bw, mat, False, "grad3-2-8.png") |
---|
329 | |
---|
330 | mat = [[ 0., 0., 0., -1, 0., 32./200, 0.], |
---|
331 | [ 12./200, 0., 26./200, 0., 30./200, 0., 16./200], |
---|
332 | [ 0., 12./200, 0., 26./200, 0., 12./200, 0.], |
---|
333 | [ 5./200, 0., 12./200, 0., 12./200, 0., 5./200]] |
---|
334 | test3xx(lenna256bw, mat, False, "out3-2-9.png") |
---|
335 | test3xx(gradient256bw, mat, False, "grad3-2-9.png") |
---|
336 | |
---|
337 | # Output 4.0.1: 4x4 Bayer dithering, 3 colours |
---|
338 | def test401(src, mat, name): |
---|
339 | (w, h) = src.size() |
---|
340 | dest = Image((w, h)) |
---|
341 | dx = len(mat[0]) |
---|
342 | dy = len(mat) |
---|
343 | for y in range(h): |
---|
344 | for x in range(w): |
---|
345 | c = src.getGray(x, y) |
---|
346 | threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1) |
---|
347 | if c < 0.5: |
---|
348 | c = 0.5 * (c > threshold / 2) |
---|
349 | else: |
---|
350 | c = 0.5 + 0.5 * (c > 0.5 + threshold / 2) |
---|
351 | dest.setGray(x, y, c) |
---|
352 | dest.writePng(name) |
---|
353 | |
---|
354 | mat = [[ 0, 8, 3, 11], |
---|
355 | [ 15, 4, 12, 7], |
---|
356 | [ 2, 10, 1, 9], |
---|
357 | [ 13, 6, 14, 5]] |
---|
358 | test401(lenna256bw, mat, "out4-0-1.png") |
---|
359 | test401(gradient256bw, mat, "grad4-0-1.png") |
---|
360 | |
---|
361 | # Output 4.0.2: standard Floyd-Steinberg, 3 colours |
---|
362 | def test402(src, mat, serpentine, name): |
---|
363 | (w, h) = src.size() |
---|
364 | dest = Image((w, h)) |
---|
365 | lines = len(mat) |
---|
366 | rows = len(mat[0]) |
---|
367 | offset = mat[0].index(-1) |
---|
368 | ey = [[0.] * (w + rows - 1) for x in range(lines)] |
---|
369 | for y in range(h): |
---|
370 | ex = [0.] * (rows - offset) |
---|
371 | if serpentine and y & 1: |
---|
372 | xrange = range(w - 1, -1, -1) |
---|
373 | else: |
---|
374 | xrange = range(w) |
---|
375 | for x in xrange: |
---|
376 | # Set pixel |
---|
377 | c = src.getGray(x, y) + ex[0] + ey[0][x + offset] |
---|
378 | d = 0.5 * (c > 0.25) + 0.5 * (c > 0.75) |
---|
379 | dest.setGray(x, y, d) |
---|
380 | error = c - d |
---|
381 | # Propagate first line of error |
---|
382 | for dx in range(rows - offset - 2): |
---|
383 | ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx] |
---|
384 | ex[rows - offset - 2] = error * mat[0][rows - 1] |
---|
385 | # Propagate next lines |
---|
386 | if serpentine and y & 1: |
---|
387 | for dy in range(1, lines): |
---|
388 | for dx in range(rows): |
---|
389 | ey[dy][x + dx] += error * mat[dy][rows - 1 - dx] |
---|
390 | else: |
---|
391 | for dy in range(1, lines): |
---|
392 | for dx in range(rows): |
---|
393 | ey[dy][x + dx] += error * mat[dy][dx] |
---|
394 | for dy in range(lines - 1): |
---|
395 | ey[dy] = ey[dy + 1] |
---|
396 | ey[lines - 1] = [0.] * (w + rows - 1) |
---|
397 | dest.writePng(name) |
---|
398 | |
---|
399 | mat = [[ 0., -1, 7./16], |
---|
400 | [ 3./16, 5./16, 1./16]] |
---|
401 | test402(lenna256bw, mat, False, "out4-0-2.png") |
---|
402 | test402(gradient256bw, mat, False, "grad4-0-2.png") |
---|
403 | |
---|
404 | # Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray |
---|
405 | dest = Image((240, 80)) |
---|
406 | for y in range(80): |
---|
407 | for x in range(80): |
---|
408 | dest.setGray(x, y, Gamma.ItoC(0.5)) |
---|
409 | for x in range(80, 160): |
---|
410 | c = (x + y) & 1 |
---|
411 | dest.setGray(x, y, c) |
---|
412 | for x in range(160, 240): |
---|
413 | dest.setGray(x, y, 0.5) |
---|
414 | dest.writePng("pat4-1-1.png") |
---|
415 | |
---|
416 | ############################################################################## |
---|
417 | # Only temporary cruft below this |
---|
418 | import sys |
---|
419 | sys.exit(0) |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | # Pattern 5: gamma-corrected 50% gray, black-white halftone, 50% gray |
---|
425 | dest = Image((400, 240)) |
---|
426 | for y in range(80): |
---|
427 | for x in range(400): |
---|
428 | if x < 80: |
---|
429 | c = 0. |
---|
430 | elif x < 160: |
---|
431 | c = ((x + y) & 1) and (y & 1) |
---|
432 | elif x < 240: |
---|
433 | c = (x + y) & 1 |
---|
434 | elif x < 320: |
---|
435 | c = ((x + y) & 1) or (y & 1) |
---|
436 | else: |
---|
437 | c = 1. |
---|
438 | dest.setGray(x, y, c) |
---|
439 | for y in range(80, 160): |
---|
440 | for x in range(400): |
---|
441 | dest.setGray(x, y, x / 80 / 4.) |
---|
442 | for y in range(160, 240): |
---|
443 | for x in range(400): |
---|
444 | if x < 80: |
---|
445 | c = 0. |
---|
446 | elif x < 160: |
---|
447 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
---|
448 | elif x < 240: |
---|
449 | c = ((x + y) & 1) and (y & 1) |
---|
450 | elif x < 320: |
---|
451 | c = (x + y) & 1 |
---|
452 | else: |
---|
453 | c = 1. |
---|
454 | dest.setGray(x, y, c) |
---|
455 | dest.writePng("pat005.png") |
---|
456 | |
---|
457 | # Output 6: gamma-aware 20/40/60/80% threshold: |
---|
458 | def test4(src, name): |
---|
459 | (w, h) = src.size() |
---|
460 | dest = Image((w, h)) |
---|
461 | for y in range(h): |
---|
462 | for x in range(w): |
---|
463 | c = src.getGray(x, y) |
---|
464 | if c < 0.2: |
---|
465 | c = 0. |
---|
466 | elif c < 0.4: |
---|
467 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
---|
468 | elif c < 0.6: |
---|
469 | c = ((x + y) & 1) and (y & 1) |
---|
470 | elif c < 0.8: |
---|
471 | c = (x + y) & 1 |
---|
472 | else: |
---|
473 | c = 1. |
---|
474 | dest.setGray(x, y, c) |
---|
475 | dest.writePng(name) |
---|
476 | |
---|
477 | test4(lenna256bw, "out007.png") |
---|
478 | test4(gradient256bw, "grad007.png") |
---|
479 | |
---|
480 | |
---|
481 | src = lenna256bw |
---|
482 | src = gradient256bw |
---|
483 | (w, h) = src.size() |
---|
484 | |
---|
485 | mat = [[ 0, 8, 3, 11], |
---|
486 | [ 15, 4, 12, 7], |
---|
487 | [ 2, 10, 1, 9], |
---|
488 | [ 13, 6, 14, 5]] |
---|
489 | #mat = [[ 6, 7, 8, 9], |
---|
490 | # [ 5, 0, 1, 10], |
---|
491 | # [ 4, 3, 2, 11], |
---|
492 | # [ 15, 14, 13, 12]] |
---|
493 | #mat = [[ 12, 5, 9, 13], |
---|
494 | # [ 8, 0, 1, 6], |
---|
495 | # [ 4, 3, 2, 10], |
---|
496 | # [ 15, 11, 7, 14]] |
---|
497 | size = 4 |
---|
498 | #mat = [[ 35, 24, 13, 14, 25, 32], |
---|
499 | # [ 31, 12, 5, 6, 15, 26], |
---|
500 | # [ 23, 4, 0, 1, 7, 16], |
---|
501 | # [ 22, 11, 3, 2, 8, 17], |
---|
502 | # [ 30, 21, 10, 9, 18, 27], |
---|
503 | # [ 34, 29, 20, 19, 28, 33]] |
---|
504 | #mat = [[ 0, 20, 30, 3, 23, 29], |
---|
505 | # [ 12, 32, 18, 15, 35, 17], |
---|
506 | # [ 27, 8, 4, 24, 11, 7], |
---|
507 | # [ 2, 22, 28, 1, 21, 31], |
---|
508 | # [ 14, 34, 16, 13, 33, 19], |
---|
509 | # [ 25, 10, 6, 26, 9, 5]] |
---|
510 | #size = 6 |
---|
511 | dest = Image((w, h)) |
---|
512 | for y in range(h): |
---|
513 | for x in range(w): |
---|
514 | c = src.getGray(x, y) |
---|
515 | i = Gamma.CtoI(c) |
---|
516 | threshold = mat[x % size][y % size] |
---|
517 | d = math.floor(i * (size * size + .9999)) > threshold |
---|
518 | if c > 0.95: |
---|
519 | print c, i, i * (size * size + .9999) |
---|
520 | c = d |
---|
521 | dest.setGray(x, y, c) |
---|
522 | dest.writePng("out008.png") |
---|
523 | |
---|
524 | # Create a dot-matrix pattern |
---|
525 | mat = [[0, 2, 2, 3, 1], |
---|
526 | [2, 2, 0, 2, 3], |
---|
527 | [2, 0, 1, 1, 3], |
---|
528 | [3, 2, 1, 0, 3], |
---|
529 | [1, 3, 3, 3, 3]] |
---|
530 | dest = Image((320, 64)) |
---|
531 | for x in range(320): |
---|
532 | d = x / 64 |
---|
533 | for y in range(64): |
---|
534 | c = mat[y % 5][x % 5] >= d |
---|
535 | dest.setGray(x, y, c) |
---|
536 | dest.writePng("pat003.png") |
---|
537 | |
---|