2016-08-18 109 views
0

我無法在python中創建有效的CGBitmapContext。它只是返回null值,然後導致其他所有內容抱怨缺少定義和python崩潰。我嘗試將內存分配設置爲None,這意味着它應該自行排除,但這也不起作用。我不認爲objC緩衝區也被分配。任何幫助將不勝感激。我的CGBitmapContext出了什麼問題?

#!/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 
+1

「CGBitmapContextCreate」的'bytesPerRow'參數看起來不對。如果您的位圖的像素寬度爲x像素,並且它是每個組件8位的RGBA上下文,則每個像素需要4個字節,因此bytesPerRow應該是「x * 4」。 (請注意,當你分配內存時,你的數學運算是正確的。) –

+0

是的,我只是讓0讓Mac爲自己工作,而腳本似乎在其他地方失敗了。所以,進展。 – benwiggy

回答

0

輸出建議我打開一些額外的日誌記錄:

os.environ["CG_CONTEXT_SHOW_BACKTRACE"] = '1' 
os.environ["CGBITMAP_CONTEXT_LOG_ERRORS"] = '1' 

這就給了我下面的信息,這些信息看似來自蘋果自己的文檔缺乏:

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作爲迴應。