<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">Hi all</span></font></pre><pre><span class="Apple-style-span" style="font-family: 'Lucida Grande'; font-size: 12px; white-space: normal; ">(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)</span></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">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.</span></font></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">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.</span></font></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">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.</span></font></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">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.</span></font></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">I've pasted the original post below. And sorry for any cross - posting.</span></font></pre><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">Cheers,&nbsp;Geert</span></font></pre><div>----------------------------------------------------------------------------------------------</div><div>Geert Dekkers Web Studio | 2e Keucheniusstraat 8HS 1051VR Amsterdam | +31(0)627224301 |&nbsp;<a href="http://nznl.net/">http://nznl.net</a></div><div>----------------------------------------------------------------------------------------------</div><div><br></div><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><blockquote type="cite">Thanks for you detailed reply Glen - much appreciated.<br>You helped me to solve what i was trying to do by using CGImageCreateWithImageinRect as suggested.<br><br>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.<br>...and its damn fast!<br><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">----------- #!/usr/bin/python</span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">from __future__ import division import CoreGraphics</span></font></pre><br>def resizeimage(srcimage,targetimage, tw, th):<br><br>srcimg = CoreGraphics .CGImageImport(CoreGraphics.CGDataProviderCreateWithFilename(srcimage))<br><br>sw = srcimg.getWidth()<br>sh = srcimg.getHeight()<br><br>aspect = tw/th<br><br>widthfactor = tw/sw        <br>heightfactor = th/sh<br><br>if widthfactor &lt; heightfactor:<br>#src height stays the same<br>#src width gets cropped<br>cropwidth = sh * aspect<br>x1 = ((sw-cropwidth)/2)<br>y1 = 0<br>x2 = sw-((sw-cropwidth))<br>y2 = sh<br>else:<br>#src height gets cropped<br>#src width stays the same<br>cropheight = sw / aspect<br>x1 = 0<br>y1 = ((sh-cropheight)/2)<br>x2 = sw<br>y2 = sh-((sh-cropheight))        <br><br>cliprect = CoreGraphics.CGRectMake(x1, y1, x2, y2)<br>croppedimg = srcimg.createWithImageInRect(cliprect)        <br><br>cs = CoreGraphics.CGColorSpaceCreateDeviceRGB()<br>c = CoreGraphics.CGBitmapContextCreateWithColor(tw, th, cs, (0,0,0,0))<br><br>c.setInterpolationQuality(CoreGraphics.kCGInterpolationLow)<br>newRect = CoreGraphics.CGRectMake(0, 0, tw, th)<br>c.drawImage(newRect, croppedimg)<br>c.writeToFile(targetimage, CoreGraphics.kCGImageFormatJPEG)<br><br>resizeimage("/users/adam/Desktop/bootlogo4pa8.jpg", "/users/adam/ Desktop/aaaaa.jpg" , 80,80)        <br><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br></span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br></span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br></span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><blockquote type="cite"><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">On Aug 12, 2008, at 3:41 AM, Glenn Cole wrote:</span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">Hi, Adam --</span></font></pre><br>I'm no expert, but until others more knowledgeable respond, here's my understanding.<br><br>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.)<br><br>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.<br><br>For example, given different aspect ratios, the new image could either:<br><br>1. ignore the difference (i.e., the current behaviour)<br>2. show the entire original image within the new target, respecting the aspect ratio<br>3. clip the original image to match the new aspect ratio<br><br>For #2, you could shrink the target rect to match the aspect ratio of the original image.<br><br>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.<br><br>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).<br><br>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.)<br><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">--Glenn</span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br>On Aug 11, 2008, at 5:33 AM, Adam Jones wrote:</span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">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.</span></font></pre><br>It works mint as but it obviously scales the image with no respect for the aspect ratio.<br><br>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.<br><br>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?<br><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; ">Thanks Adam</span></font></pre><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br>----------- #!/usr/bin/python</span></font></pre><br>import CoreGraphics<br><br>def resizeimage(srcimage,targetimage, w, h):<br><br>origImage = CoreGraphics .CGImageImport (CoreGraphics.CGDataProviderCreateWithFilename(srcimage))<br><br>origwidth = origImage.getWidth() # not used yet but will be once i work out how to crop<br>origheight = origImage.getHeight() # not used yet but will be once i work out how to crop<br><br>cs = CoreGraphics.CGColorSpaceCreateDeviceRGB()<br>c = CoreGraphics.CGBitmapContextCreateWithColor(w, h, cs, (0,0,0,0))<br><br>c.setInterpolationQuality(CoreGraphics.kCGInterpolationLow)<br>newRect = CoreGraphics.CGRectMake(0, 0, w, h)<br>c.drawImage(newRect, origImage)<br>c.writeToFile(targetimage, CoreGraphics.kCGImageFormatJPEG)<br><br><br>resizeimage(uploadedfile, "/svr/data/images/listings/%s/ %s_160x90.jpg" % (str(listing_id), newimagename), 160,90)<br><br>-------------<br><br><pre><font class="Apple-style-span" face="'Lucida Grande'" size="3"><span class="Apple-style-span" style="font-size: 12px; white-space: normal; "><br>_______________________________________________ Do not post admin requests to the list. They will be ignored. Quartz-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription:&nbsp;<a href="http://lists.apple.com/mailman/options/quartz-dev/email@hidden">http://lists.apple.com/mailman/options/quartz-dev/email@hidden</a></span></font></pre><br>This email sent to email@hidden</blockquote><div><font class="Apple-style-span" color="#540000"><br></font></div></span></font></pre></blockquote></span></font></pre></div><font class="Apple-style-span" face="'Lucida Grande'" size="3"></font></div><font class="Apple-style-span" face="'Lucida Grande'" size="3"></font><font class="Apple-style-span" face="'Lucida Grande'" size="3"></font></blockquote></span></font></pre></div><br><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div><br></div></div></div></span><br class="Apple-interchange-newline"></div><br><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div><br></div></div></div></span><br class="Apple-interchange-newline"></div></div></body></html>