source: www/study/study.py @ 1942

Last change on this file since 1942 was 1942, checked in by Sam Hocevar, 13 years ago
  • Added a figure to explain the 2×2 Bayer matrix results.
  • Started grayscale dithering.
  • Property svn:executable set to *
File size: 16.1 KB
Line 
1#!/usr/bin/env python
2
3import math, gd, random
4
5# Tiny image class to make examples short and readable
6class 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
33class 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
42lenna256bw = Image("lenna256bw.png")
43
44# Create a 32x256 grayscale gradient
45gradient256bw = Image((32, 256))
46for x in range(32):
47    for y in range(256):
48        gradient256bw.setGray(x, 255 - y, y / 255.)
49gradient256bw.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
54def 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
63test11x(lenna256bw, 0.5, "out1-1-1.png")
64test11x(lenna256bw, 0.4, "out1-1-2.png")
65test11x(lenna256bw, 0.6, "out1-1-3.png")
66test11x(gradient256bw, 0.5, "grad1-1-1.png")
67test11x(gradient256bw, 0.4, "grad1-1-2.png")
68test11x(gradient256bw, 0.6, "grad1-1-3.png")
69
70# Output 1.2.1: 3-colour threshold
71# Output 1.2.2: 5-colour threshold
72def 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
84test12x(lenna256bw, 3, "out1-2-1.png")
85test12x(lenna256bw, 5, "out1-2-2.png")
86test12x(gradient256bw, 3, "grad1-2-1.png")
87test12x(gradient256bw, 5, "grad1-2-2.png")
88
89# Pattern 2.1.1: a 50% halftone pattern with various block sizes
90dest = Image((320, 80))
91for 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)
96dest.writePng("pat2-1-1.png")
97
98# Pattern 2.1.2: 25% and 75% halftone patterns with various block sizes
99dest = Image((320, 80))
100for 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)
108dest.writePng("pat2-1-2.png")
109
110# Output 2.1.1: 20/40/60/80% threshold with 25/50/75% patterns inbetween:
111def 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
130test211(lenna256bw, "out2-1-1.png")
131test211(gradient256bw, "grad2-1-1.png")
132
133# Pattern 2.2.1: vertical, mixed and horizontal black-white halftones
134dest = Image((240, 80))
135for 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)
145dest.writePng("pat2-2-1.png")
146
147# Pattern 2.2.2: two different 25% patterns
148dest = Image((320, 80))
149for 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)
162dest.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
167def 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
180mat = [[  0,  8,  3, 11],
181       [ 15,  4, 12,  7],
182       [  2, 10,  1,  9],
183       [ 13,  6, 14,  5]]
184test23x(lenna256bw, mat, "out2-3-1.png")
185test23x(gradient256bw, mat, "grad2-3-1.png")
186
187mat = [[ 12,  5,  6, 13],
188       [  4,  0,  1,  7],
189       [ 11,  3,  2,  8],
190       [ 15, 10,  9, 14]]
191test23x(lenna256bw, mat, "out2-3-2.png")
192test23x(gradient256bw, mat, "grad2-3-2.png")
193
194mat = [[ 13,  7,  0,  4, 10],
195       [  9,  3,  1,  8, 14],
196       [ 11,  5,  2,  6, 12],]
197test23x(lenna256bw, mat, "out2-3-3.png")
198test23x(gradient256bw, mat, "grad2-3-3.png")
199
200# Output 2.4.1: uniform random dithering
201def 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
212test241(lenna256bw, "out2-4-1.png")
213test241(gradient256bw, "grad2-4-1.png")
214
215# Output 2.4.2: random dithering
216def 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
227test242(lenna256bw, "out2-4-2.png")
228test242(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
242def 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
279mat = [[    0.,    -1, 7./16],
280       [ 3./16, 5./16, 1./16]]
281test3xx(lenna256bw, mat, False, "out3-1-1.png")
282test3xx(gradient256bw, mat, False, "grad3-1-1.png")
283test3xx(lenna256bw, mat, True, "out3-1-2.png")
284test3xx(gradient256bw, mat, True, "grad3-1-2.png")
285
286mat = [[    0.,    0.,    -1, 7./16],
287       [ 1./16, 3./16, 5./16,    0.]]
288test3xx(lenna256bw, mat, False, "out3-2-1.png")
289test3xx(gradient256bw, mat, False, "grad3-2-1.png")
290
291mat = [[    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]]
294test3xx(lenna256bw, mat, False, "out3-2-2.png")
295test3xx(gradient256bw, mat, False, "grad3-2-2.png")
296
297mat = [[    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]]
300test3xx(lenna256bw, mat, False, "out3-2-3.png")
301test3xx(gradient256bw, mat, False, "grad3-2-3.png")
302
303mat = [[    0.,    0.,    -1, 8./32, 4./32],
304       [ 2./32, 4./32, 8./32, 4./32, 2./32]]
305test3xx(lenna256bw, mat, False, "out3-2-4.png")
306test3xx(gradient256bw, mat, False, "grad3-2-4.png")
307
308mat = [[    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.]]
311test3xx(lenna256bw, mat, False, "out3-2-5.png")
312test3xx(gradient256bw, mat, False, "grad3-2-5.png")
313
314mat = [[    0.,    0.,    -1, 4./16, 3./16],
315       [ 1./16, 2./16, 3./16, 2./16, 1./16]]
316test3xx(lenna256bw, mat, False, "out3-2-6.png")
317test3xx(gradient256bw, mat, False, "grad3-2-6.png")
318
319mat = [[   0.,   -1, 2./4],
320       [ 1./4, 1./4,   0.]]
321test3xx(lenna256bw, mat, False, "out3-2-7.png")
322test3xx(gradient256bw, mat, False, "grad3-2-7.png")
323
324mat = [[   0.,   -1, 1./8, 1./8],
325       [ 1./8, 1./8, 1./8,   0.],
326       [   0., 1./8,   0.,   0.]]
327test3xx(lenna256bw, mat, False, "out3-2-8.png")
328test3xx(gradient256bw, mat, False, "grad3-2-8.png")
329
330mat = [[      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]]
334test3xx(lenna256bw, mat, False, "out3-2-9.png")
335test3xx(gradient256bw, mat, False, "grad3-2-9.png")
336
337# Output 4.0.1: 4x4 Bayer dithering, 3 colours
338def 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
354mat = [[  0,  8,  3, 11],
355       [ 15,  4, 12,  7],
356       [  2, 10,  1,  9],
357       [ 13,  6, 14,  5]]
358test401(lenna256bw, mat, "out4-0-1.png")
359test401(gradient256bw, mat, "grad4-0-1.png")
360
361# Output 4.0.2: standard Floyd-Steinberg, 3 colours
362def 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
399mat = [[    0.,    -1, 7./16],
400       [ 3./16, 5./16, 1./16]]
401test402(lenna256bw, mat, False, "out4-0-2.png")
402test402(gradient256bw, mat, False, "grad4-0-2.png")
403
404# Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray
405dest = Image((240, 80))
406for 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)
414dest.writePng("pat4-1-1.png")
415
416##############################################################################
417# Only temporary cruft below this
418import sys
419sys.exit(0)
420
421
422
423
424# Pattern 5: gamma-corrected 50% gray, black-white halftone, 50% gray
425dest = Image((400, 240))
426for 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)
439for y in range(80, 160):
440    for x in range(400):
441        dest.setGray(x, y, x / 80 / 4.)
442for 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)
455dest.writePng("pat005.png")
456
457# Output 6: gamma-aware 20/40/60/80% threshold:
458def 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
477test4(lenna256bw, "out007.png")
478test4(gradient256bw, "grad007.png")
479
480
481src = lenna256bw
482src = gradient256bw
483(w, h) = src.size()
484
485mat = [[  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]]
497size = 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
511dest = Image((w, h))
512for 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)
522dest.writePng("out008.png")
523
524# Create a dot-matrix pattern
525mat = [[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]]
530dest = Image((320, 64))
531for 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)
536dest.writePng("pat003.png")
537
Note: See TracBrowser for help on using the repository browser.