Transparency

iMagine Photo has the functionality to draw imported graphics with an alpha channel (transparency) and create image files with an alpha channel.

Not all image formats have the capability for adding an alpha channel. JPEG is not one of those formats. The image formats that are included with Quicktime that have alpha channel functionality are:

The third party Quicktime component "PhotoJazz" also has alpha channel support. The first example draws an image with an alpha channel over another image. This can be done by setting the graphics mode to straight alpha. The script makes no attempt not to stretch the background image or the image with an alpha channel. It is not all that difficult to scale two images so that the amount they overlap is maximised without stretching, but that is not the point of the exercise here and would distract from the main point. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set thisFile to choose file with prompt "Choose background image file: "
  
set thisFile2 to choose file with prompt "Choose image file with alpha channel: "
  
tell application "iMagine Photo"
    
set thisImporter1 to import graphic thisFile
    
if the component error of thisImporter1 is not equal to 0 then
      
close thisImporter1
      
return
    
end if
    
set thisImporter2 to import graphic thisFile2
    
if the component error of thisImporter2 is not equal to 0 then
      
close thisImporter2
      
return
    
end if
    
set the destination rectangle of thisImporter1 to {0, 0, 1024, 768}
    
set the destination rectangle of thisImporter2 to {0, 0, 1024, 768}
    
set thisDocument to make new window document with properties {dimensions:{1024, 768}}
    
set the drawing destination of thisImporter1 to thisDocument
    
draw thisImporter1
    
close thisImporter1
    
set the drawing destination of thisImporter2 to thisDocument
    
set the graphics mode of thisImporter2 to straight alpha
    
draw thisImporter2
    
close thisImporter2
  
end tell
end
run

In some cases when a digital image is created with an alpha channel, the author of the image might decide that not all users of the image file will have the tools to take advantage of the transparency information. To help these users the image is pre-calculated with a black or white background or even a color background. When drawing these images with transparency it may be necessary to remove this background. For an image created with a black background it is necessary to set the graphics mode to pre black alpha instead of straight alpha as in the script above or for an image created with a white background it is necessary to set the graphics mode to pre white alpha. If the image has been created with a color background then instead of the line in the script above:

    set the graphics mode of thisImporter2 to straight alpha

It should be replaced with:

    set the drawing mode of thisImporter2 to {graphics mode:pre mul color alpha, opcolor:thePreMulColor}

where thePreMulColor is the background color that the image was created with.

To create an image with a alpha channel using iMagine Photo it is necessary to set the alpha channel of a graphic or window document. The alpha channel information is calculated from one of the color channels or an average of each color channel, or the difference between a reference color at the color at each pixel. The inverted alpha channel property allows you to invert the alpha channel.

The second example script draws a series of red ovals each one smaller and lighter than the last. After drawing the red ovals the alpha channel is set to the red channel of the current document. The selected image is then drawn and exported. The exported image has an alpha channel that is transparent at the edges. You can use the script above to combine this new image with alpha channel to another image. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set thisFile to choose file with prompt "Choose image file to add an alpha channel to: "
  
tell application "iMagine Photo"
    
set thisImporter to import graphic thisFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set thisDocument to make new window document with properties {dimensions:{1024, 768}}
    
tell thisDocument to create composition element with properties {class:filled rectangle, bounds rectangle:{0, 0, 1024, 768}, color:{0, 0, 0}}
    
repeat with i from 1 to 32
      
set theAlphaColor to {i * 2047, 0, 0}
      --
set theWidth to 1280 - ((i - 1) * 16)
      
set theWidth to 1152 - (i * 16)
      --
set theHeight to 960 - ((i - 1) * 12)
      
set theHeight to 868 - (i * 12)
      
set theLeft to (1024 - theWidth) div 2
      
set theTop to (768 - theHeight) div 2
      
set ovalBoundsRect to {theLeft, theTop, theLeft + theWidth, theTop + theHeight}
      
tell thisDocument to create composition element with properties {class:filled oval, bounds rectangle:ovalBoundsRect, color:theAlphaColor, drawing type:quartz}
    
end repeat
    
set the alpha channel of thisDocument to {source document:thisDocument, color channel:red channel, inverted:false}
    
