1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import math, gd |
---|
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 | # Set our gamma value |
---|
33 | ga = 2.2 |
---|
34 | |
---|
35 | # Load the 256x256 grayscale Lenna image |
---|
36 | src = Image("lenna256bw.png") |
---|
37 | (w, h) = src.size() |
---|
38 | |
---|
39 | # Output 1: 50% threshold |
---|
40 | dest = Image((w, h)) |
---|
41 | for y in range(h): |
---|
42 | for x in range(w): |
---|
43 | c = src.getGray(x, y) > 0.5 |
---|
44 | dest.setGray(x, y, c) |
---|
45 | dest.writePng("out001.png") |
---|
46 | del dest |
---|
47 | |
---|
48 | # Output 2: 40% threshold |
---|
49 | dest = Image((w, h)) |
---|
50 | for y in range(h): |
---|
51 | for x in range(w): |
---|
52 | c = src.getGray(x, y) > 0.4 |
---|
53 | dest.setGray(x, y, c) |
---|
54 | dest.writePng("out002.png") |
---|
55 | del dest |
---|
56 | |
---|
57 | # Output 3: 60% threshold |
---|
58 | dest = Image((w, h)) |
---|
59 | for y in range(h): |
---|
60 | for x in range(w): |
---|
61 | c = src.getGray(x, y) > 0.6 |
---|
62 | dest.setGray(x, y, c) |
---|
63 | dest.writePng("out003.png") |
---|
64 | del dest |
---|
65 | |
---|
66 | # Output 4: 3-colour threshold |
---|
67 | dest = Image((w, h)) |
---|
68 | for y in range(h): |
---|
69 | for x in range(w): |
---|
70 | c = src.getGray(x, y) |
---|
71 | c = math.floor(c * 2.999) / 2 |
---|
72 | dest.setGray(x, y, c) |
---|
73 | dest.writePng("out004.png") |
---|
74 | |
---|
75 | # Output 5: 4-colour threshold |
---|
76 | dest = Image((w, h)) |
---|
77 | for y in range(h): |
---|
78 | for x in range(w): |
---|
79 | c = src.getGray(x, y) |
---|
80 | c = math.floor(c * 3.999) / 3 |
---|
81 | dest.setGray(x, y, c) |
---|
82 | dest.writePng("out005.png") |
---|
83 | |
---|
84 | # Pattern 1: a 50% halftone pattern with various block sizes |
---|
85 | dest = Image((320, 80)) |
---|
86 | for x in range(320): |
---|
87 | d = 8 >> (x / 80) |
---|
88 | for y in range(80): |
---|
89 | c = (x / d + y / d) & 1 |
---|
90 | dest.setGray(x, y, c) |
---|
91 | dest.writePng("pat001.png") |
---|
92 | |
---|
93 | # Pattern 2: 25% and 75% halftone patterns with various block sizes |
---|
94 | dest = Image((320, 80)) |
---|
95 | for x in range(320): |
---|
96 | d = 8 >> (x / 80) |
---|
97 | for y in range(40): |
---|
98 | c = ((x / d + y / d) & 1) or (y / d & 1) |
---|
99 | dest.setGray(x, y, c) |
---|
100 | for y in range(40, 80): |
---|
101 | c = ((x / d + y / d) & 1) and (y / d & 1) |
---|
102 | dest.setGray(x, y, c) |
---|
103 | dest.writePng("pat002.png") |
---|
104 | |
---|
105 | # Output 6: 20/40/60/80% threshold with 25/50/75% halftone patterns inbetween: |
---|
106 | dest = Image((w, h)) |
---|
107 | for y in range(h): |
---|
108 | for x in range(w): |
---|
109 | c = src.getGray(x, y) |
---|
110 | if c < 0.2: |
---|
111 | c = 0. |
---|
112 | elif c < 0.4: |
---|
113 | c = ((x + y) & 1) and (y & 1) |
---|
114 | elif c < 0.6: |
---|
115 | c = (x + y) & 1 |
---|
116 | elif c < 0.8: |
---|
117 | c = ((x + y) & 1) or (y & 1) |
---|
118 | else: |
---|
119 | c = 1. |
---|
120 | dest.setGray(x, y, c) |
---|
121 | dest.writePng("out006.png") |
---|
122 | |
---|
123 | # Pattern 3: vertical, mixed and horizontal black-white halftones |
---|
124 | dest = Image((240, 80)) |
---|
125 | for y in range(80): |
---|
126 | for x in range(80): |
---|
127 | c = x & 1 |
---|
128 | dest.setGray(x, y, c) |
---|
129 | for x in range(80, 160): |
---|
130 | c = (x / d + y / d) & 1 |
---|
131 | dest.setGray(x, y, c) |
---|
132 | for x in range(160, 240): |
---|
133 | c = y & 1 |
---|
134 | dest.setGray(x, y, c) |
---|
135 | dest.writePng("pat003.png") |
---|
136 | |
---|
137 | # Pattern 4: gamma-corrected 50% gray, black-white halftone, 50% gray |
---|
138 | dest = Image((240, 80)) |
---|
139 | for y in range(80): |
---|
140 | for x in range(80): |
---|
141 | dest.setGray(x, y, math.pow(0.5, 1 / ga)) |
---|
142 | for x in range(80, 160): |
---|
143 | c = (x + y) & 1 |
---|
144 | dest.setGray(x, y, c) |
---|
145 | for x in range(160, 240): |
---|
146 | dest.setGray(x, y, 0.5) |
---|
147 | dest.writePng("pat004.png") |
---|
148 | |
---|
149 | # Pattern 5: gamma-corrected 50% gray, black-white halftone, 50% gray |
---|
150 | dest = Image((400, 240)) |
---|
151 | for y in range(80): |
---|
152 | for x in range(400): |
---|
153 | if x < 80: |
---|
154 | c = 0. |
---|
155 | elif x < 160: |
---|
156 | c = ((x + y) & 1) and (y & 1) |
---|
157 | elif x < 240: |
---|
158 | c = (x + y) & 1 |
---|
159 | elif x < 320: |
---|
160 | c = ((x + y) & 1) or (y & 1) |
---|
161 | else: |
---|
162 | c = 1. |
---|
163 | dest.setGray(x, y, c) |
---|
164 | for y in range(80, 160): |
---|
165 | for x in range(400): |
---|
166 | dest.setGray(x, y, x / 80 / 4.) |
---|
167 | for y in range(160, 240): |
---|
168 | for x in range(400): |
---|
169 | if x < 80: |
---|
170 | c = 0. |
---|
171 | elif x < 160: |
---|
172 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
---|
173 | elif x < 240: |
---|
174 | c = ((x + y) & 1) and (y & 1) |
---|
175 | elif x < 320: |
---|
176 | c = (x + y) & 1 |
---|
177 | else: |
---|
178 | c = 1. |
---|
179 | dest.setGray(x, y, c) |
---|
180 | dest.writePng("pat005.png") |
---|
181 | |
---|
182 | # Output 6: gamma-aware 20/40/60/80% threshold: |
---|
183 | dest = Image((w, h)) |
---|
184 | for y in range(h): |
---|
185 | for x in range(w): |
---|
186 | c = src.getGray(x, y) |
---|
187 | if c < 0.2: |
---|
188 | c = 0. |
---|
189 | elif c < 0.4: |
---|
190 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
---|
191 | elif c < 0.6: |
---|
192 | c = ((x + y) & 1) and (y & 1) |
---|
193 | elif c < 0.8: |
---|
194 | c = (x + y) & 1 |
---|
195 | else: |
---|
196 | c = 1. |
---|
197 | dest.setGray(x, y, c) |
---|
198 | dest.writePng("out007.png") |
---|
199 | |
---|
200 | ############################################################################## |
---|
201 | # Only temporary cruft below this |
---|
202 | |
---|
203 | import sys |
---|
204 | sys.exit(0) |
---|
205 | |
---|
206 | # Create a dot-matrix pattern |
---|
207 | mat = [[0, 2, 2, 3, 1], |
---|
208 | [2, 2, 0, 2, 3], |
---|
209 | [2, 0, 1, 1, 3], |
---|
210 | [3, 2, 1, 0, 3], |
---|
211 | [1, 3, 3, 3, 3]] |
---|
212 | dest = Image((320, 64)) |
---|
213 | for x in range(320): |
---|
214 | d = x / 64 |
---|
215 | for y in range(64): |
---|
216 | c = mat[y % 5][x % 5] >= d |
---|
217 | dest.setGray(x, y, c) |
---|
218 | dest.writePng("pat003.png") |
---|
219 | |
---|
220 | |
---|