I've got an issue where I'm unable to get the pixel data (via CGDataProviderCopyData
) from the Image that gets returned when I call CGImageCreate
. Any other call and I'm able to get the data from images with no problem, so obviously I'm doing something incorrectly with the parameters that I pass in.
To make sure that I have "good" pixel data, I pull it from an image returned from CGWindowListCreateImage
. Further, to verify that this pixel data is not the point of failure, I have tests which convert it to a PIL Image and save it to file so that I can verify that this are as they should be.
Getting the pixel data:
display_monitor = get_rightmost_monitor()
image = CG.CGWindowListCreateImage(
CG.CGRectInfinite,
CG.kCGWindowListOptionOnScreenOnly,
display_monitor,
CG.kCGWindowImageDefault
)
new_image = CG.CGBitmapContextCreateImage(context)
prov = CG.CGImageGetDataProvider(new_image)
data = CG.CGDataProviderCopyData(prov)
return data
For testing purposes, I'm simply trying to recreate the image object from which the pixel data came. So, I feed all of the attributes of the first image into the parameters of the CGImageCreate
function.
rebuilt_image = CG.CGImageCreate(
CG.CGImageGetWidth(image), # width
CG.CGImageGetHeight(image), # height
CG.CGImageGetBitsPerComponent(image), # bitsPerComponent
CG.CGImageGetBitsPerPixel(image), # bitsPerPixel
CG.CGImageGetBytesPerRow(image), # bytesPerRow
CGImageGetColorSpace(image), # colorspace
CG.CGImageGetBitmapInfo(image), # bitmapInfo
data, # pixel data from image
None, # decode
False, # shouldInterpolate
CG.kCGRenderingIntentDefault)
Now, the Image gets created without throwing any errors. However, upon calling CGDataProviderCopyData
to extract the pixeldata, everything crashes and spits out this error:
<Error>: copy_read_only: vm_copy failed: status 1
Despite my best googling, I'm unable to figure out what's causing this to fail. There are similar quesitons on SO, like this one. However, they're not quite in line with the issue I'm having, or perhaps due to my inexperience with the Core Graphics framework, I'm unable to translate the solutions offered in those questions to something applicable to my issue.
Could someone clear up where I've gone wrong?
Thanks!