Created
January 31, 2012 11:59
-
-
Save OniDaito/1710117 to your computer and use it in GitHub Desktop.
Images to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import os | |
import getopt | |
import tempfile | |
import shutil | |
import subprocess | |
from CoreFoundation import * | |
from Quartz.CoreGraphics import * | |
def writePageFromDoc(writeContext, doc, pageNum): | |
global verbose | |
page = CGPDFDocumentGetPage(doc, pageNum) | |
if page: | |
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox) | |
if CGRectIsEmpty(mediaBox): | |
mediaBox = None | |
CGContextBeginPage(writeContext, mediaBox) | |
CGContextDrawPDFPage(writeContext, page) | |
CGContextEndPage(writeContext) | |
print "Copied page %d from %s" % (pageNum, doc) | |
def is_image(f): | |
s = f.lower() | |
return 'jpg' in s or 'png' in s or 'bmp' in s | |
def get_immediate_images(dir): | |
return [ f for f in os.listdir(dir) if is_image(f) ] | |
def get_immediate_subdirectories(dir): | |
return [dir + "/" + name for name in os.listdir(dir) | |
if os.path.isdir(os.path.join(dir, name))] | |
def append(writeContext, docs, maxPages): | |
for doc in docs: | |
for pageNum in xrange(1, maxPages + 1) : | |
writePageFromDoc(writeContext, doc, pageNum) | |
def print_to_pdf(workingdir, path, image): | |
newimage = image.replace(' ','') | |
shutil.copy (path + '/' + image, workingdir + "/" + newimage) | |
subprocess.call(["/System/Library/Printers/Libraries/./convert", "-f", workingdir + "/" + newimage, '-o', workingdir + "/" + newimage + ".pdf"]) | |
return workingdir + "/" + newimage + ".pdf" | |
def main(argv): | |
# The PDF context we will draw into to create a new PDF | |
#writeContext = None | |
topdir = "" | |
# Parse the command line options | |
try: | |
options, args = getopt.getopt(argv, "d", ["directory="]) | |
except getopt.GetoptError: | |
usage() | |
sys.exit(2) | |
for option, arg in options: | |
if option in ("-d", "--directory") : | |
if verbose: | |
print "Setting %s as the directory to use." % (arg) | |
topdir = arg | |
else : | |
print "Unknown argument: %s" % (option) | |
if len(topdir) > 0: | |
# make a working directory | |
dirs = get_immediate_subdirectories (topdir) | |
for path in dirs: | |
workingdir = topdir + "/tmp" | |
os.mkdir(workingdir) | |
# create PDF Documents for all the directories found | |
basepath = os.path.basename(path) | |
newpdf = topdir + "/" + basepath + ".pdf" | |
writeContext = CGPDFContextCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, newpdf, len(newpdf), False), None, None) | |
if (writeContext): | |
images = get_immediate_images(path) | |
for image in images: | |
imagepdf = print_to_pdf(workingdir, path, image) | |
imagedoc = CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, imagepdf, len(imagepdf), False)) | |
page = CGPDFDocumentGetPage(imagedoc, 1) | |
if page: | |
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox) | |
if CGRectIsEmpty(mediaBox): | |
mediaBox = None | |
CGContextBeginPage(writeContext, mediaBox) | |
CGContextDrawPDFPage(writeContext, page) | |
CGContextEndPage(writeContext) | |
shutil.rmtree(workingdir) | |
#CGPDFContextClose(writeContext) | |
#CGContextRelease(writeContext) | |
#del writeContext | |
print "******" | |
print "Completed conversion of images to pdf with love and kisses from Ben!" | |
def usage(): | |
print "Usage: imagestopdf [--directory <dir>]" | |
if __name__ == "__main__": | |
main(sys.argv[1:]) | |
cmd = 'osascript -e \'tell app \"System Events\" to display dialog \" Hugs and Kisses from Ben to make mint20 go easier! \"\'' | |
os.system(cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment