forked from cortesi/scurve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap
executable file
·79 lines (69 loc) · 2.1 KB
/
colormap
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
76
77
78
#!/usr/bin/env python
import os.path, math
import scurve
from scurve import progress
import Image, ImageDraw
def sortedPixels(csource, img, quiet):
img = Image.open(img, "r")
decorated = []
if quiet:
prog = progress.Dummy()
else:
prog = progress.Progress(
img.size[0]*img.size[1],
"Analysing image"
)
for x in range(img.size[0]):
for y in range(img.size[1]):
p = img.getpixel((x, y))
decorated.append(
(csource.index(p), p)
)
if not x*y%100:
prog.tick(x*y)
decorated.sort()
return [i[1] for i in decorated]
def drawmap(map, pixels, name, quiet):
c = Image.new("RGB", map.dimensions())
cd = ImageDraw.Draw(c)
step = len(pixels)/float(len(map))
if quiet:
prog = progress.Dummy()
else:
prog = progress.Progress(len(map), "Creating swatch")
for i, p in enumerate(map):
color = pixels[int(i*step)]
cd.point(tuple(p), fill=tuple(color))
if not i%100:
prog.tick(i)
c.save(name)
def main():
from optparse import OptionParser, OptionGroup
parser = OptionParser(
usage = "%prog [options] image_path output_path",
version="%prog 0.1",
)
parser.add_option(
"-c", "--colorsource", action="store",
type="str", dest="colorsource", default="hilbert"
)
parser.add_option(
"-m", "--map", action="store",
type="str", dest="map", default="hilbert"
)
parser.add_option(
"-s", "--size", action="store",
type="int", dest="size", default=256
)
parser.add_option(
"-q", "--quiet", action="store_true",
dest="quiet", default=False
)
options, args = parser.parse_args()
if len(args) != 2:
parser.error("Please specify output file.")
csource = scurve.fromSize(options.colorsource, 3, 256**3)
map = scurve.fromSize(options.map, 2, options.size**2)
p = sortedPixels(csource, args[0], options.quiet)
drawmap(map, p, args[1], options.quiet)
main()