Guest User

Untitled

a guest
May 19th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Watermark each page in a PDF document
  3.  
  4. import sys #, os
  5. import getopt
  6. import math
  7. from Quartz.CoreGraphics import *
  8. from Quartz.ImageIO import *
  9.  
  10. def drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity):
  11. if image:
  12. imageWidth = CGImageGetWidth(image)
  13. imageHeight = CGImageGetHeight(image)
  14. imageBox = CGRectMake(0, 0, imageWidth, imageHeight)
  15.  
  16. CGContextSaveGState(ctx)
  17. CGContextSetAlpha(ctx, opacity)
  18. CGContextTranslateCTM(ctx, xOffset, yOffset)
  19. CGContextScaleCTM(ctx, scale, scale)
  20. CGContextTranslateCTM(ctx, imageWidth / 2, imageHeight / 2)
  21. CGContextRotateCTM(ctx, angle * math.pi / 180)
  22. CGContextTranslateCTM(ctx, -imageWidth / 2, -imageHeight / 2)
  23. CGContextDrawImage(ctx, imageBox, image)
  24. CGContextRestoreGState(ctx)
  25.  
  26. def createImage(imagePath):
  27. image = None
  28.  
  29. # provider = CGDataProviderCreateWithFilename(imagePath) # FIXED: replaced by the following CGDataProviderCreateWithURL()
  30. url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, imagePath, len(imagePath), False)
  31. provider = CGDataProviderCreateWithURL(url)
  32.  
  33. if provider:
  34. imageSrc = CGImageSourceCreateWithDataProvider(provider, None)
  35. if imageSrc:
  36. image = CGImageSourceCreateImageAtIndex(imageSrc, 0, None)
  37. if not image:
  38. print "Cannot import the image from file %s" % imagePath
  39. return image
  40.  
  41. def watermark(inputFile, watermarkFiles, outputFile, under, xOffset, yOffset, angle, scale, opacity, verbose):
  42.  
  43. images = map(createImage, watermarkFiles)
  44.  
  45. ctx = CGPDFContextCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outputFile, len(outputFile), False), None, None)
  46. if ctx:
  47. pdf = CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, inputFile, len(inputFile), False))
  48. if pdf:
  49.  
  50. for i in range(1, CGPDFDocumentGetNumberOfPages(pdf) + 1):
  51. image = images[i % len(images) - 1]
  52. page = CGPDFDocumentGetPage(pdf, i)
  53. if page:
  54. mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
  55. if CGRectIsEmpty(mediaBox):
  56. mediaBox = None
  57.  
  58. CGContextBeginPage(ctx, mediaBox)
  59. if under:
  60. drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity)
  61. CGContextDrawPDFPage(ctx, page)
  62. if not under:
  63. drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity)
  64. CGContextEndPage(ctx)
  65.  
  66. del pdf
  67. CGPDFContextClose(ctx)
  68. del ctx
  69.  
  70. def main(argv):
  71.  
  72. verbose = False
  73. readFilename = None
  74. writeFilename = None
  75. under = False
  76. xOffset = 0.0 # FIXED: changed to float value
  77. yOffset = 0.0 # FIXED: changed to float value
  78. angle = 0.0 # FIXED: changed to float value
  79. scale = 1.0 # FIXED: added
  80. opacity = 1.0
  81.  
  82. # Parse the command line options
  83. try:
  84. options, args = getopt.getopt(argv, "vutx:y:a:p:s:i:o:", ["verbose", "under", "over", "xOffset=", "yOffset=", "angle=", "opacity=", "scale=", "input=", "output=", ])
  85. except getopt.GetoptError:
  86. usage()
  87. sys.exit(2)
  88.  
  89. for option, arg in options:
  90.  
  91. print option, arg
  92.  
  93. if option in ("-i", "--input") :
  94. if verbose:
  95. print "Reading pages from %s." % (arg)
  96. readFilename = arg
  97.  
  98. elif option in ("-o", "--output") :
  99. if verbose:
  100. print "Setting %s as the output." % (arg)
  101. writeFilename = arg
  102.  
  103. elif option in ("-v", "--verbose") :
  104. print "Verbose mode enabled."
  105. verbose = True
  106.  
  107. elif option in ("-u", "--under"):
  108. print "watermark under PDF"
  109. under = True
  110.  
  111. elif option in ("-t", "--over"): # FIXED: changed to "-t" from "t"
  112. print "watermark over PDF"
  113. under = False
  114.  
  115. elif option in ("-x", "--xOffset"):
  116. xOffset = float(arg)
  117.  
  118. elif option in ("-y", "--yOffset"):
  119. yOffset = float(arg)
  120.  
  121. elif option in ("-a", "--angle"):
  122. angle = -float(arg)
  123.  
  124. elif option in ("-s", "--scale"):
  125. scale = float(arg)
  126.  
  127. elif option in ("-p", "--opacity"):
  128. opacity = float(arg)
  129.  
  130. else:
  131. print "Unknown argument: %s" % (option)
  132.  
  133. if (len(args) > 0):
  134. watermark(readFilename, args, writeFilename, under, xOffset, yOffset, angle, scale, opacity, verbose);
  135. else:
  136. shutil.copyfile(readFilename, writeFilename);
  137.  
  138. def usage():
  139. print "Usage: watermark --input <file> --output <file> <watermark files>..."
  140.  
  141. if __name__ == "__main__":
  142. print sys.argv
  143. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment