-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile_smooth.py
executable file
·125 lines (107 loc) · 3.81 KB
/
tile_smooth.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
# Interlaces slices from a timelapse series
#
# Copyright (C) 2011 Roger Barnes http://roger.mindsocket.com.au
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from PIL import Image, ImageChops
import os
from multiprocessing import Pool
import sys
class memoize:
# from http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
def __init__(self, function):
self.function = function
self.memoized = {}
def __call__(self, *args):
try:
return self.memoized[args]
except KeyError:
self.memoized[args] = self.function(*args)
return self.memoized[args]
@memoize
def getAlpha(gradient, size):
return gradient.resize(size)
def doTile(horiz, tile, files):
#files=files[2200:2500]
width, height = Image.open(files[0]).size
print "width, height:", width, height
print "Creating alpha gradient for blending slices"
if horiz:
gradient = Image.new('L', (1,5*256))
for x in range(512):
gradient.putpixel((0, x), x/2)
for x in range(256):
gradient.putpixel((0, x+2*256), 255)
for x in range(512):
gradient.putpixel((0, x+3*256), 255-(x/2))
else:
gradient = Image.new('L', (5*256,1))
for x in range(512):
gradient.putpixel((x, 0),x/2)
for x in range(256):
gradient.putpixel((x+2*256, 0),255)
for x in range(512):
gradient.putpixel((x+3*256, 0),255-(x/2))
image_count = len(files)
outwidth = width
outheight = height
if horiz:
outheight = image_count * tile
else:
outwidth = image_count * tile
print "out width, height:", outwidth, outheight
output_image = Image.new('RGBA', (outwidth, outheight))
for i in range(image_count):
print i, files[i]
offset = i * tile
if horiz:
start = float(i)/image_count * height
p0 = int(start - 2 * tile)
p1 = int(start + 3 * tile)
box = (0, p0, width, p1)
else:
start = float(i)/image_count * width
p0 = int(start - 2 * tile)
p1 = int(start + 3 * tile)
box = (p0, 0, p1, height)
print box
try:
im = Image.open(files[i]).crop(box)
except IOError:
print "brrp"
im = Image.open(files[i - 1]).crop(box)
# resize the gradient to the size of im...
alpha = getAlpha(gradient, im.size)
#temp_image = Image.new('RGBA', (outwidth, outheight))
if horiz:
#temp_image.paste(im, (0, offset))#, alpha)
output_image.paste(im, (0, offset), alpha)
else:
#temp_image.paste(im, (offset, 0))#, alpha)
output_image.paste(im, (offset, 0), alpha)
#output_image = ImageChops.lighter(output_image, temp_image)
print "Done"
print "Saving...",
output_image.save('tiled%05d.tif' % tile, 'TIFF')
print "Done"
if __name__ == '__main__':
index=1
horiz = sys.argv[index] == 'horiz'
if horiz:
index += 1
tile = int(sys.argv[index])
index += 1
files = sys.argv[index:]
doTile(horiz, tile, files)