User:Eleanorg/1.3/Embedding/Code1

From XPUB & Lens-Based wiki

Spits out Imagemagick commands that will take a series of crops from an image you give it. Pipe to bash to run.

#!/usr/bin/python
#-*- coding:utf-8 -*-

import os

#--------------- calculate image width & height:

widthCommand = "identify -format %w photo3.JPG" 
heightCommand = "identify -format %h photo3.JPG"
widthFind = os.popen(widthCommand,'r')            
heightFind = os.popen(heightCommand,'r')  
    
width = float(widthFind.read())
height = float(heightFind.read())


#--------------- determine what 1% of width and height are in pixels
# (to allow for use with pixels-only imagemagick arguments). conversion to int rounds to nearest pixel

onex = int(width/100)
oney = int(height/100)


##--------------- put offset positions into variables
## to set an offset of 50% from the left of the image, use: 
## -crop 20%x+ (onex x 50)

# crop 2 - aligned to top right corner
crop2x = str(onex * 40)
crop2y = str(0)

# crop 3 - aligned to bottom right corner
crop3x = str(onex * 40)
crop3y = str(oney * 40)

#crop 4 - aligned to bottom left corner
crop2y = str(0)
crop2y = str(oney * 40)

# crop 5: 40%, centred. (30 is half of 100-40)
crop5x = str(onex * 30)
crop5y = str(oney * 30)

crop4x = str(0)
crop4y = str(oney * 100)

#--------------- take a series of crops 
print """
convert photo.JPG -crop 60%x+0+0 crop1.JPG \
convert photo.JPG -crop 60%x+""" + crop2x + "+" + crop2y + """ crop2.JPG \
convert photo.JPG -crop 40%x+""" + crop3x + "+" + crop3y + """ crop3.JPG \ 
convert photo.JPG -crop 60%x+""" + crop4x + "+" + crop4y + """ crop4.JPG \
convert photo.JPG -crop 60%x+""" + crop5x + "+" + crop5y + """ crop4.JPG \
"""