Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1745  / 2 Years ago, thu, february 3, 2022, 10:59:47

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?


More From » scripts

 Answers
2

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.



    #!/bin/sh
# 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
do
echo "Processing $f file..."
# take action on each file. $f has current file name
wmax=0
hmax=0
# 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
done

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
do
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
done

[#21142] Friday, February 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peratingcit

Total Points: 253
Total Questions: 122
Total Answers: 94

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;