spacepaste

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Image
import sys
import xlwt

# The 64 colors used by Excel for cell backgrounds.
xlwt_colors = [
    (0x00,0x00,0x00), (0xFF,0xFF,0xFF), (0xFF,0x00,0x00), (0x00,0xFF,0x00),
    (0x00,0x00,0xFF), (0xFF,0xFF,0x00), (0xFF,0x00,0xFF), (0x00,0xFF,0xFF),
    (0x00,0x00,0x00), (0xFF,0xFF,0xFF), (0xFF,0x00,0x00), (0x00,0xFF,0x00),
    (0x00,0x00,0xFF), (0xFF,0xFF,0x00), (0xFF,0x00,0xFF), (0x00,0xFF,0xFF),
    (0x80,0x00,0x00), (0x00,0x80,0x00), (0x00,0x00,0x80), (0x80,0x80,0x00),
    (0x80,0x00,0x80), (0x00,0x80,0x80), (0xC0,0xC0,0xC0), (0x80,0x80,0x80),
    (0x99,0x99,0xFF), (0x99,0x33,0x66), (0xFF,0xFF,0xCC), (0xCC,0xFF,0xFF),
    (0x66,0x00,0x66), (0xFF,0x80,0x80), (0x00,0x66,0xCC), (0xCC,0xCC,0xFF),
    (0x00,0x00,0x80), (0xFF,0x00,0xFF), (0xFF,0xFF,0x00), (0x00,0xFF,0xFF),
    (0x80,0x00,0x80), (0x80,0x00,0x00), (0x00,0x80,0x80), (0x00,0x00,0xFF),
    (0x00,0xCC,0xFF), (0xCC,0xFF,0xFF), (0xCC,0xFF,0xCC), (0xFF,0xFF,0x99),
    (0x99,0xCC,0xFF), (0xFF,0x99,0xCC), (0xCC,0x99,0xFF), (0xFF,0xCC,0x99),
    (0x33,0x66,0xFF), (0x33,0xCC,0xCC), (0x99,0xCC,0x00), (0xFF,0xCC,0x00),
    (0xFF,0x99,0x00), (0xFF,0x66,0x00), (0x66,0x66,0x99), (0x96,0x96,0x96),
    (0x00,0x33,0x66), (0x33,0x99,0x66), (0x00,0x33,0x00), (0x33,0x33,0x00),
    (0x99,0x33,0x00), (0x99,0x33,0x66), (0x33,0x33,0x99), (0x33,0x33,0x33),
]

def color_dist((R0, G0, B0), (R1, G1, B1)):
    return abs(R0 - R1) + abs(G0 - G1) + abs(B0 - B1)

def nearest_xlwt(c0):
    # Return the index of the Excel color nearest to c0.
    best_col = best_dist = None
    for i, c1 in enumerate(xlwt_colors):
        d = color_dist(c0, c1)
        if best_dist is None or d < best_dist:
            best_col, best_dist = i, d
    return best_col

def to_rgb((R, G, B, A)):
    # Mix alpha values with white.
    s = lambda x: (255 * (255 - A) + x * A) // 255
    return tuple(map(s, [R, G, B]))

def write_spreadsheet(image_name, output_file):
    # Convert indexed images and resize images if they're huge.
    im = Image.open(image_name).convert('RGBA')
    if im.size[0] > 255:
        im = im.resize((255, im.size[1] * 255 // im.size[0]))

    width, height = im.size
    im = im.getdata()

    # Make an xls file and define some styles for it.
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet(image_name)

    color_styles = []
    for i, _ in enumerate(xlwt_colors):
        st = xlwt.easyxf('pattern: pattern solid;')
        st.pattern.pattern_fore_colour = i
        color_styles.append(st)

    # Copy the image column by column.
    for x in xrange(width):
        # 0.5 cm (row height) -- make sure the output looks square.
        sheet.col(x).width = 658
        for y in xrange(height):
            col = nearest_xlwt(to_rgb(im[y * width + x]))
            sheet.write(y, x, '', color_styles[col])

    wbk.save('output.xls')

if __name__ == '__main__':
    if len(sys.argv) < 2:
        sys.exit('usage: python %s image [outfile]' % sys.argv[0])
    output_file = sys.argv[2] if len(sys.argv) >= 3 else 'output.xls'
    write_spreadsheet(sys.argv[1], output_file)