Crop Specific Portion of Image

Hi ckeyes888,

The ASObjC Runner wouldn’t compile after the first run for me. I can’t remember what I did exactly, but probably: repair disk permissions, cold start, zap the PRAM, and I might have made it 64 bit I think in the info window. Oh, and try quitting it in the status menu.

Edited: if you don’t have the menu icon, then:
tell app “ASObjC Runner” to quit

gl,
kel

Restarted everything and the script is running fine.
Just having issues with the height and width settings. Are they percentages do you know?
Been experimenting with them but can’t seem to come up with any pattern to how they work.

I assume one set of them is the final image size and the second the position it crops from?

Thanks!

Carl

I think height and width is usually pixels.

Edited: here’s the rectangle definition:

Edited: note that the origin might be the upper left corner instead of the lower left. I’m not sure and haven’t run the script yet.

gl,
kel

Got it. Can’t thank you enough for the excellent solution!
Just took a lot of number combinations.

It’s amazingly fast as well :slight_smile:

Carl

Funny how they never mention that a ‘rectangle’ is an array of dictionaries. How would you even know this?

Sorry – the x and y values are the origin, and the others are the width and height. But the origin is the bottom-left, not top-left.

It’s much quicker than using apps that display images first.

It’s not, though. It’s actually a struct, which is C thing that you can use a bit like a dictionary or record, but really just packs all the bits together in a structured way. There’s no direct AS equivalent because, apart from anything else, AS doesn’t support primitive data types – everything in AS is really an object.

Anyway, ASObjC gets around this by automatically converting between a handful of structs and AS records. So an NSRect is returned as:

{origin:{x:x1, y:y1}, |size|:{|width|:theWidth, height:theHeight}} 

The origin value is another struct, NSPoint, and the size is also a struct: NSSize.

The other one you’ll strike often in ASObjC is NSRange, which is how ranges of text are typically described: {|location|:theLocation, |length|:theLength} . The location is the zero-based offset of a string, and the length is the number of characters in the range.

Perfect…you guys are tha men.

Thanks,

Carl

Interesting. The script works perfectly but returns an error: unrecognized function size (-10000)
when I move it to an identical machine running the same ASObjC Runner and OSX version.

Tried just changing a few size numbers but the error persists.

Any idea what it may be?

Thanks,

Carl

Hi Shane,

Thanks for the great quick tutorial. :slight_smile: It looks more like a record of records.

Kel

Does the term “size” still appear in pipes? Any third-party scripting additions?

For completeness. I’ve fixed the errors in the first part of Carl’s original script so that it works for me. There were numerous errors but your initial problem was that the path needs to be made into a file or alias object for iMagine Photo to know what to do with it. See script below.

In terms of performance I’d think that this would give Shane’s excellent solution a run for its money.

I wouldn’t however recommend using iMagine Photo as I haven’t updated it since 2006 and it is not going to be updated.

Kevin


set thisFile to "MacintoshHD:Users:ktam:Phone Pictures:07-08-2011 17.18.35.jpg" as alias
set exportFile to "MacintoshHD:Users:ktam:Desktop:cropped.jpg"

tell application "iMagine Photo"
	set thisImporter to import graphic thisFile
	set {x, y, xDim, yDim} to source rectangle of thisImporter
	tell thisImporter to make exporter
	set the export file location of thisImporter to file exportFile
	set theTop to (yDim / 4) as integer
	set theLeft to (xDim / 4) as integer
	set theRight to ((xDim / 2) + theLeft) as integer
	set theBottom to ((yDim / 2) + theTop) as integer
	set the source rectangle of thisImporter to {theLeft, theTop, theRight, theBottom}
	export thisImporter
	close thisImporter
end tell

Presumably we’re both doing much the same thing. The big factor is avoiding screen redraw/UI.

Is there some reason for not updating, other than, say, better things to do with your time? Just curious…

I’ve been doing other things for the last 10 years. Though I did do some updates to iMagine Photo for the first few years. I am however working for myself again so I’m onto a new project.

I know that iMagine Photo will run in the next version of OS X but I hadn’t expected it to.

I don’t see any point in updating iMagine Photo now as it depends on the carbon API of Quicktime which is 32 bit bound and I can’t see it surviving post Mavericks.

I am now working on a replacement for iMagine Photo but it won’t be applescript, but instead a mixture of unix shell scripts and json. I will have an alpha version released late October early November. It will be missing a lot of the final functionality I have planned but will have a lot of useful functionality. I am expecting to finish coding for the first alpha version release within a couple of weeks and I’ll then be onto writing the documentation.

In some ways the new tool is similar to iMagine Photo. It is running as a LaunchAgent (a user process daemon). iMagine Photo can be run as a UIElement faceless application. Like iMagine Photo the LaunchAgent stays alive between each message it receives as long as it is maintaining state (there are still objects alive that can be referenced) and maintains an idle time which keeps the tool alive for a specified period after all state is destroyed.

The new tool will be free. It will only run on Mavericks or later.

When I do the alpha release I’ll be looking for testers.

Kevin

What tool are you using to crop your images?

Can we see the script you using?

Kevin

Cropping should have very little effect on the image quality, but if you’re starting with a PDF and going to any sort of bitmap, type will have to suffer. You could increase the DPI, but although that would help with the type, it would probably do awful things with any pictures – if you’re getting 72dpi, it’s because the original is probably 72dpi.

The dpi is irrelevant – what matters is the number of pixels.

Where is it downscaled?

I’m not sure I follow. When you crop an image, you always end up with fewer pixels – that’s more or less the definition of cropping while maintaining the same resolution.

What cropping are you trying to apply to the image?

You might care to read this: proshooter.com/article_whatisa300dpiJPeg.htm