Images

import os , errno

from PIL import Image , ImageFont , ImageDraw testImage = 'test.png' cwd = os.getcwd()

image_dir = os.path.join(cwd , 'images' )

default_dir = os.path.join(cwd , 'processed' ) try : os.makedirs(default_dir) except OSError as e: if e.errno != errno.EEXIST: raise

def processImage (imageFile): # Resize image maxsize = ( 1000 , 1000 ) im = Image.open(imageFile) im.thumbnail(maxsize) im.show()

def processImage (imageFile): # Resize image ... # Crop center of an image out halfCropArea = 300 halfWidth = im.size[ 0 ] / 2 halfHeight = im.size[ 1 ] / 2 startWidth = halfWidth - halfCropArea startHeight = halfHeight - halfCropArea cropWidth = halfWidth + halfCropArea cropHeight = halfHeight + halfCropArea cropped = im.crop((startWidth , startHeight , cropWidth , cropHeight)) cropped.show()

def processImage (imageFile): ...

# Apply greyscale greyscale = cropped.convert( 'LA' ) img = greyscale.convert( 'RGB' )

def processImage (imageFile): ...

# Add icon iconImage = Image.open( 'icon.png' ) iconImage.thumbnail(( 144 , 144 )) img.paste(iconImage , ( 228 , 228 , 228 + 144 , 228 + 144 )) img.show()

Movie Time

def processFolder (): images = os.listdir(image_dir) for count , image in enumerate (images): filename = '%04d.png' % count imageFile = os.path.join(image_dir , image) print filename , imageFile processImage(imageFile , filename) processFolder()

os.system( './ffmpeg -f image2 -r 2 -i ./processed/%04d.jpg -y -an -vf fps=30 -crf 25 -vcodec libx264 ./processed/video.mp4' )

I am in the middle of building an app and need to put together several promo videos for Twitter and Instagram, and man it is boring. So I thought 'how can I automate this process?', so welcome to this post.What I am going for is a selection of images, greyscaled flipping through with our logo on the front. So my approach was to use python, pillow and ffmpeg to build something. This could be disastrous.To start a few pre-flight checks, a variable for the test image and to check for/create a processed folder for the final images to go.I am going to build a function that will do all of the processing, but first going to run on one image before looping through all of the one I need. The first process is to resize image. This is just going to social media promo video so I don't want massive images. I do this simply with the thumbnail method. Using im.show() will display the image on your OS, on mac will open in preview.Next I want to crop out the centre to give me a nice square image. The final square image is gonna be 600px x 600px. The crop function will take the top left corner and crop a box of dimension. I have the latter but to get the start location I will have to get the size of the height of the image, divid by 2 and minus 300px, then the same for the width.Next I need to convert to greyscale. will simply use a convert but will look in to filters another time.Finally to add the logo which is set to 144px square. To set in the middle I set (600 -144)/2 = 228 to top left and then 228 + 144 for bottom right.Now I am gonna save to the new folder and also added an argument to the function for a new filename.To get all of the images processed, I am gonna look at the folder with all of the images in and then loop through them. These going to be the frames of my video.Inside the directory with my script I have put the ffmpeg library and just run the command form the script.And Voila!!!