Wednesday, July 17, 2019

#1 2013-09-19 12:38:16 pm

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Crop Specific Portion of Image

Hey,

Somehow this has become a lot more difficult than I would have thought.
Just need to crop an image to a specified size from a specified point on the image.

The image I'm cropping I'm downloading using Paparazzi! which works great.
Here's the script for that if anyone is interested.

Applescript:

tell application "Paparazzi!.app"
   activate
   set minsize to {1004, 3870}
   set fileName to "whitefishT.jpg"
   set iconame to (true)
   Â«event Pzi!Capt» "[url]http://www.onthesnow.com/montana/whitefish-mountain-resort/skireport.html?XE_AFF=rss[/url]" given «class cSiz»:minsize
   repeat while «class pBzy»
       -- To wait until the page is loaded.
   end repeat
   save as «constant sFMTJPEG» in "Macintosh HD:Users:TL:desktop:Ski Images:" & fileName
   quit application "Paparazzi!.app"
end tell

I hear that iMagine Photo can do the crop but I'm not having any luck with a script for it.
Here's what I have so far:

Applescript:

set thisFile to "Macintosh HD:Users:TL:desktop:Ski Images:whitefishT.jpg"
set exportFile to "Macintosh HD:Users:TL:desktop:Ski Images:whitefish.jpg"

tell application "iMagine Photo"
   set thisImporter to thisFile
   set {x, y, xDim, yDim} to thisImporter
   tell thisImporter to make exporter
   set the export file location of thisImporter to exportedFile
   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

end tell


set thisFile to "Macintosh HD:Users:TL:desktop:Ski Images:whitefishT.jpg"
tell application "iMagine Photo"
   set exporterTypes to the available exporter types
   set theFolder to "Macintosh HD:Users:TL:desktop:Ski Images:"
   set fileName to "whitefish.jpg"
   set thisImporter to import graphic thisFile
   if the component error of thisImporter is not equal to 0 then
       close thisImporter
       return
   end if
   tell thisImporter to make exporter with properties {export file type:exportType, export folder location:theFolder}
   set theExtension to the export file extension of thisImporter
   set the export file name of thisImporter to (fileName & theExtension)
   export thisImporter
   close thisImporter
end tell

It seems to get hung up with the path to the image which I know is correct as it's the same one I used
in the Paparazzi! script.

Maybe some other way way to crop an image with a script? I tried Image Events but it will only crop from the center.

Here's the cropped portion I'm trying to end up with: http://www.cidesignhouse.com/ski_image.jpg

Any ideas or info greatly appreciated!

Thanks,

Carl

Last edited by ckeyes888 (2013-09-19 12:38:52 pm)

Offline

 

#2 2013-09-19 05:35:34 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

FWIW, you can do this fairly easily in AppleScriptObjC. You'd need to save your code as a Cocoa-AppleScript application from AppleScript Editor:

Applescript:

set thisFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefishT.jpg"
set exportFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefish.jpg"
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile_(thisFile)
set theSize to (theImage's |size|()) as record
set theHeight to height of theSize
set theWidth to width of theSize
set newRect to {{x:theWidth / 4, y:theHeight / 4}, {width:theWidth / 2, height:theHeight / 2}}
theImage's lockFocus()
set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect_(newRect)
theImage's unlockFocus()
set theData to theRep's representationUsingType_properties_(current application's NSJPEGFileType, {NSImageCompressionFactor:0.9})
theData's writeToFile_atomically_(exportFile, true)

Alternatively, you could save it as a normal AS script, using ASObjC Runner to run the AppleScriptObjC part:


Applescript:

script cropPic
   set {thisFile, exportFile} to current application's NSApp's passedValue() as list
   set theImage to current application's NSImage's alloc()'s initWithContentsOfFile_(thisFile)
   set theSize to (theImage's |size|()) as record
   set theHeight to height of theSize
   set theWidth to width of theSize
   set newRect to {{x:theWidth / 4, y:theHeight / 4}, {width:theWidth / 2, height:theHeight / 2}}
   theImage's lockFocus()
   set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect_(newRect)
   theImage's unlockFocus()
   set theData to theRep's representationUsingType_properties_(current application's NSJPEGFileType, {NSImageCompressionFactor:0.9})
   theData's writeToFile_atomically_(exportFile, true)
end script

set thisFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefishT.jpg"
set exportFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefish.jpg"
tell application "ASObjC Runner" to run the script {cropPic} passing {thisFile, exportFile}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2013-09-20 09:19:17 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Re: Crop Specific Portion of Image

Thanks a bunch. I'm trying the second version that uses ASObjC Runner. I have it installed
but the script won't compile. Gets stuck on "passing" in the last line:

Applescript:

tell application "ASObjC Runner" to run the script {cropPic} passing {thisFile, exportFile}

Really appreciate the help!

Carl

Last edited by ckeyes888 (2013-09-20 09:20:24 am)

Offline

 

#4 2013-09-20 09:45:05 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: 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

Last edited by kel1 (2013-09-20 09:47:37 am)


Os 10.10.3
Mbp

Offline

 

#5 2013-09-20 11:00:40 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Re: Crop Specific Portion of Image

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

Offline

 

#6 2013-09-20 11:08:58 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Crop Specific Portion of Image

I think height and width is usually pixels.

Edited: here's the rectangle definition:

NSRect
Represents a rectangle.

typedef struct _NSRect {
      NSPoint origin;
      NSSize size;
} NSRect;
Fields
origin
The origin of the rectangle (its starting x coordinate and y coordinate).
size
The width and height of the rectangle, as measured from the origin.


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

Last edited by kel1 (2013-09-20 11:48:47 am)


Os 10.10.3
Mbp

Offline

 

#7 2013-09-20 11:55:16 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Re: Crop Specific Portion of Image

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

It's amazingly fast as well :-)

Carl

Offline

 

#8 2013-09-20 03:43:44 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Crop Specific Portion of Image

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


Os 10.10.3
Mbp

Offline

 

#9 2013-09-20 05:20:39 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

ckeyes888 wrote:

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.


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 amazingly fast as well :-)


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2013-09-20 05:34:49 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

kel1 wrote:

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


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:

Applescript:

{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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2013-09-20 06:30:12 pm

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Re: Crop Specific Portion of Image

Perfect...you guys are tha men.

Thanks,

Carl

Offline

 

#12 2013-09-22 10:16:54 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 138

Re: Crop Specific Portion of Image

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

Offline

 

#13 2013-09-22 11:54:44 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Crop Specific Portion of Image

Hi Shane,

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

Kel


Os 10.10.3
Mbp

Offline

 

#14 2013-09-22 05:11:15 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

ckeyes888 wrote:

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.


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#15 2013-09-23 02:02:30 am

ktam
Member
From:: UK
Registered: 2003-01-24
Posts: 246
Website

Re: Crop Specific Portion of Image

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

Applescript:


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


MovingImages is a scripting tool for non-linear video editing.
http://zukini.eu/movingimages

iMagine Photo is deprecated. I would not use it for any new projects.


Filed under: image, Graphics, cropping

Offline

 

#16 2013-09-23 03:36:51 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

ktam wrote:

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


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...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#17 2013-09-23 04:05:52 am

ktam
Member
From:: UK
Registered: 2003-01-24
Posts: 246
Website

Re: Crop Specific Portion of Image

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

Last edited by ktam (2013-09-23 04:17:40 am)


MovingImages is a scripting tool for non-linear video editing.
http://zukini.eu/movingimages

iMagine Photo is deprecated. I would not use it for any new projects.

Offline

 

#18 2014-10-22 12:27:34 pm

ktam
Member
From:: UK
Registered: 2003-01-24
Posts: 246
Website

Re: Crop Specific Portion of Image

What tool are you using to crop your images?

Can we see the script you using?

Kevin


MovingImages is a scripting tool for non-linear video editing.
http://zukini.eu/movingimages

iMagine Photo is deprecated. I would not use it for any new projects.

Offline

 

#19 2014-10-22 04:25:10 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

nikocb wrote:

How can I increase the quality of the images post-crop?
My images have small text on them and they are unreadable after the crop, even with the lowest amount of compression.

Can I change the resolution or prevent to images from being reduced to 72ppi?


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#20 2014-10-23 04:33:58 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

nikocb wrote:

Even when starting with a 300dpi image, it gets reduced to 72dpi. Is there a way to keep it at 300?


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#21 2014-10-24 04:40:52 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

nikocb wrote:

If I set the width and height to the exact amount of pixels I need, it still downscales it


Where is it downscaled?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#22 2014-10-27 05:11:38 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

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?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#23 2014-11-05 04:39:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5755

Re: Crop Specific Portion of Image

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)