| 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 | # 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 | |
|---|
| 50 | # Output 1: 50% threshold |
|---|
| 51 | # Output 2: 40% threshold |
|---|
| 52 | # Output 3: 60% threshold |
|---|
| 53 | def test1(src, threshold, name): |
|---|
| 54 | (w, h) = src.size() |
|---|
| 55 | dest = Image((w, h)) |
|---|
| 56 | for y in range(h): |
|---|
| 57 | for x in range(w): |
|---|
| 58 | c = src.getGray(x, y) > threshold |
|---|
| 59 | dest.setGray(x, y, c) |
|---|
| 60 | dest.writePng(name) |
|---|
| 61 | |
|---|
| 62 | test1(lenna256bw, 0.5, "out001.png") |
|---|
| 63 | test1(lenna256bw, 0.4, "out002.png") |
|---|
| 64 | test1(lenna256bw, 0.6, "out003.png") |
|---|
| 65 | test1(gradient256bw, 0.5, "grad001.png") |
|---|
| 66 | test1(gradient256bw, 0.4, "grad002.png") |
|---|
| 67 | test1(gradient256bw, 0.6, "grad003.png") |
|---|
| 68 | |
|---|
| 69 | # Output 4: 3-colour threshold |
|---|
| 70 | # Output 5: 5-colour threshold |
|---|
| 71 | def test2(src, colors, name): |
|---|
| 72 | (w, h) = src.size() |
|---|
| 73 | dest = Image((w, h)) |
|---|
| 74 | p = -0.0001 + colors |
|---|
| 75 | q = colors - 1 |
|---|
| 76 | for y in range(h): |
|---|
| 77 | for x in range(w): |
|---|
| 78 | c = src.getGray(x, y) |
|---|
| 79 | c = math.floor(c * p) / q |
|---|
| 80 | dest.setGray(x, y, c) |
|---|
| 81 | dest.writePng(name) |
|---|
| 82 | |
|---|
| 83 | test2(lenna256bw, 3, "out004.png") |
|---|
| 84 | test2(lenna256bw, 5, "out005.png") |
|---|
| 85 | test2(gradient256bw, 3, "grad004.png") |
|---|
| 86 | test2(gradient256bw, 5, "grad005.png") |
|---|
| 87 | |
|---|
| 88 | # Pattern 1: a 50% halftone pattern with various block sizes |
|---|
| 89 | dest = Image((320, 80)) |
|---|
| 90 | for x in range(320): |
|---|
| 91 | d = 8 >> (x / 80) |
|---|
| 92 | for y in range(80): |
|---|
| 93 | c = (x / d + y / d) & 1 |
|---|
| 94 | dest.setGray(x, y, c) |
|---|
| 95 | dest.writePng("pat001.png") |
|---|
| 96 | |
|---|
| 97 | # Pattern 2: 25% and 75% halftone patterns with various block sizes |
|---|
| 98 | dest = Image((320, 80)) |
|---|
| 99 | for x in range(320): |
|---|
| 100 | d = 8 >> (x / 80) |
|---|
| 101 | for y in range(40): |
|---|
| 102 | c = ((x / d + y / d) & 1) or (y / d & 1) |
|---|
| 103 | dest.setGray(x, y, c) |
|---|
| 104 | for y in range(40, 80): |
|---|
| 105 | c = ((x / d + y / d) & 1) and (y / d & 1) |
|---|
| 106 | dest.setGray(x, y, c) |
|---|
| 107 | dest.writePng("pat002.png") |
|---|
| 108 | |
|---|
| 109 | # Output 6: 20/40/60/80% threshold with 25/50/75% halftone patterns inbetween: |
|---|
| 110 | def test3(src, name): |
|---|
| 111 | (w, h) = src.size() |
|---|
| 112 | dest = Image((w, h)) |
|---|
| 113 | for y in range(h): |
|---|
| 114 | for x in range(w): |
|---|
| 115 | c = src.getGray(x, y) |
|---|
| 116 | if c < 0.2: |
|---|
| 117 | c = 0. |
|---|
| 118 | elif c < 0.4: |
|---|
| 119 | c = ((x + y) & 1) and (y & 1) |
|---|
| 120 | elif c < 0.6: |
|---|
| 121 | c = (x + y) & 1 |
|---|
| 122 | elif c < 0.8: |
|---|
| 123 | c = ((x + y) & 1) or (y & 1) |
|---|
| 124 | else: |
|---|
| 125 | c = 1. |
|---|
| 126 | dest.setGray(x, y, c) |
|---|
| 127 | dest.writePng(name) |
|---|
| 128 | |
|---|
| 129 | test3(lenna256bw, "out006.png") |
|---|
| 130 | test3(gradient256bw, "grad006.png") |
|---|
| 131 | |
|---|
| 132 | # Pattern 3: vertical, mixed and horizontal black-white halftones |
|---|
| 133 | dest = Image((240, 80)) |
|---|
| 134 | for y in range(80): |
|---|
| 135 | for x in range(80): |
|---|
| 136 | c = x & 1 |
|---|
| 137 | dest.setGray(x, y, c) |
|---|
| 138 | for x in range(80, 160): |
|---|
| 139 | c = (x / d + y / d) & 1 |
|---|
| 140 | dest.setGray(x, y, c) |
|---|
| 141 | for x in range(160, 240): |
|---|
| 142 | c = y & 1 |
|---|
| 143 | dest.setGray(x, y, c) |
|---|
| 144 | dest.writePng("pat003.png") |
|---|
| 145 | |
|---|
| 146 | # Pattern 4: gamma-corrected 50% gray, black-white halftone, 50% gray |
|---|
| 147 | dest = Image((240, 80)) |
|---|
| 148 | for y in range(80): |
|---|
| 149 | for x in range(80): |
|---|
| 150 | dest.setGray(x, y, Gamma.ItoC(0.5)) |
|---|
| 151 | for x in range(80, 160): |
|---|
| 152 | c = (x + y) & 1 |
|---|
| 153 | dest.setGray(x, y, c) |
|---|
| 154 | for x in range(160, 240): |
|---|
| 155 | dest.setGray(x, y, 0.5) |
|---|
| 156 | dest.writePng("pat004.png") |
|---|
| 157 | |
|---|
| 158 | # Pattern 5: gamma-corrected 50% gray, black-white halftone, 50% gray |
|---|
| 159 | dest = Image((400, 240)) |
|---|
| 160 | for y in range(80): |
|---|
| 161 | for x in range(400): |
|---|
| 162 | if x < 80: |
|---|
| 163 | c = 0. |
|---|
| 164 | elif x < 160: |
|---|
| 165 | c = ((x + y) & 1) and (y & 1) |
|---|
| 166 | elif x < 240: |
|---|
| 167 | c = (x + y) & 1 |
|---|
| 168 | elif x < 320: |
|---|
| 169 | c = ((x + y) & 1) or (y & 1) |
|---|
| 170 | else: |
|---|
| 171 | c = 1. |
|---|
| 172 | dest.setGray(x, y, c) |
|---|
| 173 | for y in range(80, 160): |
|---|
| 174 | for x in range(400): |
|---|
| 175 | dest.setGray(x, y, x / 80 / 4.) |
|---|
| 176 | for y in range(160, 240): |
|---|
| 177 | for x in range(400): |
|---|
| 178 | if x < 80: |
|---|
| 179 | c = 0. |
|---|
| 180 | elif x < 160: |
|---|
| 181 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
|---|
| 182 | elif x < 240: |
|---|
| 183 | c = ((x + y) & 1) and (y & 1) |
|---|
| 184 | elif x < 320: |
|---|
| 185 | c = (x + y) & 1 |
|---|
| 186 | else: |
|---|
| 187 | c = 1. |
|---|
| 188 | dest.setGray(x, y, c) |
|---|
| 189 | dest.writePng("pat005.png") |
|---|
| 190 | |
|---|
| 191 | # Output 6: gamma-aware 20/40/60/80% threshold: |
|---|
| 192 | def test4(src, name): |
|---|
| 193 | (w, h) = src.size() |
|---|
| 194 | dest = Image((w, h)) |
|---|
| 195 | for y in range(h): |
|---|
| 196 | for x in range(w): |
|---|
| 197 | c = src.getGray(x, y) |
|---|
| 198 | if c < 0.2: |
|---|
| 199 | c = 0. |
|---|
| 200 | elif c < 0.4: |
|---|
| 201 | c = (((x + y) & 3) == 1) and ((y & 3) == 1) |
|---|
| 202 | elif c < 0.6: |
|---|
| 203 | c = ((x + y) & 1) and (y & 1) |
|---|
| 204 | elif c < 0.8: |
|---|
| 205 | c = (x + y) & 1 |
|---|
| 206 | else: |
|---|
| 207 | c = 1. |
|---|
| 208 | dest.setGray(x, y, c) |
|---|
| 209 | dest.writePng(name) |
|---|
| 210 | |
|---|
| 211 | test4(lenna256bw, "out007.png") |
|---|
| 212 | test4(gradient256bw, "grad007.png") |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | ############################################################################## |
|---|
| 216 | # Only temporary cruft below this |
|---|
| 217 | |
|---|
| 218 | src = lenna256bw |
|---|
| 219 | src = gradient256bw |
|---|
| 220 | (w, h) = src.size() |
|---|
| 221 | |
|---|
| 222 | mat = [[ 0, 8, 3, 11], |
|---|
| 223 | [ 15, 4, 12, 7], |
|---|
| 224 | [ 2, 10, 1, 9], |
|---|
| 225 | [ 13, 6, 14, 5]] |
|---|
| 226 | mat = [[ 6, 7, 8, 9], |
|---|
| 227 | [ 5, 0, 1, 10], |
|---|
| 228 | [ 4, 3, 2, 11], |
|---|
| 229 | [ 15, 14, 13, 12]] |
|---|
| 230 | mat = [[ 12, 5, 9, 13], |
|---|
| 231 | [ 8, 0, 1, 6], |
|---|
| 232 | [ 4, 3, 2, 10], |
|---|
| 233 | [ 15, 11, 7, 14]] |
|---|
| 234 | mat = [[ 35, 24, 13, 14, 25, 32], |
|---|
| 235 | [ 31, 12, 5, 6, 15, 26], |
|---|
| 236 | [ 23, 4, 0, 1, 7, 16], |
|---|
| 237 | [ 22, 11, 3, 2, 8, 17], |
|---|
| 238 | [ 30, 21, 10, 9, 18, 27], |
|---|
| 239 | [ 34, 29, 20, 19, 28, 33]] |
|---|
| 240 | mat = [[ 0, 20, 30, 3, 23, 29], |
|---|
| 241 | [ 12, 32, 18, 15, 35, 17], |
|---|
| 242 | [ 27, 8, 4, 24, 11, 7], |
|---|
| 243 | [ 2, 22, 28, 1, 21, 31], |
|---|
| 244 | [ 14, 34, 16, 13, 33, 19], |
|---|
| 245 | [ 25, 10, 6, 26, 9, 5]] |
|---|
| 246 | dest = Image((w, h)) |
|---|
| 247 | for y in range(h): |
|---|
| 248 | for x in range(w): |
|---|
| 249 | c = src.getGray(x, y) |
|---|
| 250 | i = Gamma.CtoI(c) |
|---|
| 251 | threshold = mat[x % 6][y % 6] |
|---|
| 252 | c = math.floor(i * 35.999) > threshold |
|---|
| 253 | dest.setGray(x, y, c) |
|---|
| 254 | dest.writePng("out008.png") |
|---|
| 255 | |
|---|
| 256 | import sys |
|---|
| 257 | sys.exit(0) |
|---|
| 258 | |
|---|
| 259 | # Create a dot-matrix pattern |
|---|
| 260 | mat = [[0, 2, 2, 3, 1], |
|---|
| 261 | [2, 2, 0, 2, 3], |
|---|
| 262 | [2, 0, 1, 1, 3], |
|---|
| 263 | [3, 2, 1, 0, 3], |
|---|
| 264 | [1, 3, 3, 3, 3]] |
|---|
| 265 | dest = Image((320, 64)) |
|---|
| 266 | for x in range(320): |
|---|
| 267 | d = x / 64 |
|---|
| 268 | for y in range(64): |
|---|
| 269 | c = mat[y % 5][x % 5] >= d |
|---|
| 270 | dest.setGray(x, y, c) |
|---|
| 271 | dest.writePng("pat003.png") |
|---|
| 272 | |
|---|