source: www/study/study.py @ 2010

Revision 2010, 21.4 KB checked in by sam, 6 years ago (diff)
  • Preparing random matrix halftoning.
  • Property svn:executable set to *
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 save(self, name):
14        print " * saving %s" % (name,)
15        self.writePng(name)
16    def getGray(self, x, y):
17        p = self.getPixel((x, y))
18        c = self.colorComponents(p)[0] / 255.0
19        return c
20    def getRgb(self, x, y):
21        p = self.getPixel((x, y))
22        rgb = self.colorComponents(p)
23        return [rgb[0] / 255.0, rgb[1] / 255.0, rgb[2] / 255.0]
24    def setGray(self, x, y, t):
25        p = (int)(t * 255.999)
26        c = self.colorResolve((p, p, p))
27        self.setPixel((x, y), c)
28    def setRgb(self, x, y, r, g, b):
29        r = (int)(r * 255.999)
30        g = (int)(g * 255.999)
31        b = (int)(b * 255.999)
32        c = self.colorResolve((r, g, b))
33        self.setPixel((x, y), c)
34
35##############################################################################
36print "Initialisation"
37
38# Manipulate gamma values
39class Gamma:
40    def CtoI(x):
41        return math.pow(x, 2.2)
42    def ItoC(x):
43        if x < 0:
44            return -math.pow(-x, 1 / 2.2)
45        return math.pow(x, 1 / 2.2)
46    CtoI = staticmethod(CtoI)
47    ItoC = staticmethod(ItoC)
48
49# Load the 256x256 grayscale Lenna image
50lenna256bw = Image("lenna256bw.png")
51
52# Create a 32x256 grayscale gradient
53gradient256bw = Image((32, 256))
54for x in range(32):
55    for y in range(256):
56        gradient256bw.setGray(x, 255 - y, y / 255.)
57gradient256bw.save("gradient256bw.png")
58
59##############################################################################
60print "Chapter 1. Colour quantisation"
61
62# Output 1.1.1: 50% threshold
63# Output 1.1.2: 40% threshold
64# Output 1.1.3: 60% threshold
65def test11x(src, threshold):
66    (w, h) = src.size()
67    dest = Image((w, h))
68    for y in range(h):
69        for x in range(w):
70            c = src.getGray(x, y) > threshold
71            dest.setGray(x, y, c)
72    return dest
73
74test11x(lenna256bw, 0.5).save("out1-1-1.png")
75test11x(lenna256bw, 0.4).save("out1-1-2.png")
76test11x(lenna256bw, 0.6).save("out1-1-3.png")
77test11x(gradient256bw, 0.5).save("grad1-1-1.png")
78test11x(gradient256bw, 0.4).save("grad1-1-2.png")
79test11x(gradient256bw, 0.6).save("grad1-1-3.png")
80
81# Output 1.2.1: 3-colour threshold
82# Output 1.2.2: 5-colour threshold
83def test12x(src, colors):
84    (w, h) = src.size()
85    dest = Image((w, h))
86    p = -0.0001 + colors
87    q = colors - 1
88    for y in range(h):
89        for x in range(w):
90            c = src.getGray(x, y)
91            c = math.floor(c * p) / q
92            dest.setGray(x, y, c)
93    return dest
94
95test12x(lenna256bw, 3).save("out1-2-1.png")
96test12x(lenna256bw, 5).save("out1-2-2.png")
97test12x(gradient256bw, 3).save("grad1-2-1.png")
98test12x(gradient256bw, 5).save("grad1-2-2.png")
99
100##############################################################################
101print "Chapter 2. Halftoning patterns"
102
103# Pattern 2.1.1: a 50% halftone pattern with various block sizes
104dest = Image((320, 80))
105for x in range(320):
106    d = 8 >> (x / 80)
107    for y in range(80):
108        c = (x / d + y / d) & 1
109        dest.setGray(x, y, c)
110dest.save("pat2-1-1.png")
111
112# Pattern 2.1.2: 25% and 75% halftone patterns with various block sizes
113dest = Image((320, 80))
114for x in range(320):
115    d = 8 >> (x / 80)
116    for y in range(40):
117        c = ((x / d + y / d) & 1) or (y / d & 1)
118        dest.setGray(x, y, c)
119    for y in range(40, 80):
120        c = ((x / d + y / d) & 1) and (y / d & 1)
121        dest.setGray(x, y, c)
122dest.save("pat2-1-2.png")
123
124# Output 2.1.1: 20/40/60/80% threshold with 25/50/75% patterns inbetween:
125def test211(src):
126    (w, h) = src.size()
127    dest = Image((w, h))
128    for y in range(h):
129        for x in range(w):
130            c = src.getGray(x, y)
131            if c < 0.2:
132                c = 0.
133            elif c < 0.4:
134                c = ((x + y) & 1) and (y & 1)
135            elif c < 0.6:
136                c = (x + y) & 1
137            elif c < 0.8:
138                c = ((x + y) & 1) or (y & 1)
139            else:
140                c = 1.
141            dest.setGray(x, y, c)
142    return dest
143
144test211(lenna256bw).save("out2-1-1.png")
145test211(gradient256bw).save("grad2-1-1.png")
146
147# Pattern 2.2.1: vertical, mixed and horizontal black-white halftones
148dest = Image((240, 80))
149for y in range(80):
150    for x in range(80):
151        c = x & 1
152        dest.setGray(x, y, c)
153    for x in range(80, 160):
154        c = (x / d + y / d) & 1
155        dest.setGray(x, y, c)
156    for x in range(160, 240):
157        c = y & 1
158        dest.setGray(x, y, c)
159dest.save("pat2-2-1.png")
160
161# Pattern 2.2.2: two different 25% patterns
162dest = Image((320, 80))
163for y in range(80):
164    for x in range(80):
165        c = (x / 2 & 1) and (y / 2 & 1)
166        dest.setGray(x, y, c)
167    for x in range(80, 160):
168        c = (x & 1) and (y & 1)
169        dest.setGray(x, y, c)
170    for x in range(160, 240):
171        c = (x & 1) and ((y + x / 2) & 1)
172        dest.setGray(x, y, c)
173    for x in range(240, 320):
174        c = (x / 2 & 1) and ((y / 2 + x / 4) & 1)
175        dest.setGray(x, y, c)
176dest.save("pat2-2-2.png")
177
178# Output 2.3.1: 4x4 Bayer dithering
179# Output 2.3.2: 4x4 cluster dot
180# Output 2.3.3: 5x3 line dithering
181DITHER_BAYER44 = \
182    [[  0,  8,  3, 11],
183     [ 15,  4, 12,  7],
184     [  2, 10,  1,  9],
185     [ 13,  6, 14,  5]]
186DITHER_CLUSTER44 = \
187    [[ 12,  5,  6, 13],
188     [  4,  0,  1,  7],
189     [ 11,  3,  2,  8],
190     [ 15, 10,  9, 14]]
191DITHER_LINE53 = \
192    [[ 13,  7,  0,  4, 10],
193     [  9,  3,  1,  8, 14],
194     [ 11,  5,  2,  6, 12],]
195
196def test23x(src, mat):
197    (w, h) = src.size()
198    dest = Image((w, h))
199    dx = len(mat[0])
200    dy = len(mat)
201    for y in range(h):
202        for x in range(w):
203            c = src.getGray(x, y)
204            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1)
205            c = c > threshold
206            dest.setGray(x, y, c)
207    return dest
208
209test23x(lenna256bw, DITHER_BAYER44).save("out2-3-1.png")
210test23x(gradient256bw, DITHER_BAYER44).save("grad2-3-1.png")
211
212test23x(lenna256bw, DITHER_CLUSTER44).save("out2-3-2.png")
213test23x(gradient256bw, DITHER_CLUSTER44).save("grad2-3-2.png")
214
215test23x(lenna256bw, DITHER_LINE53).save("out2-3-3.png")
216test23x(gradient256bw, DITHER_LINE53).save("grad2-3-3.png")
217
218# Output 2.4.1: uniform random dithering
219def test241(src):
220    random.seed(0)
221    (w, h) = src.size()
222    dest = Image((w, h))
223    for y in range(h):
224        for x in range(w):
225            c = src.getGray(x, y)
226            d = c > random.random()
227            dest.setGray(x, y, d)
228    return dest
229
230test241(lenna256bw).save("out2-4-1.png")
231test241(gradient256bw).save("grad2-4-1.png")
232
233# Output 2.4.2: gaussian random dithering
234def test242(src):
235    random.seed(0)
236    (w, h) = src.size()
237    dest = Image((w, h))
238    for y in range(h):
239        for x in range(w):
240            c = src.getGray(x, y)
241            d = c > random.gauss(0.5, 0.15)
242            dest.setGray(x, y, d)
243    return dest
244
245test242(lenna256bw).save("out2-4-2.png")
246test242(gradient256bw).save("grad2-4-2.png")
247
248# Output 2.4.3: random dither matrices
249def test243(src, mat):
250    random.seed(0)
251    (w, h) = src.size()
252    dest = Image((w, h))
253    dx = len(mat[0])
254    dy = len(mat)
255    randmat = [[(int)(random.random() * 16) for x in range(w / dx)] for y in range(h / dy)]
256    for y in range(h):
257        for x in range(w):
258            c = src.getGray(x, y)
259            val = (mat[y % dy][x % dx] + randmat[y / dy][x / dx]) % (dx * dy)
260            threshold = (1. + val) / (dx * dy + 1)
261            c = c > threshold
262            dest.setGray(x, y, c)
263    return dest
264
265test243(lenna256bw, DITHER_BAYER44).save("out2-4-3.png")
266test243(gradient256bw, DITHER_BAYER44).save("grad2-4-3.png")
267
268##############################################################################
269print "Chapter 3. Error diffusion"
270
271# Output 3.0.1: naive error diffusion
272# Output 3.1.1: standard Floyd-Steinberg
273# Output 3.1.2: serpentine Floyd-Steinberg
274# FIXME: serpentine only works if rows == offset * 2 + 1
275# Output 3.2.1: Fan (modified Floyd-Steinberg)
276# Output 3.2.2: Jarvis, Judice and Ninke
277# Output 3-2-3: Stucki
278# Output 3-2-4: Burkes
279# Output 3-2-5: Sierra
280# Output 3.2.6: Two-line Sierra
281# Output 3.2.7: Sierra's Filter Lite
282# Output 3-2-8: Atkinson
283ERROR_NAIVE = \
284    [[ -1, 1]]
285ERROR_FSTEIN = \
286    [[    0.,    -1, 7./16],
287     [ 3./16, 5./16, 1./16]]
288ERROR_FAN = \
289    [[    0.,    0.,    -1, 7./16],
290     [ 1./16, 3./16, 5./16,    0.]]
291ERROR_JAJUNI = \
292    [[    0.,    0.,    -1, 7./48, 5./48],
293     [ 3./48, 5./48, 7./48, 5./48, 3./48],
294     [ 1./48, 3./48, 5./48, 3./48, 1./48]]
295ERROR_STUCKI = \
296    [[    0.,    0.,    -1, 8./42, 4./42],
297     [ 2./42, 4./42, 8./42, 4./42, 2./42],
298     [ 1./42, 2./42, 4./42, 2./42, 1./42]]
299ERROR_BURKES = \
300    [[    0.,    0.,    -1, 8./32, 4./32],
301     [ 2./32, 4./32, 8./32, 4./32, 2./32]]
302ERROR_SIERRA = \
303    [[    0.,    0.,    -1, 5./32, 3./32],
304     [ 2./32, 4./32, 5./32, 4./32, 2./32],
305     [    0., 2./32, 3./32, 2./32,    0.]]
306ERROR_SIERRA2 = \
307    [[    0.,    0.,    -1, 4./16, 3./16],
308     [ 1./16, 2./16, 3./16, 2./16, 1./16]]
309ERROR_FILTERLITE = \
310    [[   0.,   -1, 2./4],
311     [ 1./4, 1./4,   0.]]
312ERROR_ATKINSON = \
313    [[   0.,   -1, 1./8, 1./8],
314     [ 1./8, 1./8, 1./8,   0.],
315     [   0., 1./8,   0.,   0.]]
316## This is Stevenson-Arce in hex lattice
317#ERROR_STAR = \
318#    [[      0.,      0.,      0.,      -1,      0.,  32./200,      0.],
319#     [ 12./200,      0., 26./200,      0., 30./200,       0., 16./200],
320#     [      0., 12./200,      0., 26./200,      0.,  12./200,      0.],
321#     [  5./200,      0., 12./200,      0., 12./200,       0.,  5./200]]
322## This is an attempt at implementing Stevenson-Arce in square lattice
323#ERROR_STAR = \
324#    [[      0.,      0.,      -1, 32./200,      0.],
325#     [  6./200, 19./200, 28./200, 23./200,  8./200],
326#     [      0., 12./200, 26./200, 12./200,      0.],
327#     [  2./200,  9./200, 12./200,  9./200,  2./200]]
328
329def test3xx(src, mat, serpentine):
330    (w, h) = src.size()
331    dest = Image((w, h))
332    lines = len(mat)
333    rows = len(mat[0])
334    offset = mat[0].index(-1)
335    ey = [[0.] * (w + rows - 1) for x in range(lines)]
336    for y in range(h):
337        ex = [0.] * (rows - offset)
338        if serpentine and y & 1:
339            xrange = range(w - 1, -1, -1)
340        else:
341            xrange = range(w)
342        for x in xrange:
343            # Set pixel
344            c = src.getGray(x, y) + ex[0] + ey[0][x + offset]
345            d = c > 0.5
346            dest.setGray(x, y, d)
347            error = c - d
348            # Propagate first line of error
349            for dx in range(rows - offset - 2):
350                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
351            ex[rows - offset - 2] = error * mat[0][rows - 1]
352            # Propagate next lines
353            if serpentine and y & 1:
354                for dy in range(1, lines):
355                    for dx in range(rows):
356                        ey[dy][x + dx] += error * mat[dy][rows - 1 - dx]
357            else:
358                for dy in range(1, lines):
359                    for dx in range(rows):
360                        ey[dy][x + dx] += error * mat[dy][dx]
361        for dy in range(lines - 1):
362            ey[dy] = ey[dy + 1]
363        ey[lines - 1] = [0.] * (w + rows - 1)
364    return dest
365
366test3xx(lenna256bw, ERROR_NAIVE, False).save("out3-0-1.png")
367test3xx(gradient256bw, ERROR_NAIVE, False).save("grad3-0-1.png")
368
369test3xx(lenna256bw, ERROR_FSTEIN, False).save("out3-1-1.png")
370test3xx(gradient256bw, ERROR_FSTEIN, False).save("grad3-1-1.png")
371test3xx(lenna256bw, ERROR_FSTEIN, True).save("out3-1-2.png")
372test3xx(gradient256bw, ERROR_FSTEIN, True).save("grad3-1-2.png")
373
374test3xx(lenna256bw, ERROR_FAN, False).save("out3-2-1.png")
375test3xx(gradient256bw, ERROR_FAN, False).save("grad3-2-1.png")
376
377test3xx(lenna256bw, ERROR_JAJUNI, False).save("out3-2-2.png")
378test3xx(gradient256bw, ERROR_JAJUNI, False).save("grad3-2-2.png")
379
380test3xx(lenna256bw, ERROR_STUCKI, False).save("out3-2-3.png")
381test3xx(gradient256bw, ERROR_STUCKI, False).save("grad3-2-3.png")
382
383test3xx(lenna256bw, ERROR_BURKES, False).save("out3-2-4.png")
384test3xx(gradient256bw, ERROR_BURKES, False).save("grad3-2-4.png")
385
386test3xx(lenna256bw, ERROR_SIERRA, False).save("out3-2-5.png")
387test3xx(gradient256bw, ERROR_SIERRA, False).save("grad3-2-5.png")
388
389test3xx(lenna256bw, ERROR_SIERRA2, False).save("out3-2-6.png")
390test3xx(gradient256bw, ERROR_SIERRA2, False).save("grad3-2-6.png")
391
392test3xx(lenna256bw, ERROR_FILTERLITE, False).save("out3-2-7.png")
393test3xx(gradient256bw, ERROR_FILTERLITE, False).save("grad3-2-7.png")
394
395test3xx(lenna256bw, ERROR_ATKINSON, False).save("out3-2-8.png")
396test3xx(gradient256bw, ERROR_ATKINSON, False).save("grad3-2-8.png")
397
398#test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png")
399#test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png")
400
401#test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png")
402#test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png")
403
404##############################################################################
405print "Chapter 4. Grayscale dithering"
406
407# Output 4.0.1: 4x4 Bayer dithering, 3 colours
408def test401(src, mat):
409    (w, h) = src.size()
410    dest = Image((w, h))
411    dx = len(mat[0])
412    dy = len(mat)
413    for y in range(h):
414        for x in range(w):
415            c = src.getGray(x, y)
416            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1)
417            if c < 0.5:
418                c = 0.5 * (c > threshold / 2)
419            else:
420                c = 0.5 + 0.5 * (c > 0.5 + threshold / 2)
421            dest.setGray(x, y, c)
422    return dest
423
424test401(lenna256bw, DITHER_BAYER44).save("out4-0-1.png")
425test401(gradient256bw, DITHER_BAYER44).save("grad4-0-1.png")
426
427# Output 4.0.2: standard Floyd-Steinberg, 3 colours
428def test402(src, mat, serpentine):
429    (w, h) = src.size()
430    dest = Image((w, h))
431    lines = len(mat)
432    rows = len(mat[0])
433    offset = mat[0].index(-1)
434    ey = [[0.] * (w + rows - 1) for x in range(lines)]
435    for y in range(h):
436        ex = [0.] * (rows - offset)
437        if serpentine and y & 1:
438            xrange = range(w - 1, -1, -1)
439        else:
440            xrange = range(w)
441        for x in xrange:
442            # Set pixel
443            c = src.getGray(x, y) + ex[0] + ey[0][x + offset]
444            d = 0.5 * (c > 0.25) + 0.5 * (c > 0.75)
445            dest.setGray(x, y, d)
446            error = c - d
447            # Propagate first line of error
448            for dx in range(rows - offset - 2):
449                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
450            ex[rows - offset - 2] = error * mat[0][rows - 1]
451            # Propagate next lines
452            if serpentine and y & 1:
453                for dy in range(1, lines):
454                    for dx in range(rows):
455                        ey[dy][x + dx] += error * mat[dy][rows - 1 - dx]
456            else:
457                for dy in range(1, lines):
458                    for dx in range(rows):
459                        ey[dy][x + dx] += error * mat[dy][dx]
460        for dy in range(lines - 1):
461            ey[dy] = ey[dy + 1]
462        ey[lines - 1] = [0.] * (w + rows - 1)
463    return dest
464
465test402(lenna256bw, ERROR_FSTEIN, False).save("out4-0-2.png")
466test402(gradient256bw, ERROR_FSTEIN, False).save("grad4-0-2.png")
467
468# Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray
469dest = Image((240, 80))
470for y in range(80):
471    for x in range(80):
472        dest.setGray(x, y, Gamma.ItoC(0.5))
473    for x in range(80, 160):
474        c = (x + y) & 1
475        dest.setGray(x, y, c)
476    for x in range(160, 240):
477        dest.setGray(x, y, 0.5)
478dest.save("pat4-1-1.png")
479
480# Output 4.2.1: gamma-corrected 2-colour Floyd-Steinberg
481# Output 4.2.2: gamma-corrected 3-colour Floyd-Steinberg
482# Output 4.2.3: gamma-corrected 4-colour Floyd-Steinberg
483def test42x(src, mat, threshold):
484    (w, h) = src.size()
485    dest = Image((w, h))
486    lines = len(mat)
487    rows = len(mat[0])
488    offset = mat[0].index(-1)
489    ey = [[0.] * (w + rows - 1) for x in range(lines)]
490    for y in range(h):
491        ex = [0.] * (rows - offset)
492        for x in range(w):
493            # Set pixel
494            c = Gamma.CtoI(src.getGray(x, y)) + ex[0] + ey[0][x + offset]
495            d = threshold(c)
496            dest.setGray(x, y, Gamma.ItoC(d))
497            error = c - d
498            # Propagate first line of error
499            for dx in range(rows - offset - 2):
500                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
501            ex[rows - offset - 2] = error * mat[0][rows - 1]
502            # Propagate next lines
503            for dy in range(1, lines):
504                for dx in range(rows):
505                    ey[dy][x + dx] += error * mat[dy][dx]
506        for dy in range(lines - 1):
507            ey[dy] = ey[dy + 1]
508        ey[lines - 1] = [0.] * (w + rows - 1)
509    return dest
510
511def threshold_2(x):
512    if x < Gamma.CtoI(0.50):
513        return 0.
514    return 1.
515
516def threshold_3(x):
517    if x < Gamma.CtoI(0.25):
518        return 0.
519    elif x < Gamma.CtoI(0.75):
520        return Gamma.CtoI(0.5)
521    return 1.
522
523def threshold_4(x):
524    if x < Gamma.CtoI(0.17):
525        return 0.
526    elif x < Gamma.CtoI(0.50):
527        return Gamma.CtoI(0.3333)
528    elif x < Gamma.CtoI(0.83):
529        return Gamma.CtoI(0.6666)
530    return 1.
531
532test42x(lenna256bw, ERROR_FSTEIN, threshold_2).save("out4-2-1.png")
533test42x(gradient256bw, ERROR_FSTEIN, threshold_2).save("grad4-2-1.png")
534test42x(lenna256bw, ERROR_FSTEIN, threshold_3).save("out4-2-2.png")
535test42x(gradient256bw, ERROR_FSTEIN, threshold_3).save("grad4-2-2.png")
536test42x(lenna256bw, ERROR_FSTEIN, threshold_4).save("out4-2-3.png")
537test42x(gradient256bw, ERROR_FSTEIN, threshold_4).save("grad4-2-3.png")
538
539##############################################################################
540print "Chapter 5. Colour dithering"
541
542# Pattern 5.1.1: 8-colour palette
543dest = Image((512, 64))
544for x in range(512):
545    d = x / 64
546    r = (d & 2) >> 1
547    g = (d & 4) >> 2
548    b = d & 1
549    for y in range(64):
550        dest.setRgb(x, y, r, g, b)
551dest.save("pat5-1-1.png")
552
553# Load the 256x256 colour Lenna image
554lenna256 = Image("lenna256.png")
555
556# Output 5.1.1: 8-colour Floyd-Steinberg RGB dithering
557# Output 5.1.2: 8-colour gamma-corrected Floyd-Steinberg RGB dithering
558def test51x(src, mat, func):
559    (w, h) = src.size()
560    dest = Image((w, h))
561    tmp = [Image((w, h)), Image((w, h)), Image((w, h))]
562    for y in range(h):
563        for x in range(w):
564            rgb = src.getRgb(x, y)
565            for i in range(3):
566                tmp[i].setGray(x, y, rgb[i])
567    for i in range(3):
568        tmp[i] = func(tmp[i], mat, threshold_2)
569    for y in range(h):
570        for x in range(w):
571            (r, g, b) = [tmp[i].getGray(x, y) for i in range(3)]
572            dest.setRgb(x, y, r, g, b)
573    return dest
574
575test51x(lenna256, ERROR_FSTEIN, test3xx).save("out5-1-1.png")
576out512 = test51x(lenna256, ERROR_FSTEIN, test42x)
577out512.save("out5-1-2.png")
578
579# Pattern 5.2.1: different colours give the same result
580dest = Image((320, 160))
581for x in range(80):
582    for y in range(80):
583        r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7
584        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
585        b = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
586        dest.setRgb(x, y, b, g, r)
587    for y in range(80, 160):
588        r = DITHER_BAYER44[y % 4][x % 4] > 7
589        g = DITHER_BAYER44[y % 4][x % 4] > 13
590        b = DITHER_BAYER44[y % 4][x % 4] > 13
591        dest.setRgb(x, y, b, g, r)
592for x in range(80, 160):
593    for y in range(80):
594        r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7
595        g = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13
596        b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13
597        dest.setRgb(x, y, b, g, r)
598    for y in range(80, 160):
599        r = DITHER_BAYER44[y % 4][x % 4] > 7
600        g = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13
601        b = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13
602        dest.setRgb(x, y, b, g, r)
603for x in range(160, 240):
604    for y in range(80):
605        r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8 + 1) % 4] > 7
606        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
607        b = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 13
608        dest.setRgb(x, y, b, g, r)
609    for y in range(80, 160):
610        r = DITHER_BAYER44[(y + 1) % 4][(x + 1) % 4] > 7
611        g = DITHER_BAYER44[y % 4][x % 4] > 13
612        b = DITHER_BAYER44[(y + 1) % 4][x % 4] > 13
613        dest.setRgb(x, y, b, g, r)
614for x in range(240, 320):
615    for y in range(80):
616        r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 7
617        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
618        b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 2) % 4] > 13
619        dest.setRgb(x, y, b, g, r)
620    for y in range(80, 160):
621        r = DITHER_BAYER44[(y + 1) % 4][x % 4] > 7
622        g = DITHER_BAYER44[y % 4][x % 4] > 13
623        b = DITHER_BAYER44[y % 4][(x + 2) % 4] > 13
624        dest.setRgb(x, y, b, g, r)
625dest.save("pat5-2-1.png")
626
627# Output 5.2.1: cropped 5.1.2
628# Output 5.2.2: close-up of cropped 5.1.2
629def test52x(src, x, y, w, h, z):
630    dest = Image((w * z, h * z))
631    for j in range(h):
632        for i in range(w):
633            (r, g, b) = src.getRgb(x + i, y + j)
634            for v in range(z):
635                for u in range(z):
636                    dest.setRgb(i * z + u, j * z + v, r, g, b)
637    return dest
638
639test52x(out512, 20, 70, 32, 32, 1).save("out5-2-1.png")
640test52x(out512, 20, 70, 32, 32, 6).save("out5-2-2.png")
641
642out523 = test51x(lenna256, ERROR_STUCKI, test42x)
643out523.save("out5-2-3.png")
644test52x(out523, 20, 70, 32, 32, 1).save("out5-2-4.png")
645test52x(out523, 20, 70, 32, 32, 6).save("out5-2-5.png")
646
647##############################################################################
648print "Finished"
649
650# Place temporary cruft below this
651import sys; sys.exit(0)
652
Note: See TracBrowser for help on using the repository browser.