Changeset 1930 for www/study/study.py


Ignore:
Timestamp:
11/11/07 13:27:40 (6 years ago)
Author:
sam
Message:
  • Introducing dither matrices.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • www/study/study.py

    r1927 r1930  
    4747    for y in range(256): 
    4848        gradient256bw.setGray(x, 255 - y, y / 255.) 
     49gradient256bw.writePng("gradient256bw.png") 
    4950 
    5051# Output 1: 50% threshold 
     
    143144        dest.setGray(x, y, c) 
    144145dest.writePng("pat003.png") 
     146 
     147# Pattern 4: 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("pat004.png") 
     163 
     164# Output 7: 4x4 Bayer dithering 
     165def test4(src, mat, name): 
     166    (w, h) = src.size() 
     167    dest = Image((w, h)) 
     168    dx = len(mat[0]) 
     169    dy = len(mat) 
     170    for y in range(h): 
     171        for x in range(w): 
     172            c = src.getGray(x, y) 
     173            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1) 
     174            c = c > threshold 
     175            dest.setGray(x, y, c) 
     176    dest.writePng(name) 
     177 
     178mat = [[  0,  8,  3, 11], 
     179       [ 15,  4, 12,  7], 
     180       [  2, 10,  1,  9], 
     181       [ 13,  6, 14,  5]] 
     182test4(lenna256bw, mat, "out007.png") 
     183test4(gradient256bw, mat, "grad007.png") 
     184 
     185mat = [[ 12,  5,  6, 13], 
     186       [  4,  0,  1,  7], 
     187       [ 11,  3,  2,  8], 
     188       [ 15, 10,  9, 14]] 
     189test4(lenna256bw, mat, "out008.png") 
     190test4(gradient256bw, mat, "grad008.png") 
     191 
     192mat = [[ 13,  7,  0,  4, 10], 
     193       [  9,  3,  1,  8, 14], 
     194       [ 11,  5,  2,  6, 12],] 
     195test4(lenna256bw, mat, "out009.png") 
     196test4(gradient256bw, mat, "grad009.png") 
     197 
     198############################################################################## 
     199# Only temporary cruft below this 
     200import sys 
     201sys.exit(0) 
     202 
     203 
     204 
    145205 
    146206# Pattern 4: gamma-corrected 50% gray, black-white halftone, 50% gray 
     
    213273 
    214274 
    215 ############################################################################## 
    216 # Only temporary cruft below this 
    217  
    218275src = lenna256bw 
    219276src = gradient256bw 
     
    224281       [  2, 10,  1,  9], 
    225282       [ 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]] 
     283#mat = [[  6,  7,  8,  9], 
     284#       [  5,  0,  1, 10], 
     285#       [  4,  3,  2, 11], 
     286#       [ 15, 14, 13, 12]] 
     287#mat = [[ 12,  5,  9, 13], 
     288#       [  8,  0,  1,  6], 
     289#       [  4,  3,  2, 10], 
     290#       [ 15, 11,  7, 14]] 
     291size = 4 
     292#mat = [[ 35, 24, 13, 14, 25, 32], 
     293#       [ 31, 12,  5,  6, 15, 26], 
     294#       [ 23,  4,  0,  1,  7, 16], 
     295#       [ 22, 11,  3,  2,  8, 17], 
     296#       [ 30, 21, 10,  9, 18, 27], 
     297#       [ 34, 29, 20, 19, 28, 33]] 
     298#mat = [[  0, 20, 30,  3, 23, 29], 
     299#       [ 12, 32, 18, 15, 35, 17], 
     300#       [ 27,  8,  4, 24, 11,  7], 
     301#       [  2, 22, 28,  1, 21, 31], 
     302#       [ 14, 34, 16, 13, 33, 19], 
     303#       [ 25, 10,  6, 26,  9,  5]] 
     304#size = 6 
    246305dest = Image((w, h)) 
    247306for y in range(h): 
     
    249308        c = src.getGray(x, y) 
    250309        i = Gamma.CtoI(c) 
    251         threshold = mat[x % 6][y % 6] 
    252         c = math.floor(i * 35.999) > threshold 
     310        threshold = mat[x % size][y % size] 
     311        d = math.floor(i * (size * size + .9999)) > threshold 
     312        if c > 0.95: 
     313            print c, i, i * (size * size + .9999) 
     314        c = d 
    253315        dest.setGray(x, y, c) 
    254316dest.writePng("out008.png") 
    255  
    256 import sys 
    257 sys.exit(0) 
    258317 
    259318# Create a dot-matrix pattern 
Note: See TracChangeset for help on using the changeset viewer.