0

Given batch of vertical and horizontal rectangular images :

enter image description here rect-h.png

enter image description here rect-v.png

How to convert batch of vertical and horizontal rectangular images into square images ?

So to get same sizes, not-cutted, no disformed :

enter image description here rect-h-sq.png

enter image description here rect-v-sq.png


I currently use

mkdir -p ./temp ./png                                  # create folders to work on copies of data and store final png output
cp ./* ./temp                                          # copies to ./temp, so to word on copies 
for file in ./temp/*.png                               # loop on the [edited] copies in ./temp
do
  keyIn=$(basename "$file" .png)                       # name of the file minus .png
  keyOut=$(basename "$file" .png)-sq.png               # name of the file minus .png, plus .-red.png 
  convert -background none -density 1200 ./temp/$keyIn.png -resize 300x300\! ./png/$keyOut   
done

But it fails.

Note: Density is there because I often work with svg as well.

fmw42
  • 46,825
  • 10
  • 62
  • 80
Hugo LOPEZ
  • 605
  • 8
  • 21
  • You cannot do that without either padding or cropping or deforming the images. Please clarify what you think you want done from these options. – fmw42 Mar 29 '18 at 16:48
  • (In the question : "So to get same sizes, not-cutted, no disformed". Also, i answered the question there https://stackoverflow.com/a/49556858/8992875 ) – Hugo LOPEZ Apr 05 '18 at 13:53

1 Answers1

2

Padding works :

mkdir -p ./temp ./png                                  # create folders to work on copies of data and store final png output
cp ./* ./temp                                          # copies to ./temp, so to word on copies 
for file in ./temp/*.png                               # loop on the [edited] copies in ./temp
do
  keyIn=$(basename "$file" .png)                       # name of the file minus .png
  keyOut=$(basename "$file" .png)-sq.png               # name of the file minus .png, plus .-red.png 
  convert -background none -density 1200 ./temp/$keyIn.png \
     -thumbnail '300x300>' -background white \
     -gravity center -extent 300x300 -resize 300x300\! ./png/$keyOut   
done
Hugo LOPEZ
  • 605
  • 8
  • 21
  • I want the size of the square should be equal to the largest side of the original photo E.g. Original: 500 x 400 => Output:500 x 500 Original: 400 x 600 => Output: 600 x 600 Can you help me? – diego Feb 14 '22 at 19:04