Wednesday, December 8, 2021

#1 2021-10-14 02:57:21 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Resizing an image

I'm working to gain a basic understanding of NSImage, and I"m currently working with the script included below. It resizes the source image but in every case the newly-created image has a width of 2000 pixels, which is twice the desired result. Why is this?

Applescript:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set sourceFile to POSIX path of (choose file)
set targetFile to POSIX path of ((path to desktop as text) & "Reduced Image.png")

resizeImage(sourceFile, targetFile, 1000, 750)

on resizeImage(sourceFile, targetFile, newWidth, newHeight)
   set sourceImage to current application's NSImage's alloc()'s initWithContentsOfFile:sourceFile
   set scaledImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(newWidth, newHeight))
   scaledImage's lockFocus()
   sourceImage's setSize:(current application's NSMakeSize(newWidth, newHeight))
   (current application's NSGraphicsContext's currentContext())'s setImageInterpolation:(current application's NSImageInterpolationHigh)
   sourceImage's drawInRect:(current application's NSMakeRect(0.0, 0.0, newWidth, newHeight))
   scaledImage's unlockFocus()
   set theData to current application's NSBitmapImageRep's imageRepWithData:(scaledImage's TIFFRepresentation)
   set theRep to theData's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value)
   theRep's writeToFile:targetFile atomically:true
end resizeImage

The above script is a simple rewrite of the script in the following thread, and I do not really understand how it works. Perhaps I made an edit that is causing this isue.

https://macscripter.net/viewtopic.php?pid=187419

Last edited by peavine (2021-10-14 04:46:10 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2021-10-14 05:19:25 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6730

Re: Resizing an image

The size property of an image is the size in points the image should appear at. In the case of bitmaps, pre-retina screens this was generally the same as the number of pixels when the image was a bitmap, but now it is more often half that value.

For bitmaps, you need to get the pixelsHigh and/or pixelsWide properties of the bitmaps, and use those values.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#3 2021-10-14 06:23:56 pm

technomorph
Member
Registered: 2017-12-14
Posts: 219

Re: Resizing an image

If you want to keep the image proportionally scaled you just want to provide a new width.

here's my Objective C code that I use to scale a new image to a provided width in pixels.
NOTE this is a Extension on NSImage so references to SELF would be your original image.

Applescript:

- (NSImage*)scaledImageForPix:(NSUInteger)aSize {
NSSize imageSize = [self size];
NSImage *squareImage = [[NSImage alloc] initWithSize:NSMakeSize(imageSize.width, imageSize.width)];
NSImage *scaledImage = nil;
NSRect drawRect;

NSSize scaledSize = NSMakeSize(aSize, aSize);

// make the image square
if ( imageSize.height > imageSize.width )
{
drawRect = NSMakeRect(0, imageSize.height - imageSize.width, imageSize.width, imageSize.width);
}
else
{
drawRect = NSMakeRect(0, 0, imageSize.height, imageSize.height);
}

// use native square size if passed zero size
if ( NSEqualSizes(scaledSize, NSZeroSize) )
{
scaledSize = drawRect.size;
}

scaledImage = [[NSImage alloc] initWithSize:scaledSize];

[squareImage lockFocus];
[self drawInRect:NSMakeRect(0, 0, imageSize.width, imageSize.width) fromRect:drawRect operation:NSCompositeSourceOver fraction:1.0];
[squareImage unlockFocus];

// scale the image to the desired size

[scaledImage lockFocus];
[squareImage drawInRect:NSMakeRect(0, 0, scaledSize.width, scaledSize.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[scaledImage unlockFocus];

// convert back to readable bitmap data

CGImageRef cgImage = [scaledImage CGImageForProposedRect:NULL context:nil hints:nil];
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
NSImage *finalImage = [[NSImage alloc] initWithSize:scaledImage.size];
[finalImage addRepresentation:bitmapRep];
return finalImage;
}

Online

 

#4 2021-10-14 06:42:15 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

Thanks Shane and technomorph for your responses. I appreciate your help.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2021-10-14 07:02:32 pm

technomorph
Member
Registered: 2017-12-14
Posts: 219

Re: Resizing an image

Also maybe the sourceImage size isn't being changed because you've locked focus on the scaledImage.

Online

 

#6 2021-10-14 07:04:10 pm

technomorph
Member
Registered: 2017-12-14
Posts: 219

Re: Resizing an image

And here's some code that will create a new rect with proportion scaling (IE may distort)

(NOTE this is note my code)
Note your should be able to substitute all the CG's for NS as they are bridged.
For the std::min function you'll have to do a if / then and compare the widthFactor, heightFactor.
And I would probably create the NSRect before returning it.

Applescript:


- (CGRect) proportionallyScale:(CGSize)fromSize toSize:(CGSize)toSize
{
CGPoint origin = CGPointZero;
CGFloat width = fromSize.width, height = fromSize.height;
CGFloat targetWidth = toSize.width, targetHeight = toSize.height;

float widthFactor = targetWidth / width;
float heightFactor = targetHeight / height;

CGFloat scaleFactor = std::min(widthFactor, heightFactor);

CGFloat scaledWidth = width * scaleFactor;
CGFloat scaledHeight = height * scaleFactor;

if (widthFactor < heightFactor)
origin.y = (targetHeight - scaledHeight) / 2.0;
else if (widthFactor > heightFactor)
origin.x = (targetWidth - scaledWidth) / 2.0;
return {origin, {scaledWidth, scaledHeight}};
}

Last edited by technomorph (2021-10-14 07:12:02 pm)

Online

 

#7 2021-10-14 07:15:18 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

technomorph wrote:

Also maybe the sourceImage size isn't being changed because you've locked focus on the scaledImage.



The dimensions of my source image are 3264 x 2448, and the dimensions of the image created by my script are 2000 x 1500. So, the image size is being changed.

Thanks for the code suggestion. I'll try to work that into my script.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#8 2021-10-14 07:36:17 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6730

Re: Resizing an image

Here's a slightly different approach:

Applescript:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set sourceFile to POSIX path of (choose file)
set targetFile to POSIX path of ((path to desktop as text) & "Reduced Image.png")

resizeImage(sourceFile, targetFile, 1000, 750)

on resizeImage(sourceFile, targetFile, newWidth, newHeight)
   set sourceImage to current application's NSImage's alloc()'s initWithContentsOfFile:sourceFile
   set aRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:newWidth pixelsHigh:newHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
   set newSize to {width:newWidth, height:newHeight}
   aRep's setSize:newSize
   
   current application's NSGraphicsContext's saveGraphicsState()
   set theContext to current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep
   current application's NSGraphicsContext's setCurrentContext:theContext
   theContext's setShouldAntialias:false -- change to suit
   theContext's setImageInterpolation:(current application's NSImageInterpolationNone) -- change to suit
   sourceImage's drawInRect:(current application's NSMakeRect(0, 0, newWidth, newHeight)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:(1.0)
   current application's NSGraphicsContext's restoreGraphicsState()
   
   set theData to aRep's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value)
   theData's writeToFile:targetFile atomically:true
end resizeImage


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#9 2021-10-15 07:43:08 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

Shane. I tested your script on a number of images and it worked great. I read through the script and have a general understanding of its operation, and I'll have a good understanding after spending some time with the documentation. Many thanks.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#10 2021-10-16 10:25:47 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

I looked at a script written by Fredrik71 in the thread linked below. I was curious how this script compared with the script included above and with a script that uses the sips utility. My test image was a photo taken with a Pixel 4a phone, but I ran additional tests with many other images from various sources. The goal was to reduce the width of the photo to 1000 pixels.

The results were:

SCRIPT - FILE SIZE - TIMING RESULT

AppKit - 102KB - 52 milliseconds
CoreImage 113KB - 78 milliseconds
Sips - 160KB - 87 milliseconds

None of the above seems very significant, but I was surprised that the image created with the AppKit script was degraded as compared with the original with a somewhat dark cast and a lack of definition. The other scripts returned photos that were not at all degraded. Perhaps my implementation of the AppKit script is erroneous in some respect. It's important to note that the AppKit and CoreImage scripts do not include Exif and other significant metadata in the newly-created images.

Most users are best advised to use sips or Image Events or one of the many utilities that will resize images. For learning purposes, I find this topic of interest.

The AppKit script is:

Applescript:

-- Revised 2021.10.18 to fix lack of definition and dark color cast

use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set sourceFile to POSIX path of (choose file)
   set targetFile to POSIX path of ((path to desktop as text) & "Reduced Image.jpg")
   set {existingWidth, existingHeight} to getImageSize(sourceFile)
   set newWidth to 1000
   set newHeight to round ((newWidth / existingWidth) * existingHeight)
   
   set sourceImage to current application's NSImage's alloc()'s initWithContentsOfFile:sourceFile
   set aRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:newWidth pixelsHigh:newHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
   set newSize to {width:newWidth, height:newHeight}
   aRep's setSize:newSize
   
   current application's NSGraphicsContext's saveGraphicsState()
   set theContext to current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep
   current application's NSGraphicsContext's setCurrentContext:theContext
   theContext's setShouldAntialias:false -- change to suit
   theContext's setImageInterpolation:(current application's NSImageInterpolationDefault) -- change to suit
   sourceImage's drawInRect:(current application's NSMakeRect(0, 0, newWidth, newHeight)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:(1.0)
   current application's NSGraphicsContext's restoreGraphicsState()
   
   set theRep to aRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5, NSImageProgressive:false}
   theRep's writeToFile:targetFile atomically:true
end main

on getImageSize(theFile)
   set theImage to current application's NSBitmapImageRep's imageRepWithContentsOfFile:theFile
   set pixelWidth to theImage's pixelsWide()
   set pixelHeight to theImage's pixelsHigh()
   return {pixelWidth, pixelHeight}
end getImageSize

main()

The CoreImage script is:

Applescript:

use framework "Foundation"
use framework "CoreImage"
use scripting additions

on main()
   set sourceFile to "/Users/Robert/Working/Test Photo.jpg"
   set targetFile to POSIX path of ((path to desktop as text) & "Reduced CoreImage.jpg")
   set targetWidth to 1000
   
   set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
   set sourceImage to current application's CIImage's imageWithContentsOfURL:sourceFile options:(missing value)
   
   set {sourceWidth, sourceHeight} to {item 1 of item 2 of sourceImage's extent(), item 2 of item 2 of sourceImage's extent()}
   set imageScale to targetWidth / sourceWidth
   
   set resizeFilter to current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputImage:sourceImage, inputScale:imageScale, inputAspectRatio:1.0}
   set outputImage to resizeFilter's outputImage()
   set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:outputImage
   
   set theRep to imageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5, NSImageProgressive:false}
   theRep's writeToFile:targetFile atomically:true
