<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
 Load a sequence of FVF images, crop them and contrast them
 output them to be animated into a movie using ffmpeg.

 You must have ffmpeg installed:  see https://ffmpeg.org 

 At the moment, this only works on a mac; the system call to ffmpeg has
 to be enabled (or given permission) to run on Windows --- somehow.

 Stephen Morris July 2017

"""

from PIL import Image
from PIL import ImageEnhance

from os import listdir
from os import mkdir
from os import system
from os import chdir
from os import remove
from os import rmdir

path = './'  # path to raw image directory

raw_frame_dir = 'some_name'  # directory of raw images to animate

mkdir(path+raw_frame_dir+'_frames')  # temporary directory for frames

fname_root = raw_frame_dir # root of name to give the temporary frames

top_crop 	= 85    #  crop edges just to exclude handles etc
bot_crop 	= 11
left_crop	= 330   # kludged to make multiples of 16 for better compression
right_crop 	= 150 

iframe = 1   # index from frames

file_list = listdir(path+raw_frame_dir)  
file_list = sorted([name for name in file_list if (name.endswith('.tif') and not name.startswith('.'))])  
# pick out only tifs and not hidden files (on mac).  
# Files are sorted by frame number as long as only one shot of the camera is in the directory.

for raw_frame in file_list:

	print ('working on frame '+str(iframe))

	f = Image.open(path+raw_frame_dir+'/'+raw_frame)

	width = f.size[0]
	height = f.size[1]

	fcrop = f.crop((left_crop, top_crop, width-right_crop, height-bot_crop))

	contrast = ImageEnhance.Contrast(fcrop)   # adjust contrast
	fcrop = contrast.enhance(2)

	bright = ImageEnhance.Brightness(fcrop)   # adjust brightness
	fcrop = bright.enhance(1.4)

	fcrop.save(path+raw_frame_dir+'_frames/' + fname_root + '.' + str(iframe) + '.jpg')

	iframe += 1

	f.close()
	fcrop.close()

frame_rate = 20   # frames per second

# this system call runs ffmeg --- you must have ffmpeg installed:
# see https://ffmpeg.org to download it

# At the moment, this does not work on Windows without some special tweak

system('ffmpeg -r '+str(frame_rate) +' -i ' + path+raw_frame_dir+'_frames/'+fname_root+'.%d.jpg'+ ' -vcodec h264 -b 12000 ' + fname_root + '.mp4' )

chdir(path+raw_frame_dir+'_frames/')

for crop_frame in listdir('.'):   # delete the temporary frame files

	remove(crop_frame)
	
chdir('..')
rmdir(path+raw_frame_dir+'_frames/')
	</pre></body></html>