skip to Main Content

We need to have huge amounts of png’s resized to be divisible by 12, each png is variable in size and the image needs to stay 1:1 in the top left.

At the moment were having to manually bring in each file into Photoshop and enlarge the canvas on the x+y to be divisible by 12 and keep the image in the top left corner. With the amount of png’s we need doing now and in future we need an automated process.

2

Answers


  1. Never mind, this is a possible solution for your problem. This script will run in MATLAB or Octave (Octave is an open-source alternative to MATLAB, so you might want to use that.)

    Copy the following function into a file and call it resizeIm.m. Then start Octave and call this function for every image you have.

    function resizeIm(fileName)
        % Read image
        origIm = imread(fileName);
    
        % Get size and calculate new size
        origSize = size(origIm);
        div = ceil(origSize ./ 12);
    
        % Create new, padded image
        newIm = zeros(12*div,class(origIm));
        newIm(1:origSize(1),1:origSize(2)) = origIm;
    
        % Write image to new file
        [dir, name, ext] = fileparts(fileName);
        newFileName = [dir,name,"_resized",ext];
        imwrite(newIm,newFileName);
    end
    

    The function can be called by

    resizeIm("C:pathtofilemyimage.png")
    
    Login or Signup to reply.
  2. I would do this with ImageMagick, which is free and installed on most Linux distros and also available for OSX and Windows from here.

    This little bash script will resize all the PNG files in the current directory and save them with the original name in the subdirectory called output. It is pretty easy to read – it basically loops through all the PNG files in the directory. It then uses ImageMagick’s built-in calculator to work out the size of your output file as nearest multiple of 12. Then it loads the image and extends the background using transparent pixels (-background none) to that size (using -extent) and leaves the original image in the top-left corner (-gravity NorthWest).

    #!/bin/bash
    # Make output directory - ignore errors
    mkdir output 2> /dev/null
    
    # Make sure we don't barf if there are no files
    shopt -s nullglob
    
    # Make sure we process *.png, *.PNG, *.pNg etc
    shopt -s nocaseglob
    
    # Loop through all pngs in current directory
    for f in *.png; do
    
        # Calculate new extent as nearest multiple of 12
        # In general, to round x to nearest n, you do ((x+n-1)/n)*n
        extent=$(convert "$f" -format "%[fx:12*round(((w+11)/12)-0.5)]x%[fx:12*round(((h+11)/12)-0.5)]" info: )
    
        # Now extend canvas transparently to new size and leave original image in top-left
        convert "$f" -background none -gravity northwest -extent $extent output/"$f"
    done
    

    P.S. If installing ImageMagick on OSX, please ask for advice before trying.

    P.P.S. If you have 10,000+ images to resize, and you do it often, and you are on OSX or Linux (probably not Windows), I would recommend GNU Parallel. If that is likely, please ask.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search