[Pythonmac-SIG] CoreGraphics, 10.5
Erik van Blokland
erik at letterror.com
Sat Feb 16 13:45:09 CET 2008
Folks,
I've been wrestling with some CoreGraphics stuff, OSX 10.5.2, stock
Python 2.5.1. I want to load a RGB image, then load a grayscale
image, draw one while using the other as a mask. I can build all the
required objects. I can draw with "masking-like things" happening to
the image. But the masking appears to be according to a (random) piece
of memory, rather than the pixels from the mask image.
The RGB image:
http://erik.letterror.com/cg/picture.png
The grayscale mask:
http://erik.letterror.com/cg/mask.tif
The resulting image:
http://erik.letterror.com/cg/results.png
The script:
http://erik.letterror.com/cg/cgMaskTestPost.py
The script which draws this file is below. Yellow background, rgb
image of a neon sign, mask. I get the impression I'm somehow not
making the right kind of mask. Or perhaps the parameters I'm passing
to it are wrong. Perhaps the mask image is wrong. I've tried black/
white bitmaps, tiff, jpeg, png, grayscale 8 bit, 24 bit. Different
formats result in different patterns being used as a mask - so there
is some correlation between the file and the result. Is this anywhere
close to how it needs to be done?
Any pointers are most welcome, thanks!
Erik van Blokland
#! /usr/bin/python2.5
from CoreGraphics import *
import os
def test():
testWidth = 1000
testHeight = 500
fileName = 'picture.png' # name of a 8 bit PNG, RGB image
maskName = 'mask.tif' # name of an 8 bit, grayscale tiff
# the paths
root = os.getcwd()
filePath = os.path.join(root,fileName)
maskPath = os.path.join(root,maskName)
dstPath = os.path.join(root, "results.png")
# data providers for the image and the mask
img = CGImageImport(CGDataProviderCreateWithFilename(filePath))
maskProvider = CGDataProviderCreateWithFilename(maskPath)
imgMask = CGImageImport(maskProvider)
# first, let's find out some things about the mask.
width = imgMask.getWidth()
height = imgMask.getHeight()
bitsPerComponent = imgMask.getBitsPerComponent()
bitsPerPixel = imgMask.getBitsPerPixel()
bytesPerRow = imgMask.getBytesPerRow()
shouldInterpolate = True
decode = [0,255] # not relevant?
# make the mask
maskObject = CGImageMaskCreate(width, height,
bitsPerComponent, bitsPerPixel,
bytesPerRow, maskProvider,
decode, shouldInterpolate)
# now make the image with source image AND mask
imgWithMask = CGImage_createWithMask(img, maskObject)
# make a new image to save it all
ctx = CGBitmapContextCreateWithColor(testWidth, testHeight,
CGColorSpaceCreateDeviceRGB(), (0,0,0,0))
# a background color
ctx.setRGBFillColor(.9, .9, 0, .8)
ctx.addRect(CGRectMake(0, 0, testWidth, testHeight))
ctx.fillPath()
# draw the masked image
ctx.drawImage(CGRectMake(50, 50, width, height), imgWithMask)
CGContext_writeToFile(ctx, dstPath, kCGImageFormatPNG)
print "done"
if __name__ == "__main__":
test()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythonmac-sig/attachments/20080216/6c1871d1/attachment.htm
More information about the Pythonmac-SIG
mailing list