set the drawing destination of thisImporter to thisDocument
    
set the destination rectangle of thisImporter to {0, 0, 1024, 768}
    
draw thisImporter
    
close thisImporter
    
set the export file type of thisDocument to "PNG"
    
set defaultName to "MyFileName" & (export file extension of thisDocument)
    
set the export file location of thisDocument to choose file name with prompt "Choose name and location for image with alpha channel: " default name defaultName
    
export thisDocument
    
close thisDocument
  
end tell
end run

The above script is made slightly more complicated than it would otherwise be because I wanted to use ovals to define the shape of the transparency rather than rectangles. Using the blue or green color channel works in the same way, and the average of channel is only different in that the alpha channel is calculated from the average of the color channels. If an alpha channel has been previously set then you can clear it by setting the color channel to clear alpha.

The reference color option for color channel is quite powerful and more difficult to understand. If you specify the color channel as reference color then you also need to supply a reference color against which the colors of all the pixels in the source document are compared. The reference color option works like chroma key. If you are bluescreening then you can set the reference color to your blue screen color. You can also specify how quickly colors become opaque depending on how far (spectrally) the color is from the reference color. The alpha channel property to control this is called slope and it ranges from 0.0 to 256.0. A slope of 0.0 means that every color is fully transparent, not very useful, a slope of 1.0 means that if your reference color is white, then white is fully transparent, black is fully opaque and all the colors in between have varying levels of transparency depending on their level of brightness. A slope of 256.0 means that every pixel is opaque except for the reference color which is fully transparent. The effect of a slope of 256 is no different to drawing a image using the transparent graphics mode with the opcolor set to the reference color. It is also possible to have the alpha channel inverted. The following script allows you to choose an image file to add an alpha channel to, where you want to save the image with the alpha channel, a reference color and a slope. For best results the background image should be the same size as the original. This is quite a lot of computing, so delays will be noticeable especially in comparison to other scripts in this documentation. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set thisFile to choose file with prompt "Choose a file to add alpha channel to: "
  
set newFile to choose file name with prompt "Select a folder and name to save the image with alpha channel to: " default name "AlphaChannelImage.png"
  
set backImageFile to choose file with prompt "Choose a file to be the background image to draw the file with an alpha channel on: "
  
tell application "iMagine Photo"
    
display dialog "The color picker dialog about to be displayed is asking you for the color to be the reference color or Chroma key. "
    
set chromaKeyColor to choose color
    
set theSlope to (text returned of (display dialog "Enter the slope (0.0 to 256.0): " default answer "6.0")) as real
    
set thisImporter to import graphic thisFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set {x, y, xDim, yDim} to the natural bounds of thisImporter
    
set thisDocument to make new window document with properties {dimensions:{xDim, yDim}}
    
set the drawing destination of thisImporter to thisDocument
    
draw thisImporter
    
close thisImporter
    
set the alpha channel of thisDocument to {color channel:reference color, source document:thisDocument, color:chromaKeyColor, slope:theSlope, inverted:false}
    
set the export file type of thisDocument to "PNG"
    
set the export file location of thisDocument to newFile
    
export thisDocument
    
set thisImporter to import graphic backImageFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set the drawing destination of thisImporter to thisDocument
    
set the destination rectangle of thisImporter to {0, 0, xDim, yDim}
    
draw thisImporter
    
close thisImporter
    
set thisImporter to import graphic newFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set the drawing destination of thisImporter to thisDocument
    
set the graphics mode of thisImporter to straight alpha
    
draw thisImporter
    close thisImporter
    
try
      
set newFile to (choose file name "Select a folder and name to save the file as or click cancel to not save: " default name "MyNewFile.jpg")
      
set the export file type of thisDocument to "JPEG"
      
set the export file location of thisDocument to newFile
      
set the export compression quality of thisDocument to high
      
export thisDocument
    
end try
  
end tell
end run

The above script also allows you to save the final script.

The ability to open scripts in a new Script Editor window is provided by an application called "Convert Script to Markup Code" and can be obtained from http://homepage.mac.com/jonn8/as/

keywords: AppleScript, Apple Script, transparency, Macintosh, Mac, Quicktime, alpha channel, chroma key, bluescreening