Mazyod
Mazyod

Reputation: 22559

Bash script to rename images according to their size?

I downloaded a bunch on images using wget, and unfortunately, there was a huge drawback...

The downloaded images had the same name! So, the script automatically appended .1, .2 , ...etc. at the end:

Accept-Male-User-icon.png
Accept-Male-User-icon.png.1
Accept-Male-User-icon.png.2
...

So, am looking for a script that would take these files and rename them according to their size, given that their size could be one of the following:

(256x256, 128x128, 64x64, 48x48, 32x32, 16x16)

So I end up with something like this:

Accept-Male-User-icon256.png
Accept-Male-User-icon128.png
Accept-Male-User-icon64.png
...

Thanks!!

Upvotes: 4

Views: 1830

Answers (2)

eumiro
eumiro

Reputation: 212835

If you have ImageMagick installed, you can try:

for a in *.png*; do mv -i $a ${a%\.png*}`identify -format '%w' $a`.png; done

Test it and if it works, remove the -i switch after mv.

Upvotes: 10

Mikel
Mikel

Reputation: 25596

  1. Get the sizes using something like pngcheck, pnginfo, or imageinfo --width --height
    You will probably need to install it, e.g. using apt-get or yum
  2. Strip the suffix using newname=${filename%.*}
  3. Rename using mv

Upvotes: 3

Related Questions