мне нужен быстрый и надежный способ изменить много изображений. Существует много белого вокруг моих изображений, о которых я хочу избавиться. Проблема, это - много подобных изображений, но у них есть различные размеры. Пример здесь: ссылка Мои изображения имеет только красно-синюю часть и огромный пробел вокруг них. Я хочу изображения с тем же форматом и меньшим пробелом.
Сначала я нуждался бы в максимальном размере цветной части всех изображений и затем обрезал бы все изображения к формату самого большого размера. Но центр изображения должен остаться центр.
Это так или иначе возможно с преобразованием или каким-либо другим инструментом командной строки?
Хорошо, после другого часа Google я предложил свое собственное решение. В эта ссылка существует сценарий, который проходит все изображения и обрезки их, в то время как центр остается центр. Теперь у меня есть свои кадрированные изображения. С
ww=`convert -ping "$f" -format "%w" info:
И циклически повторяющийся через весь ww оценивает за все изображения, я получаю wmax.
wmax=`convert xc: -format "%[fx:max($ww,$wmax)]" info:`
С этим и несколькими циклами
convert $f -gravity center -background white -extent ${wmax}x${hmax} ${f}-canvas.png
я получаю результаты. Это не хорошо, я обрезаю еще раз, чем необходимый, но это делает задание, и я должен закончить свой тезис.
#!/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
Попробуйте следующий сценарий удара:
#!/bin/bash
max_width=0
max_height=0
border=10
# Trim all files to remove the white borders
for i in *.png; do
convert $i -trim "${i%.*}"__trimmed.png
done
# 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;
done
# Add a small border (optional)
max_width=$(($max_width+$border))
max_height=$(($max_height+$border))
# 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
done
rm -f *__trimmed.png