Automatically add blank margins to pictures to obtain 4x5 aspect-ratio?

0

1

I frequently order prints online. However the printing-service is automated and crops out the pictures I upload to a 4x5 ratio. I'd like to be able to add margins (e.g. white) to the pictures BEFORE I upload them, so that they are in 4x5 aspect ratio.

I need to do this for several pictures, so is there a script / macro I can use? You may suggest a solution based on Photoshop.

n a

Posted 2011-01-28T14:39:08.893

Reputation: 131

What image file format(s) are involved? – martineau – 2011-01-28T15:16:54.523

Answers

2

Photoshop can be scripted using what it calls Actions. They're a lot like macros and can be recorded, edited, and played all within the program. Once you've created one, it can be executed manually on individual images or applied to all the images in a folder.

There's extensive information about them in Photoshop's Help. There's also a great number of free ones on the web for all kinds of tasks, so it wouldn't surprise me if there was one that does what you want...or close to it. If not, what you'd like to do sounds fairly easy, so it probably wouldn't be that hard to create your own (assuming you know how to go about doing it in the program). Alternatively, you could download a free one that's close or similar to your goal and edit (modify) it.

Update: I found this web tutorial on creating your own actions -- How to: Crop 10 or more images to a defined size using actions and batch processing in Adobe Photoshop.

martineau

Posted 2011-01-28T14:39:08.893

Reputation: 3 849

To expand on this a little: since the end result is physical prints, one option is to take all of the landscape (aka horizontal) photos, rotate them 90 degrees, and then save as a temporary copy. This way all the photos you are sending to the service are in one orientation. If your photos are not 4x5 (or 8x10) to begin with, you can then take all the photos and apply one tranformation to them all to have them all 5 inches wide. Then the auto-crop won't crop them. Once you figure out your workflow, the actions can be automated as martineau suggested. – horatio – 2011-01-28T16:06:06.860

1

You can do this using a command-line tool that's part of ImageMagick. It's available for Unix/Linux, OS X and Windows.

For example:

convert *.jpg -bordercolor white -border +10+10 %t-4x5.%e

Adds a 10-pixel border around the image. To do aspect-ratio padding, things have to be quite a bit more complex. Here is a Bash script that uses ImageMagick to do what you want: aspectpad.

Paused until further notice.

Posted 2011-01-28T14:39:08.893

Reputation: 86 075

0

I wrote a script in Matlab that solves the problem: It reads all the images in the directory and adds a black margin to achieve the desired aspect ratio.

% Margins

% SPECIFY RATIO
RATIO_HEIGHT = 10;
RATIO_WIDTH = 8;

% READ FILES
files = dir('*.jpg');

% START
clc
for f=1:size(files)
    % READ FILE
    IMG = imread(files(f).name);
    [h w d] = size(IMG);

    % CALCULATE RATIO
    ratio = w/h;

    % PRINT FILENAME
    fprintf('\n\n-------------------------------------');
    fprintf('\n# PROCESSING FILE: \t%s', files(f).name);

    % PRINT ORIENTATION
    if(h > w)
        orientation = 1;
        fprintf('\nPORTRAIT');
    else
        orientation = 2;
        fprintf('\nLANDSCAPE');
    end

    % PRINT DIMENSIONS
    fprintf('\t|\t%i x %i', h, w);

    % CHECK RATIO?
    if(h/w == RATIO_HEIGHT/RATIO_WIDTH)
        fprintf('\n > Correct aspect ratio.');
        NIMG = IMG;        
    else
        fprintf('\n! BAD aspect ratio : %f', h/w);

        % CALCULATE NEW DIMENSIONS
        if(ratio < RATIO_WIDTH/RATIO_HEIGHT)
            ch = h;
            cw = round(RATIO_WIDTH/RATIO_HEIGHT*ch);
        else
            cw = w;
            ch = round(RATIO_HEIGHT/RATIO_WIDTH*cw);
        end

        % PRINT NEW DIMENSIONS
        fprintf('\nNEW DIMENSIONS: \t%i x %i | ADDED: %i x %i', ch, cw, ch-h, cw-w);

        % CREATE NEW IMAGE
        NIMG = uint8(zeros(ch,cw,d));

        % COPY
        for k=1:d
            for i=1:h
                for j=1:w
                    NIMG(i,j,k) = IMG(i,j,k);
                end
            end
        end
    end

    % WRITE
    imwrite(NIMG, ['modified\' files(f).name '.png'], 'png');
end

n a

Posted 2011-01-28T14:39:08.893

Reputation: 131