i need a fast and reliable way to modify a lot of images. There is a lot of white around my pictures i want to get rid about. The problem is, it is a number of similar pictures, but they have different sizes. An example is here: a link My pictures have only the red and blue part and a huge white space around them. I want pictures with the same format and less white space.

First i would need the maximum dimension of the non-white part of all pictures and then crop all pictures to the format of the biggest dimension. But the center of the image has to stay the center.

Is this somehow possible with convert or any other command line tool?

  • Do you want to create 4 images from the link you provided? I ask because the white margin is not that big in your example (if you consider the whole picture). – Sylvain Pineau Feb 27 '15 at 9:12
  • The link is more an example how my pictures look. I have for example 4 pictures that look like the one in the link. But in my pictures there is a lot of white space around. – ano302 Feb 27 '15 at 9:13
  • For pdf images, I have found pdfcrop to be excellent for this kind of thing. – steeldriver Feb 27 '15 at 13:18

Okay, after an other hour of google i came up with my own solution. In this link there is a script that goes through all images and crops them while the center stays the center. Now i have my cropped pictures. With

ww=`convert -ping "$f" -format "%w" info:

And cycling through all ww values for all pictures i get wmax.

wmax=`convert xc: -format "%[fx:max($ww,$wmax)]" info:`

With that and several loops

 convert $f -gravity center -background white -extent ${wmax}x${hmax} ${f}-canvas.png

I get the results. It is not nice, i crop one more time than necessary but it does the job and i have to finish my thesis.

    # gives the largest x and y values of all  png files in directory and puts them in the center of a canvas. This canvas is 10px*10px larger than the largest png file.

    # We cycle over all pngs to get maximum w and h
    for f in *.png
      echo "Processing $f file..."
      # take action on each file. $f has current file name
    # width
    ww=`convert -ping "$f" -format "%w" info:`
    #echo $ww
    # height
    hh=`convert -ping "$f" -format "%h" info:`
    #echo $hh
    wmax=`convert xc: -format "%[fx:max($ww,$wmax)]" info:`

    hmax=`convert xc: -format "%[fx:max($hh,$hmax)]" info:`

    #  centertrim $f
    #  rm $f
    #  mv $f.out $f

    echo $wmax
    echo $hmax
    wmaxp10=$(($wmax + 10 ))
    echo $wmaxp10
    hmaxp10=$(($hmax + 10 ))
    echo $hmaxh10
    # now we cycle through all pictures and add them to a canvas with $wmax+10 $hmax+10
    for f in *.png
      echo "Processing $f file..."
      # take action on each file. $f has current file name
    echo convert $f -gravity center -background white -extent ${wmaxp10}x${hmaxp10} ${f}-canvas.png
    convert $f -gravity center -background white -extent ${wmaxp10}x${hmaxp10} ${f}-canvas.png
    #  centertrim $f
    #  rm $f
    #  mv $f.out $f

Try the following bash script:



# Trim all files to remove the white borders
for i in *.png; do
    convert $i -trim "${i%.*}"__trimmed.png

# Find the max width and height
for i in *__trimmed.png; do
    w="$(identify -format "%w" $i)"
    h="$(identify -format "%h" $i)"
    if (( $w > $max_width )); then max_width=$w; fi;
    if (( $h > $max_height )); then max_height=$h; fi; 

# Add a small border (optional)

# Add borders to all pictures so that they all have the same size
# "-gravity center" will center them
# -background None will avoid the default white background as your sample image
# was a png with a transparent backgroud
for i in *__trimmed.png; do
    convert $i -background None -gravity center -extent "${max_width}x${max_height}" "${i%__trimmed.*}".png

rm -f *__trimmed.png
  • Thank you for your effort! Unfortunately you loose the center if you do not have center symmetric pictures. – ano302 Feb 27 '15 at 10:48
  • I don't understand, we have the same convert commands, and I did test them with asymmetric pictures. Anyway good luck with your thesis. – Sylvain Pineau Feb 27 '15 at 10:54
  • The difference is, that i first use the script in the link. This script crops as much as possible, but it only crops the same amount from right and from left. – ano302 Feb 27 '15 at 11:59
  • That ensures the center stays the center. Here a LINK to explain it better. 1st one original, 2nd one your script, 3rd one my script.Sorry for double post. – ano302 Feb 27 '15 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.