• 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: Resize NSImage without anti-aliasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Resize NSImage without anti-aliasing


  • Subject: Re: Resize NSImage without anti-aliasing
  • From: Takaaki Naganoya <email@hidden>
  • Date: Wed, 22 Mar 2017 10:04:16 +0900

Thank Shane,

But the first line you indicated returns nothing. So, variable “theContext” seems empty or undefined.

set theContext to current application’s NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)

So the code causes error in next line 

theContext’s setShouldAntiAlias:false

<My Whole Test Code>

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "AppKit"


set anImage to current application's NSImage's imageNamed:(current application's NSImageNameComputer)

--set aPath to POSIX path of (choose file of type {"public.image"})
--set aNSImage to current application's NSImage's alloc()'s initWithContentsOfFile:aPath

set newImage to my resizeNSImage:anImage toScale:10.0


set aDesktopPath to (current application's NSProcessInfo's processInfo()'s environment()'s objectForKey:("HOME"))'s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath's stringByAppendingString:((current application's NSUUID's UUID()'s UUIDString())'s stringByAppendingString:".png")
set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me


on resizeNSImage:aSourceImg toScale:imgScale
set aSize to aSourceImg's |size|()


set aWidth to (aSize's width) * imgScale
set aHeight to (aSize's height) * imgScale


set aRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0


set newSize to {width:aWidth, height:aHeight}
aRep's setSize:newSize


current application's NSGraphicsContext's saveGraphicsState()
--current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)


set theContext to current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)
theContext’s setShouldAntiAlias:false - - ** This line cause error ***

theContext's setImageInterpolation:(current application's NSImageInterpolationNone)


aSourceImg's drawInRect:(current application's NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:(1.0)


current application's NSGraphicsContext's restoreGraphicsState()


set newImg to current application's NSImage's alloc()'s initWithSize:newSize
newImg's addRepresentation:aRep


return newImg --Unnecessary?? 
end resizeNSImage:toScale:


on saveNSImageAtPathAsPNG(anImage, outPath)
set imageRep to anImage's TIFFRepresentation()
set aRawimg to current application's NSBitmapImageRep's imageRepWithData:imageRep
set pathString to current application's NSString's stringWithString:outPath
set newPath to pathString's stringByExpandingTildeInPath()
set myNewImageData to (aRawimg's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
set aRes to (myNewImageData's writeToFile:newPath atomically:true) as boolean
return aRes
end saveNSImageAtPathAsPNG

</My Whole Test Code>


2017/03/22 7:59、Shane Stanley <email@hidden>のメール:

On 22 Mar 2017, at 1:34 am, Takaaki Naganoya <email@hidden> wrote:

Is there a way to scale NSImage without anti-aliasing? 

Try changing this:

current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)

set theContext to current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)
theContext's setShouldAntiAlias:false
theContext's setImageInterpolation:(current application's NSImageInterpolationNone)

The last line is probably unnecessary.

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

Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users This email sent to email@hidden
  • Follow-Ups:
    • Re: Resize NSImage without anti-aliasing
      • From: Shane Stanley <email@hidden>
References: 
 >Resize NSImage without anti-aliasing (From: Takaaki Naganoya <email@hidden>)
 >Re: Resize NSImage without anti-aliasing (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Manipulate Contacts with AppleScriptObjC?
  • Next by Date: Re: Resize NSImage without anti-aliasing
  • Previous by thread: Re: Resize NSImage without anti-aliasing
  • Next by thread: Re: Resize NSImage without anti-aliasing
  • Index(es):
    • Date
    • Thread