473,545 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using Mac OS X CoreGraphics via ctypes

I'm trying to implement a routine that converts a PDF document to
image files using ctypes and the Apple CoreGraphics library as per the
'Splitting a PDF File' example on Apple's web site [0]. Unfortunately
I cannot use the CoreGraphics module used in that example because I'm
using Python 2.5 (AFAIK that module is only available in the system
default Python 2.3.5). There are three questions in the code snippet
below. Each problem area has been commented out in the example so it
runs through to the end. The code is obviously not complete, but it's
enough do demonstrate my problems so far.

BTW, this is the only way I have found to convert a PDF (generated by
ReportLab) to an image that can be fed into a PyQt (v3) QPrinter
object. If anyone knows another way to gain access to the system print
services on Mac OS X using Python please do tell. Oh yes, I'd rather
not include PyObjC because I'm already packaging PyQt, and that would
make the resulting app a lot bigger.

# BEGIN CODE
from ctypes import cdll, c_void_p
from ctypes.util import find_library

cglib = cdll.LoadLibrar y(find_library( 'ApplicationSer vices'))

# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )
#cs = cglib.CGColorSp aceCreateWithNa me(GCS_RGB)

# the next line causes the following error:
#CGPDFDocumentR ef = cglib.CGPDFDocu mentRef
# AttributeError: dlsym(0x1018c0, CGPDFDocumentRe f): symbol not found

CGPDFDocumentCr eateWithProvide r =
cglib.CGPDFDocu mentCreateWithP rovider
CGPDFDocumentCr eateWithProvide r.restype = c_void_p #CGPDFDocumentR ef

provider = cglib.CGDataPro viderCreateWith Filename("sampl e.pdf")
pdf = CGPDFDocumentCr eateWithProvide r(provider)

#for page in xrange(1, pdf.getNumberOf Pages() + 1):
# print page
# pdf.getNumberOf Pages caues the following error:
# AttributeError: 'int' object has no attribute 'getNumberOfPag es'
# I presume the 'pdf' object is a pointer. How do I get a real
# CGPDFDocuemnt instance that has a getNumberOfPage s method?

cglib.CGPDFDocu mentRelease(pdf )
# END CODE

[0] Splitting a PDF File <http://developer.apple.com/graphicsimaging/
pythonandquartz .html>

Thanks in advance for any hints you can provide.

~ Daniel

Jun 15 '07 #1
5 5849
Daniel schrieb:
I'm trying to implement a routine that converts a PDF document to
image files using ctypes and the Apple CoreGraphics library as per the
'Splitting a PDF File' example on Apple's web site [0]. Unfortunately
I cannot use the CoreGraphics module used in that example because I'm
using Python 2.5 (AFAIK that module is only available in the system
default Python 2.3.5). There are three questions in the code snippet
below. Each problem area has been commented out in the example so it
runs through to the end. The code is obviously not complete, but it's
enough do demonstrate my problems so far.

BTW, this is the only way I have found to convert a PDF (generated by
ReportLab) to an image that can be fed into a PyQt (v3) QPrinter
object. If anyone knows another way to gain access to the system print
services on Mac OS X using Python please do tell. Oh yes, I'd rather
not include PyObjC because I'm already packaging PyQt, and that would
make the resulting app a lot bigger.
# BEGIN CODE
from ctypes import cdll, c_void_p
from ctypes.util import find_library

cglib = cdll.LoadLibrar y(find_library( 'ApplicationSer vices'))

# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )
Usually, things in the OSX lib that start with k* are a constant - not a
function. As is this.
Diez
Jun 15 '07 #2
# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )

Usually, things in the OSX lib that start with k* are a constant - not a
function. As is this.

Diez
That's what I thought too. But when I try passing it directly as if it
were a constant:

GCS_RGB = cglib.kCGColorS paceGenericRGB
cs = cglib.CGColorSp aceCreateWithNa me(GCS_RGB)

I get a segfault too. ctypes said kCGColorSpaceGe nericRGB was a
function pointer, so I thought maybe I needed to call it to get the
value (not very good reasoning, I know).

~ Daniel

Jun 15 '07 #3
Daniel wrote:
# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )

Usually, things in the OSX lib that start with k* are a constant - not a
function. As is this.

Diez

That's what I thought too. But when I try passing it directly as if it
were a constant:

GCS_RGB = cglib.kCGColorS paceGenericRGB
cs = cglib.CGColorSp aceCreateWithNa me(GCS_RGB)

I get a segfault too. ctypes said kCGColorSpaceGe nericRGB was a
function pointer, so I thought maybe I needed to call it to get the
value (not very good reasoning, I know).
I'm not sure what that constant exported is - but how about looking the
constant up in the ref docs and pass it as value? Usually these thingies
are some 4-byte-string, or a normal zero-terminated one.

Diez
Jun 18 '07 #4
On Jun 18, 6:07 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
Daniel wrote:
# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )
Usually, things in the OSX lib that start with k* are a constant - not a
function. As is this.
Diez
That's what I thought too. But when I try passing it directly as if it
were a constant:
GCS_RGB = cglib.kCGColorS paceGenericRGB
cs = cglib.CGColorSp aceCreateWithNa me(GCS_RGB)
I get a segfault too. ctypes said kCGColorSpaceGe nericRGB was a
function pointer, so I thought maybe I needed to call it to get the
value (not very good reasoning, I know).

