Skip to content

Instantly share code, notes, and snippets.

@nikolajbaer
Created September 26, 2013 20:47
Show Gist options
  • Save nikolajbaer/6720307 to your computer and use it in GitHub Desktop.
Save nikolajbaer/6720307 to your computer and use it in GitHub Desktop.
converts a vertical image of a surfboard shape to a set of horizontal 1/2 size PDF pages ... using it to make a template for an alaia i am shaping in my garage
import Image,ImageDraw,optparse,pyPdf,math
parser = optparse.OptionParser()
parser.add_option("--width",action="store",dest="width",default=20,help="width of target board in inches",type="int")
parser.add_option("--paperwidth",action="store",dest="paperwidth",default=8,help="width of target image on paper",type="int")
parser.add_option("--dpi",action="store",dest="dpi",default=72,help="target dpi resolution",type="int")
(options,args) = parser.parse_args()
DPI = options.dpi
# split image in half
orig_im = Image.open(args[0])
im = orig_im.crop((0,0,orig_im.size[0]/2,orig_im.size[1]))
print "Cropping at ",orig_im.size[0]/2
target_width = float(options.width) / 2 # we are doing a 1/2 view
img_width,img_height = im.size
print "Image is %i x %i px (%s dpi)"%(img_width,img_height,orig_im.info["dpi"])
print "Target width is %f inches"%target_width
ratio = img_height/float(img_width)
target_height = ratio * target_width
print "Target height is %0.2f inches (%0.2f ft)"%(target_height,target_height/12)
# Scale image to be proper size at standard dpi (DPI,DPI)
big_im = im.resize((int(target_width*DPI),int(target_height*DPI)),Image.ANTIALIAS)
# TODO draw grid lines at 0.5"
# X
draw = ImageDraw.Draw(big_im)
for i in range(0,big_im.size[0],DPI):
draw.line(((i,0),(i,big_im.size[1])),fill=0)
# Y
for i in range(0,big_im.size[1],DPI):
draw.line(((0,i),(big_im.size[0],i)),fill=0)
big_im = big_im.rotate(270)
big_im.show()
# TODO chop up into page images
# Shoot for 8" wide segments
n = int(math.ceil(big_im.size[0]/DPI/options.paperwidth))
for i in range(n):
p = big_im.crop((i*n*DPI,0,(i+1)*n*DPI,big_im.size[1]))
p.save("output-%i.pdf"%i,"PDF",dpi=(DPI,DPI))
# TODO save pages as PDF with appropriate DPI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment