I find this code on stackoverflow in Swift, so I made myself the challenge to convert it to ASObjC.
My approach to get theWidth and theHeight from sourceImage is properly not the best way.
The source code of Swift example is included.
The default values for scale and aspectRatio = 1.0
You could see in the log what whose values are.
Change the targetSize of width and height
use framework "Foundation"
use framework "CoreImage"
use scripting additions
(**
* import CoreImage
*
* let context = CIContext()
* let imageURL = URL(fileURLWithPath: "sample.jpg")
* let sourceImage = CIImage(contentsOf: imageURL, options: nil)
* let resizeFilter = CIFilter(name:"CILanczosScaleTransform")!
*
* // Desired output size
* let targetSize = NSSize(width:800, height:600)
*
* // Compute scale and corrective aspect ratio
* let scale = targetSize.height / (sourceImage?.extent.height)!
* let aspectRatio = targetSize.width/((sourceImage?.extent.width)! * scale)
*
* // Apply resizing
* resizeFilter.setValue(sourceImage, forKey: kCIInputImageKey)
* resizeFilter.setValue(scale, forKey: kCIInputScaleKey)
* resizeFilter.setValue(aspectRatio, forKey: kCIInputAspectRatioKey)
* let outputImage = resizeFilter.outputImage
*)
set thePath to POSIX path of (choose file)
set imageURL to current application's |NSURL|'s fileURLWithPath:thePath
set sourceImage to current application's CIImage's imageWithContentsOfURL:imageURL options:(missing value)
set targetSize to current application's NSMakeSize(512, 750)
set {theWidth, theHeight} to {item 1 of item 2 of sourceImage's extent(), item 2 of item 2 of sourceImage's extent()}
set scale to (targetSize's height) / (theHeight)
set aspectRatio to (targetSize's width) / ((theWidth) * scale)
log {scale, aspectRatio}
set resizeFilter to current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputImage:sourceImage, inputScale:scale, inputAspectRatio:aspectRatio}
set outputImage to resizeFilter's outputImage()
set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:outputImage
set imageData to imageRep's TIFFRepresentation()
imageData's writeToFile:(POSIX path of (path to desktop) & "imageSize.tif") atomically:true
We could also use a scale factor with CIImage class with affine transformation matrix.
Reference: https://developer.apple.com/documentation/coregraphics/1455016-cgaffinetransformmakescale
use framework "Foundation"
use framework "CoreImage"
use scripting additions
property outputImage : POSIX path of (path to desktop) & "imageSize.tif"
set thePath to POSIX path of (choose file)
set imageURL to current application's |NSURL|'s fileURLWithPath:thePath
set sourceImage to current application's CIImage's imageWithContentsOfURL:imageURL options:(missing value)
-- Default scale factor
transformScale(sourceImage, outputImage, 1.0, 1.0)
on transformScale(inputImage, outputImage, scaleFactorSX, scaleFactorSY)
set inputImage to inputImage's imageByApplyingTransform:(current application's CGAffineTransformMakeScale(scaleFactorSX, scaleFactorSY))
set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:inputImage
set imageData to imageRep's TIFFRepresentation()
imageData's writeToFile:outputImage atomically:true
end transformScale
Or we could do something like this
use framework "Foundation"
use framework "CoreImage"
use scripting additions
property outputImage : POSIX path of (path to desktop) & "imageSize.tif"
set thePath to POSIX path of (choose file)
set imageURL to current application's |NSURL|'s fileURLWithPath:thePath
set sourceImage to current application's CIImage's imageWithContentsOfURL:imageURL options:(missing value)
applyingOrientation(sourceImage, outputImage, "left")
on applyingOrientation(inputImage, outputImage, imageOrientation)
if imageOrientation = "up" then
set inputImage to inputImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationUp)
else if imageOrientation = "down" then
set inputImage to inputImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationDown)
else if imageOrientation = "left" then
set inputImage to inputImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationLeft)
else if imageOrientation = "right" then
set inputImage to inputImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationRight)
end if
set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:inputImage
set imageData to imageRep's TIFFRepresentation()
imageData's writeToFile:outputImage atomically:true
end applyingOrientation
More useful to have handlers for other projects.
use framework "Foundation"
use framework "CoreImage"
use scripting additions
property outputImage : POSIX path of (path to desktop) & "imageSize.tif"
set thePath to POSIX path of (choose file)
set imageURL to current application's |NSURL|'s fileURLWithPath:thePath
set sourceCIImage to current application's CIImage's imageWithContentsOfURL:imageURL options:(missing value)
set theCIImage to applyingOrientation(sourceCIImage, "up")
saveTIFFFromCIImage(theCIImage, outputImage)
(**
* [applyingOrientation(inputCIImage, imageOrientation)]
**
* imageOrientation: up, down, left and right, with mirror: upM,downM,leftM and rightM
*)
on applyingOrientation(inputCIImage, imageOrientation)
if imageOrientation = "up" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationUp)
else if imageOrientation = "down" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationDown)
else if imageOrientation = "left" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationLeft)
else if imageOrientation = "right" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationRight)
else if imageOrientation = "upM" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationUpMirrored)
else if imageOrientation = "downM" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationDownMirrored)
else if imageOrientation = "leftM" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationLeftMirrored)
else if imageOrientation = "rightM" then
set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationRightMirrored)
end if
return inputImage
end applyingOrientation
(**
* [saveTIFFFromCIImage(inputCIImage, outputImageTIFF)]
*)
on saveTIFFFromCIImage(inputCIImage, outputImageTIFF)
set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:inputCIImage
set imageData to imageRep's TIFFRepresentation()
imageData's writeToFile:outputImageTIFF atomically:true
end saveTIFFFromCIImage