 Timestamp:
 Nov 8, 2007, 3:34:17 AM (13 years ago)
 Location:
 www/study
 Files:

 7 added
 1 edited
Legend:
 Unmodified
 Added
 Removed

www/study/study.py
r1922 r1924 40 40 41 41 # Load the 256x256 grayscale Lenna image 42 src = Image("lenna256bw.png") 43 (w, h) = src.size() 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.) 44 49 45 50 # Output 1: 50% threshold 46 dest = Image((w, h))47 for y in range(h):48 for x in range(w):49 c = src.getGray(x, y) > 0.550 dest.setGray(x, y, c)51 dest.writePng("out001.png")52 del dest53 54 51 # Output 2: 40% threshold 55 dest = Image((w, h))56 for y in range(h):57 for x in range(w):58 c = src.getGray(x, y) > 0.459 dest.setGray(x, y, c)60 dest.writePng("out002.png")61 del dest62 63 52 # Output 3: 60% threshold 64 dest = Image((w, h)) 65 for y in range(h): 66 for x in range(w): 67 c = src.getGray(x, y) > 0.6 68 dest.setGray(x, y, c) 69 dest.writePng("out003.png") 70 del dest 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") 71 68 72 69 # Output 4: 3colour threshold 73 dest = Image((w, h))74 for y in range(h):75 for x in range(w):76 c = src.getGray(x, y)77 c = math.floor(c * 2.999) / 278 dest.setGray(x, y, c)79 dest.writePng("out004.png")80 81 70 # Output 5: 4colour threshold 82 dest = Image((w, h)) 83 for y in range(h): 84 for x in range(w): 85 c = src.getGray(x, y) 86 c = math.floor(c * 3.999) / 3 87 dest.setGray(x, y, c) 88 dest.writePng("out005.png") 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, 4, "out005.png") 85 test2(gradient256bw, 3, "grad004.png") 86 test2(gradient256bw, 4, "grad005.png") 89 87 90 88 # Pattern 1: a 50% halftone pattern with various block sizes … … 110 108 111 109 # Output 6: 20/40/60/80% threshold with 25/50/75% halftone patterns inbetween: 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("out006.png") 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") 128 131 129 132 # Pattern 3: vertical, mixed and horizontal blackwhite halftones … … 187 190 188 191 # Output 6: gammaaware 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]] 189 246 dest = Image((w, h)) 190 247 for y in range(h): 191 248 for x in range(w): 192 249 c = src.getGray(x, y) 193 if c < 0.2: 194 c = 0. 195 elif c < 0.4: 196 c = (((x + y) & 3) == 1) and ((y & 3) == 1) 197 elif c < 0.6: 198 c = ((x + y) & 1) and (y & 1) 199 elif c < 0.8: 200 c = (x + y) & 1 201 else: 202 c = 1. 203 dest.setGray(x, y, c) 204 dest.writePng("out007.png") 205 206 ############################################################################## 207 # Only temporary cruft below this 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") 208 255 209 256 import sys … … 224 271 dest.writePng("pat003.png") 225 272 226
Note: See TracChangeset
for help on using the changeset viewer.