Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import argparse
import os
import subprocess
import sys
from wand.image import Color, Image, COMPOSITE_OPERATORS
from wand.drawing import Drawing
from wand.display import display
parser = argparse.ArgumentParser()
parser.add_argument('project', help="Path to directory containing raw/ folder")
parser.add_argument('--reverse',
help='Reverse the order of the images',
action='store_true')
parser.add_argument('--slices_per_image', type=int, default=3)
parser.add_argument('--starting_alpha', type=int, default=50)
args = parser.parse_args()
def make_translucent(image, alpha_percent):
with Image(width=image.width,
height=image.height,
background=Color("gray%d" % alpha_percent)
) as alpha:
image.composite_channel(
'default_channels', alpha, 'copy_opacity', 0, 0
)
raw_dir = os.path.join(args.project, 'raw')
image_files = sorted(os.listdir(raw_dir), reverse=args.reverse)
num_files = len(image_files)
_, file_extension = os.path.splitext(image_files[0])
first_image = Image(filename=os.path.join(raw_dir, image_files[0]))
width = first_image.width
height = first_image.height
cropped_width = width / num_files
print "Cropped width", cropped_width
slice_width = cropped_width / args.slices_per_image
print "Slice width", slice_width
alpha_delta = (100 - args.starting_alpha) / (args.slices_per_image - 1)
start_x = 0
for image_file in image_files[1:]:
# First, get the piece of this image that will be used.
image_piece = Image(filename=os.path.join(raw_dir, image_file))
end_x = start_x + cropped_width * 2
image_piece.crop(start_x, 0, end_x, height)
for slice_i in xrange(args.slices_per_image):
slice_start_x = slice_i * slice_width
slice_alpha_percent = args.starting_alpha + alpha_delta * slice_i
slice_end_x = slice_start_x + slice_width
slice_image = image_piece.clone()
slice_image.crop(
slice_start_x,
0,
slice_end_x,
height
)
if slice_alpha_percent < 100:
make_translucent(slice_image, slice_alpha_percent)
first_image.composite(
image=slice_image,
left=start_x + slice_start_x,
top=0
)
image_piece.crop(cropped_width, 0, cropped_width * 2, height)
first_image.composite(
image=image_piece,
left=start_x + cropped_width,
top=0
)
start_x += cropped_width
filename = 'output_{slices}_{alpha}{ext}'.format(
slices=args.slices_per_image,
alpha=args.starting_alpha,
ext=file_extension
)
if args.reverse:
filename = 'reverse_' + filename
output_filename = os.path.join(args.project, filename)
print "Saved to %s" % output_filename
first_image.save(filename=output_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment