我在python中创建有效的CGBitmapContext时遇到问题。它只是返回空值,然后导致其他一切抱怨缺乏定义和python崩溃。我已经尝试将内存分配设置为None,这意味着它应该自行排序,但这也不起作用。而且我认为objC缓冲区也没有被分配。任何帮助将不胜感激。
#!/usr/bin/python
import os, sys, objc
from Quartz import *
os.environ["CG_CONTEXT_SHOW_BACKTRACE"] = '1'
resolution = 300 #dpi
scale = resolution/72
cs = CGColorSpaceCreateWithName(kCGColorSpaceSRGB)
# Options might be: kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedLast \ or FIRST
transparency = kCGImageAlphaNoneSkipLast
#Save image to file
def writeImage (image, url, type, options):
destination = CGImageDestinationCreateWithURL(url, type, 1, None)
CGImageDestinationAddImage(destination, image, options)
CGImageDestinationFinalize(destination)
CFRelease(destination)
return
if __name__ == '__main__':
for filename in sys.argv[1:]:
pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(filename))
numPages = CGPDFDocumentGetNumberOfPages(pdf)
shortName = os.path.splitext(filename)[0]
# For each page, create a file
for i in range (1, numPages+1):
page = CGPDFDocumentGetPage(pdf, i)
if page:
#Get mediabox
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
x = CGRectGetWidth(mediaBox)
y = CGRectGetHeight(mediaBox)
x *= scale
y *= scale
# Allocate Memory, in this day and age.
try:
rasterData = objc.allocateBuffer(int(4 * x * y))
except MemoryError: break
# Create a Bitmap Context
ctx = CGBitmapContextCreate(rasterData, x, y, 8, x, cs, transparency)
CGContextSaveGState (ctx)
CGContextScaleCTM(ctx, scale,scale)
CGContextDrawPDFPage(ctx, page)
CGContextRestoreGState(ctx)
# Convert to an "Image"
image = CGBitmapContextCreateImage(ctx)
# Create unique filename per page
outFile = shortName + str(i) + ".tiff"
url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outFile, len(outFile), False)
# kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG
type = 'kUTTypeTIFF'
options = {
kCGImagePropertyTIFFXResolution : 300,
kCGImagePropertyTIFFYResolution : 300
}
writeImage (image, url, type, options)
CGContextRelease(ctx)
del page
答案 0 :(得分:0)
输出表明我打开了一些额外的日志记录:
os.environ["CG_CONTEXT_SHOW_BACKTRACE"] = '1'
os.environ["CGBITMAP_CONTEXT_LOG_ERRORS"] = '1'
这给了我以下信息,这些信息似乎缺乏Apple自己的文档:
CGBitmapContextCreate: unsupported parameter combination:
16 integer bits/component;
48 bits/pixel;
RGB color space model; kCGImageAlphaNone;
14336 bytes/row.
Valid parameters for RGB color space model are:
16 bits per pixel, 5 bits per component, kCGImageAlphaNoneSkipFirst
32 bits per pixel, 8 bits per component, kCGImageAlphaNoneSkipFirst
32 bits per pixel, 8 bits per component, kCGImageAlphaNoneSkipLast
32 bits per pixel, 8 bits per component, kCGImageAlphaPremultipliedFirst
32 bits per pixel, 8 bits per component, kCGImageAlphaPremultipliedLast
64 bits per pixel, 16 bits per component, kCGImageAlphaPremultipliedLast
64 bits per pixel, 16 bits per component, kCGImageAlphaNoneSkipLast
64 bits per pixel, 16 bits per component, kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents
64 bits per pixel, 16 bits per component, kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents
128 bits per pixel, 32 bits per component, kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents
128 bits per pixel, 32 bits per component, kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents
因此,仅将透明度常量更改为kCGImageAlphaPremultipliedLast已停止崩溃并修复了上下文。但是,整个脚本仍然不起作用,我只是将Trace/BPT trap: 5
作为回复。