Thursday, November 23, 2017

You are not logged in.

• Index
•  » Code Exchange
•  » Computations for padding and cropping correctly by proportion

## #1 2015-03-08 03:53:59 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

### Computations for padding and cropping correctly by proportion

We may have totally different opinions about what "correct" is with respect cropping of pictures.

The reason for this, is to retain as much as possible of pictures, while getting them cropped - or padded into "letter-box" format.

The way I chosen to do it, is to keep as much as possible of the original images, without scaling, I leave that as the next step. (The cropping and padding command of Image events, appears to be the same, but you can't specify a padding colour for the cropping. )

Those scripts alters the original image, please play with copies of your images.
Idealistic cropping algorithm, as it tries to keep as much as possible of the original image, while respecting the aspect ratio.

#### Applescript:

-- Copyright Â© 2015 McUsr, you are free to use, and share per email, but not to publish on a webpage or in a book.
-- You are free to derive the computations, but you are not free to publish them as your own work.
-- No guarrantees about anything what so ever. [url]http://macscripter.net/viewtopic.php?pid=179196#p179196[/url]
set W_prop to 9
set H_prop to 16
-- Test of Cropping to proportions
-- The idea is that the cropped image, should keep as much as the original.
-- It should be inscribed, rather than padded when circumstances are so.
-- ( the aspect ratio, should be kept.)

set this_file to (choose file) as text

try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- get dimensions of the image
copy dimensions of this_image to {W, H}
-- calculate crop dimensions

if (W_prop â‰¥ H_prop) and (W â‰¥ H) then
-- tested with the proportion 16x9, and sizes 500x300,500x270,500x800
set new_H to ((W * H_prop) / W_prop)
--
if H â‰¥ new_H then
-- we should crop H
set crop_dimensions to {W, new_H} -- A 500x300 -> 500x281,25
else
-- the height is too small, so we'll crop the width - and height
set new_W to (H * W_prop) / H_prop
set crop_dimensions to {new_W, H} -- B 500x270 -> 480x270
end if

else if (W_prop â‰¥ H_prop) and (H > W) then
-- We should keep the width we have!
set new_H to ((W * H_prop) / W_prop)
set crop_dimensions to {W, new_H} -- C 500x800 -> 500x281,25
-- --------------------------------------------------
else if (H_prop â‰¥ W_prop) and (H â‰¥ W) then
-- tested with the proportion 9x16, and sizes 300x500,270x500,800x500
set new_W to ((H * W_prop) / H_prop)
if W â‰¥ new_W then
-- width is larger than biggest length
-- we crop it!
set crop_dimensions to {new_W, H} -- D 300x500 -> 281,25x300
else
-- we keep the width, and crop the height.
set new_H to (W * H_prop) / W_prop
set crop_dimensions to {W, new_H} -- E 270x500 -> 270X480

end if
else -- (H_prop â‰¥ W_prop) and (W > H)
-- We'll keep the height we have.
set the new_W to (H * W_prop) / H_prop
set crop_dimensions to {new_W, H} -- F 800x500 -> 281,25x500
end if
-- perform action
crop this_image to dimensions crop_dimensions
-- save the changes
save this_image with icon
-- purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try

Idealistic padding algorithm, in that it tries to pad as little as possible of the image.

#### Applescript:

-- Copyright Â© 2015 McUsr, you are free to use, and share per email, but not to publish on a webpage or in a book.
-- You are free to derive the computations, but you are not free to publish them as your own work.
-- No guarrantees about anything what so ever. [url]http://macscripter.net/viewtopic.php?pid=179196#p179196[/url]
set W_prop to 16
set H_prop to 9
-- Test av padding to proportions, - implemented!
set this_file to (choose file) as text
try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- get dimensions of the image
copy dimensions of this_image to {W, H}
-- calculate pad dimensions
if (W * H_prop) â‰¥ (H * W_prop) then
set the new_H to (W * H_prop) / W_prop
set pad_dimensions to {W, new_H}
else
set the new_W to (H * W_prop) / H_prop
set pad_dimensions to {new_W, H}
end if
-- perform action
-- save the changes
save this_image with icon
-- purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try

Enjoy!
Edited! I realized I had just copy pasted the test values from h_prop > w_prop, it is now fixed.
And added declarations of origin, and then some.

Last edited by McUsrII (2015-03-08 05:11:25 pm)

Offline

• Index
•  » Code Exchange
•  » Computations for padding and cropping correctly by proportion