Re: applescriptobjc-dev Digest, Vol 64, Issue 3
Re: applescriptobjc-dev Digest, Vol 64, Issue 3
- Subject: Re: applescriptobjc-dev Digest, Vol 64, Issue 3
- From: James Yanchak <email@hidden>
- Date: Tue, 15 Sep 2015 16:56:49 -0400
Thank you Shane, a perfect fix! The best part is it gives me some new stuff to study.
>> 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.
>
_______________________________________________
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