I'm not sure what that constant exported is - but how about looking the
constant up in the ref docs and pass it as value? Usually these thingies
are some 4-byte-string, or a normal zero-terminated one.
Thanks Diez. I'll try that if I decide to keep going with ctypes. I
got a bit further but had some problems with memory management (i.e.
retaining and releasing object references). It seemed like Python/
ctypes was accessing referenced objects after I had released them,
which caused segfaults. I may resort to writing an extension in C that
will handle the entire print process for me rather than fiddling
around with something that gets me half way there. For anyone
interested, here is the code I ended up with:
from ctypes import cdll, cast, c_void_p
from ctypes.util import find_library
cglib = cdll.LoadLibrar y(find_library( "ApplicationSer vices"))

CFStringRef = c_void_p

CGColorSpaceCre ateWithName = cglib.CGColorSp aceCreateWithNa me
CGColorSpaceCre ateWithName.res type = c_void_p
CGColorSpaceCre ateWithName.arg types = [CFStringRef]

CGPDFDocumentCr eateWithProvide r =
cglib.CGPDFDocu mentCreateWithP rovider
CGPDFDocumentCr eateWithProvide r.restype = c_void_p

provider = cglib.CGDataPro viderCreateWith Filename("sampl e.pdf")
if provider:
BMP_INFO = cglib.kCGBitmap ByteOrderDefaul t
CS_RGB = cast(cglib.kCGC olorSpaceGeneri cRGB, CFStringRef)

#cs = CGColorSpaceCre ateWithName(CS_ RGB) # segfault
pdf = CGPDFDocumentCr eateWithProvide r(provider)
npages = cglib.CGPDFDocu mentGetNumberOf Pages(pdf)

for pnum in xrange(1, npages + 1):
print "processing page", pnum
page = cglib.CGPDFDocu mentGetPage(pdf , pnum)
rect = cglib.CGPDFPage GetBoxRect(page , 0) # kCGPDFMediaBox = 0
page_w = cglib.CGRectGet Width(rect)
page_h = cglib.CGRectGet Height(rect)

# incorrect constructor for bitmap
#bitmap = cglib.CGBitmapC ontextCreate(No ne, page_w, page_h,
cs, BMP_INFO)
#cglib.CGContex tDrawPDFDocumen t(bitmap, rect, pdf, pnum)

#cglib.CGPDFDoc umentRelease(pd f) # segfault
#cglib.CGColorS paceRelease(cs)
cglib.CGDataPro viderRelease(pr ovider)

~ Daniel

Jun 18 '07 #5
Daniel wrote:
Thanks Diez. I'll try that if I decide to keep going with ctypes. I
got a bit further but had some problems with memory management (i.e.
retaining and releasing object references). It seemed like Python/
ctypes was accessing referenced objects after I had released them,
which caused segfaults.
I would be unlikely that this is the case. Much more likely is that you've
got some memory allocated by Python which is being passed on to a C library
and is subsequently freed by Python before the C library is finished with
it. Happens all the time. Just make sure you keep a reference in Python
land to any objects you've handed over to C for the lifetime that the C
library is likely to use it.

Having said that, it doesn't look like that's what's happening with your
CGPDFDocumentRe lease call, though perhaps you needed to clean up the page
or rect that you obtained in the loop?

"k" constants defined as strings are 32-bit numbers derived from 4 character
strings. You'll need to convert those strings to the correct number. Once
you've done that I believe your CGColorSpaceCre ateWithName call will work.
Richard

Jun 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3261
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden, Thomas Heller, and Mark Hammond for your earlier help with this problem. I am uncertain about what etiquette calls for, but more on that later. My Objective: I am trying to control the _VMWare...
1
4716
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. This is Python 2.5 on Linux. Here's what I have so far--if you comment out the memmove call (3 lines) it works as expected: # START...
7
7006
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by case basis"? \\\ For example, suppose sometimes I receive the value '\x03hi' + \x04bye' for the struct:
0
3176
by: malen | last post by:
Hi all! I'm building mouse movement filter program for Windows and Mac OS X. In Windows I use ctypes.windll.user32.getCursorPos(pointer) and ctypes.windll.user32.setCursorPos(x,y) to get and set the cursor position but I do not know what the library and functions are called in Mac OS X (I'm a complete noob when it comes to Mac :) ) To...
10
1910
by: David C. Ullrich | last post by:
Running OS X 10.4 "Tiger". Various references by Apple and others say that there exists a module that gives Quartz bindings, allowing all sort of graphics things in Python. Sure enough, after installing Xcode I have some sample scripts. They all start with from CoreGraphics import *
6
12744
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how I should declare the return type of the functions and read the fields. Any hint is appreciated. typedef struct { char *country_short; char...
1
2437
by: moreati | last post by:
Recently I discovered the re module doesn't support POSIX character classes: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for more information. None So I thought I'd try out pcre through ctypes, to recreate pcredemo.c in python. The c code is at:
3
11636
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks, -a
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.