Changeset 1924 for www


Ignore:
Timestamp:
Nov 8, 2007, 3:34:17 AM (13 years ago)
Author:
Sam Hocevar
Message:
  • Added gradient samples for each dithering algorithm we demonstrate.
  • Rewrote parts of the code here and there.
Location:
www/study
Files:
7 added
1 edited

Legend:

Unmodified
Added
Removed
  • www/study/study.py

    r1922 r1924  
    4040
    4141# Load the 256x256 grayscale Lenna image
    42 src = Image("lenna256bw.png")
    43 (w, h) = src.size()
     42lenna256bw = Image("lenna256bw.png")
     43
     44# Create a 32x256 grayscale gradient
     45gradient256bw = Image((32, 256))
     46for x in range(32):
     47    for y in range(256):
     48        gradient256bw.setGray(x, 255 - y, y / 255.)
    4449
    4550# 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.5
    50         dest.setGray(x, y, c)
    51 dest.writePng("out001.png")
    52 del dest
    53 
    5451# 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.4
    59         dest.setGray(x, y, c)
    60 dest.writePng("out002.png")
    61 del dest
    62 
    6352# 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
     53def 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
     62test1(lenna256bw, 0.5, "out001.png")
     63test1(lenna256bw, 0.4, "out002.png")
     64test1(lenna256bw, 0.6, "out003.png")
     65test1(gradient256bw, 0.5, "grad001.png")
     66test1(gradient256bw, 0.4, "grad002.png")
     67test1(gradient256bw, 0.6, "grad003.png")
    7168
    7269# Output 4: 3-colour 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) / 2
    78         dest.setGray(x, y, c)
    79 dest.writePng("out004.png")
    80 
    8170# Output 5: 4-colour 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")
     71def 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
     83test2(lenna256bw, 3, "out004.png")
     84test2(lenna256bw, 4, "out005.png")
     85test2(gradient256bw, 3, "grad004.png")
     86test2(gradient256bw, 4, "grad005.png")
    8987
    9088# Pattern 1: a 50% halftone pattern with various block sizes
     
    110108
    111109# 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")
     110def 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
     129test3(lenna256bw, "out006.png")
     130test3(gradient256bw, "grad006.png")
    128131
    129132# Pattern 3: vertical, mixed and horizontal black-white halftones
     
    187190
    188191# Output 6: gamma-aware 20/40/60/80% threshold:
     192def 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
     211test4(lenna256bw, "out007.png")
     212test4(gradient256bw, "grad007.png")
     213
     214
     215##############################################################################
     216# Only temporary cruft below this
     217
     218src = lenna256bw
     219src = gradient256bw
     220(w, h) = src.size()
     221
     222mat = [[  0,  8,  3, 11],
     223       [ 15,  4, 12,  7],
     224       [  2, 10,  1,  9],
     225       [ 13,  6, 14,  5]]
     226mat = [[  6,  7,  8,  9],
     227       [  5,  0,  1, 10],
     228       [  4,  3,  2, 11],
     229       [ 15, 14, 13, 12]]
     230mat = [[ 12,  5,  9, 13],
     231       [  8,  0,  1,  6],
     232       [  4,  3,  2, 10],
     233       [ 15, 11,  7, 14]]
     234mat = [[ 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]]
     240mat = [[  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]]
    189246dest = Image((w, h))
    190247for y in range(h):
    191248    for x in range(w):
    192249        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)
     254dest.writePng("out008.png")
    208255
    209256import sys
     
    224271dest.writePng("pat003.png")
    225272
    226 
Note: See TracChangeset for help on using the changeset viewer.