[Pythonmac-SIG] Python Core Graphics Question
geert at nznl.com
Wed May 27 12:14:26 CEST 2009
Below is a piece of code that I found over in the quartz-dev list.
I've been using this function unchanged in a batch processor, and it
appears to leak memory quite substantially. So much so that my test
machine (1Gb memory) , a mac mini, and my production xserve (2 Gb
memory) both crashed using it. My dev machine, an imac (4Gb memory)
slowed, but stayed up.
I think I have the problem down to "croppedimg =
srcimg.createWithImageInRect(cliprect)". If used with an image of
suffient size, it quickly sucks up all available memory.
Has anyone experienced similar problems using this method? Its Obj-C
name is "CGContextCreateWithImageInRect". And I should think that one
would only see something bad happening in some sort of batch processing.
Incidentally, my workaround was to lower the size of the source image.
But the process is still quite unstable. It brought down my xserve
after processing 7000 of the 7600 odd items.
I've pasted the original post below. And sorry for any cross - posting.
Geert Dekkers Web Studio | 2e Keucheniusstraat 8HS 1051VR Amsterdam |
+31(0)627224301 | http://nznl.net
>> Thanks for you detailed reply Glen - much appreciated.
>> You helped me to solve what i was trying to do by using
>> CGImageCreateWithImageinRect as suggested.
>> For anyone else interested here is some simple python code for
>> making cropped thumbnails of any proportion and not loosing the
>> aspect ratio of the original image.
>> ...and its damn fast!
>> ----------- #!/usr/bin/python
>> from __future__ import division import CoreGraphics
>> def resizeimage(srcimage,targetimage, tw, th):
>> srcimg =
>> sw = srcimg.getWidth()
>> sh = srcimg.getHeight()
>> aspect = tw/th
>> widthfactor = tw/sw
>> heightfactor = th/sh
>> if widthfactor < heightfactor:
>> #src height stays the same
>> #src width gets cropped
>> cropwidth = sh * aspect
>> x1 = ((sw-cropwidth)/2)
>> y1 = 0
>> x2 = sw-((sw-cropwidth))
>> y2 = sh
>> #src height gets cropped
>> #src width stays the same
>> cropheight = sw / aspect
>> x1 = 0
>> y1 = ((sh-cropheight)/2)
>> x2 = sw
>> y2 = sh-((sh-cropheight))
>> cliprect = CoreGraphics.CGRectMake(x1, y1, x2, y2)
>> croppedimg = srcimg.createWithImageInRect(cliprect)
>> cs = CoreGraphics.CGColorSpaceCreateDeviceRGB()
>> c = CoreGraphics.CGBitmapContextCreateWithColor(tw, th, cs,
>> newRect = CoreGraphics.CGRectMake(0, 0, tw, th)
>> c.drawImage(newRect, croppedimg)
>> c.writeToFile(targetimage, CoreGraphics.kCGImageFormatJPEG)
>> resizeimage("/users/adam/Desktop/bootlogo4pa8.jpg", "/users/adam/
>> Desktop/aaaaa.jpg" , 80,80)
>>> On Aug 12, 2008, at 3:41 AM, Glenn Cole wrote:
>>> Hi, Adam --
>>> I'm no expert, but until others more knowledgeable respond, here's
>>> my understanding.
>>> First, I suspect the fact that you're using Python won't matter at
>>> all here. (It does in other circumstances, but I don't think so
>>> It sounds like you're looking for more support from Quartz for
>>> your task than what's really there. In the end, I think it's going
>>> to be more a choice of algorithm on your part.
>>> For example, given different aspect ratios, the new image could
>>> 1. ignore the difference (i.e., the current behaviour)
>>> 2. show the entire original image within the new target,
>>> respecting the aspect ratio
>>> 3. clip the original image to match the new aspect ratio
>>> For #2, you could shrink the target rect to match the aspect ratio
>>> of the original image.
>>> For #3 (your goal), you could clip the original image to match the
>>> target aspect ratio. However, page 250 of the outstanding
>>> Programming With Quartz notes a few downsides to this, and
>>> suggests using CGImageCreateWithImageInRect instead (first
>>> available in Tiger) to define a "subimage" of the original image.
>>> Again, though, you would need to determine yourself that the
>>> subimage should ignore 100 pixels from the left and right sides of
>>> the original image.
>>> At least, that's my take on things. We'll see what others say
>>> (though they've tended to ignore Python questions in the past).
>>> Incidentally, the subject line says "Core Image resize question."
>>> The code below has no reference to Core Image, and I'm pretty sure
>>> that Core Image need not be involved in this task at all. (Perhaps
>>> you mentioned Core Image because of the existence of its CICrop
>>> filter, but I think that's not the best direction for the task at
>>> On Aug 11, 2008, at 5:33 AM, Adam Jones wrote:
>>> Hi there i am wondering if someone can help me with this. Below is
>>> the code i am using to resize and save a jpeg image.
>>> It works mint as but it obviously scales the image with no respect
>>> for the aspect ratio.
>>> i want to be able to take an image and scale it proportionaly to a
>>> square thumbnail. so for example a 800x600 image scaled to 80x80
>>> would need to loose 100pixels off the left and 100 pixels on the
>>> right before scaling.
>>> Does any one know how to crop using python quartz or can i draw
>>> the rec with the image off the canvas to achieve the same?
>>> Thanks Adam
>>> ----------- #!/usr/bin/python
>>> import CoreGraphics
>>> def resizeimage(srcimage,targetimage, w, h):
>>> origImage = CoreGraphics .CGImageImport
>>> origwidth = origImage.getWidth() # not used yet but will be once i
>>> work out how to crop
>>> origheight = origImage.getHeight() # not used yet but will be once
>>> i work out how to crop
>>> cs = CoreGraphics.CGColorSpaceCreateDeviceRGB()
>>> c = CoreGraphics.CGBitmapContextCreateWithColor(w, h, cs, (0,0,0,0))
>>> newRect = CoreGraphics.CGRectMake(0, 0, w, h)
>>> c.drawImage(newRect, origImage)
>>> c.writeToFile(targetimage, CoreGraphics.kCGImageFormatJPEG)
>>> resizeimage(uploadedfile, "/svr/data/images/listings/%s/
>>> %s_160x90.jpg" % (str(listing_id), newimagename), 160,90)
>>> _______________________________________________ Do not post admin
>>> requests to the list. They will be ignored. Quartz-dev mailing
>>> list (email at hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/quartz-dev/email@hidden
>>> This email sent to email at hidden
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Pythonmac-SIG