end main

main()

The sips script is:

Applescript:

set sourceFile to "/Users/Robert/Working/Test Photo.jpg"
set targetFile to POSIX path of ((path to desktop as text) & "Reduced Sips.jpg")

do shell script "sips -Z 1000 " & quoted form of sourceFile & " --out " & quoted form of targetFile

Fredrik71's script can be found at:

https://macscripter.net/viewtopic.php?id=48708

Last edited by peavine (2021-10-26 07:39:29 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2021-10-16 05:33:28 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6730

Re: Resizing an image

peavine wrote:

I was surprised that the image created with the AppKit script was degraded as compared with the original with a somewhat dark cast and a lack of definition.



Play with the two lines with the comment "-- change to suit". The defaults I used are probably more suited to non-photographic content (text, etc).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#12 2021-10-16 06:24:25 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

Thanks Shane. I changed NSImageInterpolationNone to NSImageInterpolationDefault, and that fixed the issue. I don't know why I didn't try that before. The antialias setting made no difference.

BTW, the Core Image script often does not retain the photo orientation, and the AppKit script does. CoreImage has some methods that deal with the setting of orientation, and I assume something needs to be done with them.

Last edited by peavine (2021-10-16 07:12:07 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#13 2021-10-17 02:37:12 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Resizing an image

peavine wrote:

BTW, the Core Image script often does not retain the photo orientation, and the AppKit script does. CoreImage has some methods that deal with the setting of orientation, and I assume something needs to be done with them.


I believe the reason is:

AppKit and CoreImage do not use the same coordinate system or maybe that only apply to AppKit vs UIKit (iOS). My own experience I have never had orientation issue in CoreImage but most of my
outputs is always TIFF.

Reference:https://developer.apple.com/documentati … rientation
Returns a new image created by transforming the original image to the specified EXIF orientation.

You could use the code in my last example with the method: imageByApplyingOrientation

Or

Applescript:

inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationUp)

Last edited by Fredrik71 (2021-10-17 05:20:33 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#14 2021-10-17 03:02:31 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Resizing an image

Here is a example to use the method: imageByApplyingTransform and imageTransformForOrientation

Applescript:

use framework "Foundation"
use framework "AppKit" -- NSBitmapImageRep
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 affineTransform to its imageTransform:sourceCIImage forOrientation:"up"
set theCIImage to sourceCIImage's imageByApplyingTransform:affineTransform
saveTIFFFromCIImage(theCIImage, outputImage)

(**
* [imageTransform:inputCIImage forOrientation:imageOrientation]
**
* Returns the transformation needed to reorient the image to the specified orientation.
*)

on imageTransform:inputCIImage forOrientation:imageOrientation
   if imageOrientation = "up" then
       set inputImage to inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationUp)
   end if
   return inputImage
end imageTransform:forOrientation:

(**
* [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

Last edited by Fredrik71 (2021-10-17 10:25:59 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#15 2021-10-17 10:42:49 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Resizing an image

peavine wrote:

* Your script contains kCGImagePropertyOrientationDownUp--is this a typo?


Yes, kCGImagePropertyOrientationUp is the correct for orientation up

You could use on CIImage:

Applescript:

set inputImage to inputCIImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationUp)

This will apply the coordinate orientation up for the CIImage before you use CIFilter...

To not confuse you but here I include up, down, left and right affine transformation matrix

Applescript:

on imageTransform:inputCIImage forOrientation:imageOrientation
   if imageOrientation = "up" then
       set inputImage to inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationUp)
       log inputImage -- a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0
   else if imageOrientation = "down" then
       set inputImage to inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationDown)
       log inputImage -- a:-1.0, b:0.0, c:0.0, d:-1.0, tx:720.0, ty:1280.0
   else if imageOrientation = "left" then
       set inputImage to inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationLeft)
       log inputImage -- a:0.0, b:1.0, c:-1.0, d:0.0, tx:960.0, ty:0.0
   else if imageOrientation = "right" then
       set inputImage to inputCIImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationRight)
       log inputImage -- a:0.0, b:-1.0, c:1.0, d:0.0, tx:0.0, ty:720.0
   end if
   return inputImage
end imageTransform:forOrientation:

Last edited by Fredrik71 (2021-10-17 10:48:51 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#16 2021-10-17 11:58:47 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

Fredrik71. Sorry for any confusion with my earlier post, which I deleted.

My test image is a photo taken in landscape mode with my phone. When viewed in Preview, the orientation is shown as "3 (rotated 180 degrees)".

I've modified my script to implement your suggestion, and it works great when applied to my test image. It rotates the image 180 degrees and then resizes it.

Obviously, not every image needs to be rotated 180 degrees. So, it would appear all I need to do is get the orientation of the source image and to then apply the necessary  orientation change to the resized image using a conditional as you suggest. I don't know how to get the orientation of the source image, but I'm sure some research will yield a suitable answer. Other than that, the changes to my script are relatively simple. Thanks for the help.

Applescript:

use framework "Foundation"
use framework "CoreImage"
use scripting additions

on main()
   set sourceFile to POSIX path of (choose file)
   set targetFile to POSIX path of ((path to desktop as text) & "Reduced CoreImage.jpg")
   set targetWidth to 1000
   
   set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
   set sourceImage to current application's CIImage's imageWithContentsOfURL:sourceFile options:(missing value)
   
   set {sourceWidth, sourceHeight} to {item 1 of item 2 of sourceImage's extent(), item 2 of item 2 of sourceImage's extent()}
   set imageScale to targetWidth / sourceWidth
   
   set inputImage to sourceImage's imageTransformForOrientation:(current application's kCGImagePropertyOrientationDown)
   set sourceImage to sourceImage's imageByApplyingTransform:inputImage
   
   set resizeFilter to current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputImage:sourceImage, inputScale:imageScale, inputAspectRatio:1.0}
   set outputImage to resizeFilter's outputImage()
   set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:outputImage
   
   set theRep to imageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5, NSImageProgressive:false}
   theRep's writeToFile:targetFile atomically:true
