Skip to content

Instantly share code, notes, and snippets.

@riccardomarotti
Last active September 9, 2016 12:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardomarotti/4737373 to your computer and use it in GitHub Desktop.
Save riccardomarotti/4737373 to your computer and use it in GitHub Desktop.
Resizes images passed as arguments to RESIZE_WIDTH x RESIZE_HEIGTH, stretching source image. Meant to be used in Automator.
#! /usr/bin/python
import sys
import CoreGraphics
RESIZE_WIDTH = 100
RESIZE_HEIGHT = 100
def resizeimage(source_image_file, target_image_file, target_width, target_height):
source_image = CoreGraphics.CGImageImport (CoreGraphics.CGDataProviderCreateWithFilename(source_image_file))
source_width = source_image.getWidth()
source_height = source_image.getHeight()
source_image_rect = CoreGraphics.CGRectMake(0, 0, source_width, source_height)
new_image = source_image.createWithImageInRect(source_image_rect)
colors_space = CoreGraphics.CGColorSpaceCreateDeviceRGB()
colors = CoreGraphics.CGFloatArray(5)
context = CoreGraphics.CGBitmapContextCreateWithColor(target_width, target_height, colors_space, colors)
context.setInterpolationQuality(CoreGraphics.kCGInterpolationHigh)
new_image_rect = CoreGraphics.CGRectMake(0, 0, target_width, target_height)
context.drawImage(new_image_rect, new_image)
context.writeToFile(target_image_file, CoreGraphics.kCGImageFormatJPEG)
def main(argv):
for image_file_name in argv:
resizeimage(image_file_name, image_file_name, RESIZE_WIDTH, RESIZE_HEIGHT)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment