• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSImage Resize
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSImage Resize


  • Subject: Re: NSImage Resize
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 15 Sep 2015 09:56:48 +1000

On 15 Sep 2015, at 3:16 AM, James Yanchak <email@hidden> wrote:

Everything works: reading the file, changing the file (it actually gets new dimensions), but the output file never comes out at those dimensions.

Unfortunately you can't just scale an image like that. Basically, you have to create a new image of the size you want, and draw from the old to the new.

Here are two alternatives:

on aocConvertImageFileAtPath:{locImageFilePath, exportFileFormat, imageWidthPixels}
set locImageOriginal to current application's NSString's stringWithString:locImageFilePath
set locImageNew to locImageOriginal's stringByDeletingPathExtension()'s stringByAppendingPathExtension:exportFileFormat
# make NSImage from file
set thisImage to current application's NSImage's alloc()'s initWithContentsOfFile:locImageOriginal
set imageMeasures to (thisImage's |size|())
set scalePercentage to (imageWidthPixels / (imageMeasures's width))
set newWidth to (imageMeasures's width) * scalePercentage
set newHeight to imageMeasures's height
set newImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(newWidth, newHeight)) -- make new blank image
-- draw from original to new
newImage's lockFocus()
thisImage's drawInRect:{origin:{x:0, y:0}, |size|:{width:newHeight, height:newHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0
newImage's unlockFocus()
# get TIFF version as data
set thisImageDataAsTIFF to newImage's TIFFRepresentation()
# make bitmap representation from TIFF data
set thisImageDataAsBITMAP to current application's NSBitmapImageRep's imageRepWithData:thisImageDataAsTIFF
# extract jpeg
set theData to thisImageDataAsBITMAP's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5}
# save to file
theData's writeToFile:locImageNew atomically:true
end aocConvertImageFileAtPath:

This version uses NSBitmapImageReps directly:

on aocConvertImageFileAtPath:{locImageFilePath, exportFileFormat, imageWidthPixels}
set locImageOriginal to current application's NSString's stringWithString:locImageFilePath
set locImageNew to locImageOriginal's stringByDeletingPathExtension()'s stringByAppendingPathExtension:exportFileFormat
# make bitmap from file
set oldRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:locImageOriginal
set imageMeasures to (oldRep's |size|())
set scalePercentage to (imageWidthPixels / (imageMeasures's width))
set newWidth to (imageMeasures's width) * scalePercentage
set newHeight to imageMeasures's height
-- make new bitmap
set newRep 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) bitmapFormat:(current application's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
-- store the existing graphics context
current application's NSGraphicsContext's saveGraphicsState()
-- set graphics context to new context based on the new bitmapImageRep
(current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:newRep))
oldRep's drawInRect:{origin:{x:0, y:0}, |size|:{width:newHeight, height:newHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
-- restore graphics state
current application's NSGraphicsContext's restoreGraphicsState()
# extract jpeg
set theData to newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.5}
# save to file
theData's writeToFile:locImageNew atomically:true
end aocConvertImageFileAtPath:

The second method can be a bit more flexible if you want to do things like set a background.


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSImage Resize (From: James Yanchak <email@hidden>)

  • Prev by Date: NSImage Resize
  • Next by Date: Re: applescriptobjc-dev Digest, Vol 64, Issue 3
  • Previous by thread: NSImage Resize
  • Next by thread: Re: applescriptobjc-dev Digest, Vol 64, Issue 3
  • Index(es):
    • Date
    • Thread