end main

main()

Last edited by peavine (2021-10-17 12:01:51 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#17 2021-10-17 12:26:50 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Resizing an image

After more reading to understand the coordinate system Apple use follow reading:

https://developer.apple.com/library/arc … ystem.html

You could see from the URL above.

iOS use: origin at upper left
macOS use: origin at lower left

Both the AppKit and UIKit frameworks provide methods for converting points and rectangles between the coordinate systems of a view and another view, a view and its window, and (on OS X) the screen and the window. An application locates mouse, tablet, gesture, and multi-touch events in the coordinate system of a window, but views can easily convert these to their local coordinate systems.

You can also map points from one coordinate space to another using a two-dimensional mathematical array known as a transform. Using transforms, you can easily scale, rotate, and translate content in two-dimensional space.

https://developer.apple.com/library/arc … forms.html

Maybe this was the approach Shane did... (text from the link above)
You can change the size of an image at any time using the setSize: method of either NSImage or NSImageRep. The size returned by the NSImage version of the method represents the dimensions of the image in the user coordinate space. Changing this value therefore changes the size of the image as it is drawn in one of your views. This change typically affects only the cached copy of the image data, if one exists. Changing the size of an image representation object changes the actual number of bits used to hold the image data. This change primarily affects bitmap images, and can result in a loss of data for your in-memory copy of the image.


if you are the expert, who will you call if its not your imagination.

Offline

 

#18 2021-10-17 07:17:28 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Resizing an image

This is my final Core Image script. It's written for resizing JPG images but could be edited for use with other image types.

Applescript:

use framework "Foundation"
use framework "CoreImage"
use scripting additions

set sourceFile to POSIX path of (choose file)
set targetFile to POSIX path of ((path to desktop as text) & "Resized Image.jpg")
set targetWidth to 1000

resizeImage(sourceFile, targetFile, targetWidth)

on resizeImage(sourceFile, targetFile, targetWidth)
   set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
   set sourceImage to current application's CIImage's imageWithContentsOfURL:sourceFile options:(missing value)
   
   set sourceProperties to (sourceImage's valueForKey:"properties") as record
   set sourceWidth to PixelWidth of sourceProperties
   try
       set sourceOrientation to Orientation of sourceProperties
   on error
       set sourceOrientation to 1
   end try
   
   if sourceOrientation = 3 then
       set sourceImage to sourceImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationDown)
   else if sourceOrientation = 6 then
       set sourceImage to sourceImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationRight)
   else if sourceOrientation = 8 then
       set sourceImage to sourceImage's imageByApplyingOrientation:(current application's kCGImagePropertyOrientationLeft)
   end if
   
   set imageScale to targetWidth / sourceWidth
   set targetImage to current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputImage:sourceImage, inputScale:imageScale, inputAspectRatio:1.0}
   set targetImage to targetImage's outputImage()
   
   set imageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:targetImage
   set imageRep to imageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5, NSImageProgressive:false}
   imageRep's writeToFile:targetFile atomically:true
end resizeImage

Last edited by peavine (2021-10-18 09:39:27 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#19 2021-10-18 09:25:43 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Resizing an image

I believe EXIF metadata store the orientation if a picture is portrait or landscape mode.

Here is some info:https://eorvain-app.medium.com/image-or … af8321820b


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)