Re: [Image-SIG] Python Core Graphics Question (resent)
On Tue, Jun 2, 2009 at 9:13 PM, Geert Dekkers wrote: > (I just resent this message as I couldn't it find it on the digest even > after days - again, I apologise for any cross-posting) It's in the image-sig archive, at least: http://mail.python.org/pipermail/image-sig/2009-May/005709.html ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig
[Image-SIG] Python Core Graphics Question (resent)
Hi all (I just resent this message as I couldn't it find it on the digest even after days - again, I apologise for any cross-posting) 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. Cheers, Geert -- 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 = CoreGraphics .CGImageImport (CoreGraphics.CGDataProviderCreateWithFilename(srcimage)) 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 else: #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, (0,0,0,0)) c.setInterpolationQuality(CoreGraphics.kCGInterpolationLow) 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/a.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 here.) 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 either: 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 hand.) --Glenn 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
[Image-SIG] Python Core Graphics Question
Hi all 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. Cheers, Geert -- 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 = CoreGraphics .CGImageImport (CoreGraphics.CGDataProviderCreateWithFilename(srcimage)) 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 else: #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, (0,0,0,0)) c.setInterpolationQuality(CoreGraphics.kCGInterpolationLow) 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/a.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 here.) 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 either: 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 hand.) --Glenn 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 t