Add alpha channel

Hi there,

The last few days I’ve been searching and searching for ways to add an alpha channel by scripting.

I need to add an alpha channel to a PNG file in order to let it be the user’s new login background (otherwise it would be garbled).
When the user uploads a file, I know how to convert it to a PNG image, but I don’t know how to let it have an alpha channel. The most easy way is to set the image transparency to 99.9%, but I don’t even know how to do that.
Terminal and ObjectiveC commands are fine too!

If anyone has any idea, please reply, even if you think it’s impossible!

Hi
I’m guessing that you are using Photoshop, if so you could try whats in this link, just tried it
here using CS6, works fine.

http://my.safaribooksonline.com/book/-/9780240808697/part-2-tutorials/tutorial_58_scripting_alpha_ch#X2ludGVybmFsX0J2ZGVwRmxhc2hSZWFkZXI/eG1saWQ9OTc4MDI0MDgwODY5Ny80NTg=

Hi,

Thank you for your reply, but I can’t use it…
My script needs to be able to open any kind of image file (so not only PSD), and then save it as a PNG with an alpha channel.

Nevertheless, it may use Adobe Photoshop.

Hello.

I am totally sure that ImageMagick (A command line tool), can do it for you, but you’ll have to find and install the binaries, and read the documentation.

This snippet will do it in AppleScriptObjC. It probably needs modifying for PDFs, but it should get you started:

set theImage to current application's NSImage's alloc()'s initWithContentsOfFile_(pathArg) -- load the file as an NSImage
set {width:x1, height:y1} to theImage's |size|() -- get size
-- set required dimensions of newX, newY
set newRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(missing value, newX, newY, 8, 4, true, false, current application's NSCalibratedRGBColorSpace, current application's NSAlphaFirstBitmapFormat, 0, 32) -- make new image rep
current application's NSGraphicsContext's saveGraphicsState() -- save graphics context state
current application's NSGraphicsContext's setCurrentContext_(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep_(newRep)) -- set current graphics context to new image rep
theImage's drawInRect_fromRect_operation_fraction_({origin:{x:0, y:0}, |size|:{width:newX, height:newY}}, current application's NSZeroRect, current application's NSCompositeCopy, 1.0) -- draw from old rep into graphics context
current application's NSGraphicsContext's restoreGraphicsState() -- restore graphics state
set theData to newRep's representationUsingType_properties_(current application's NSPNGFileType, {NSImageGamma:1.0, NSImageInterlaced:false}) -- convert image rep to data
theData's writeToFile_atomically_(savePath, true) -- write it to disk

You probably want to change NSCompositeCopy to NSCompositeSourceOver or something similar.

Shane: Incredibly THANK YOU
And MCUrll: Thank you as well, and I would’ve used ti secondly, but Shane’s method is more user friendly