Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from PIL import Image
import glob
import numpy as np
### TUKEY WINDOW
### https://leohart.wordpress.com/2006/01/29/hello-world/
def tukey_window(window_length, alpha=0.5):
# Special cases
if alpha <= 0:
return np.ones(window_length) #rectangular window
elif alpha >= 1:
return np.hanning(window_length)
# Normal case
x = np.linspace(0, 1, window_length)
w = np.ones(x.shape)
# first condition 0 <= x < alpha/2
first_condition = x<alpha/2
w[first_condition] = 0.5 * (1 + np.cos(2*np.pi/alpha * (x[first_condition] - alpha/2) ))
# second condition already taken care of
# third condition 1 - alpha / 2 <= x <= 1
third_condition = x>=(1 - alpha/2)
w[third_condition] = 0.5 * (1 + np.cos(2*np.pi/alpha * (x[third_condition] - 1 + alpha/2)))
return w
### END TUKEY WINDOW
slice_files = glob.glob("slice_*.jpg")
num_slices = len(slice_files)
print "num_slices", num_slices
probe = Image.open(slice_files[0])
slice_x = probe.size[0]
slice_y = probe.size[1]
### THIS IS THE TUNABLE BIT
overlap_factor = 0.5
###
real_x = slice_x / (1 + overlap_factor)
real_y = slice_y
total_x = int(real_x * num_slices)
total_y = int(real_y)
x_stride = int(real_x)
# window factor is "what proportion of the window should smoothly ramped"
# so 1.0 is a perfect belled hamming window
# and 0.0 is a rectangular window
window_factor = (slice_x - real_x) / real_x
# don't know why x and y are inverted here...??
mask_array = np.ndarray(shape=(slice_y, slice_x), dtype="uint8")
tukey_template = tukey_window(slice_x, window_factor) * 255
"""
import matplotlib.pyplot as plot
plot.plot(tukey_template)
axes = plot.gca()
margin = 10
axes.set_xlim([-margin,slice_x+margin])
axes.set_ylim([-margin,255+margin])
plot.show()
"""
for i in xrange(slice_y):
mask_array[i, :] = tukey_template
mask = Image.fromarray(mask_array, "L")
output_pano = Image.new("RGBA", (total_x, total_y))
for i in xrange(num_slices):
slice_img = Image.open(slice_files[i])
output_pano.paste(slice_img, (i*x_stride, 0), mask=mask)
output_pano.save("output_pano.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.