Get just the best image in multiple image files like icons

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sbertolucci
Posts: 2
Joined: 2019-03-08T08:43:32-07:00
Authentication code: 1152

Get just the best image in multiple image files like icons

Post by sbertolucci »

Hi,
I'm not very good in use of ImageMagick, so I need some help

I inherited from a previous coworker a huge library of icons. The problem is that many of them are very old and, hence, useless nowadays.
I'd like to put aside the old ones and keep just the modern ones.
In order to do that I developed a script which prepends to the original icon name the size and color depth in square brackets, so that for example the file MyIcon.ico becomes [48x48 8] - MyIcon.ico
I use identify -format "%wx%h %z in my script
My problem arises when the icon contains several images inside.
In these cases I get a multiple output, so that, for example identify -format "%wx%h %z|" myicon1.ico (the | in format is used to clarify the output) returns:
48x48 8|32x32 8|24x24 8|16x16 8|256x256 8|256x256 8|128x128 8|96x96 8|72x72 8|64x64 8|48x48 8|32x32 8|24x24 8|16x16 8|
I would like to pick just the 256x256 8 entry, but I don't know if there is any way to do this like sorting the output or similar.

I use ImageMagick 7.0.8-32 Q16 x64 2019-03-05 on Windows

Thanks in advance

Stefano
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get just the best image in multiple image files like icons

Post by snibgo »

I would write it as a bat script. -format "%w %s\n" gives the width and index number of images in an ICO file. The script would read these and find the index number of the greatest width.

An alternative would use unix text tools: sort the list numerically, and the last line of that is the largest, and parse ("cut") that to get the index number.
snibgo's IM pages: im.snibgo.com
sbertolucci
Posts: 2
Joined: 2019-03-08T08:43:32-07:00
Authentication code: 1152

Re: Get just the best image in multiple image files like icons

Post by sbertolucci »

Hi snibgo,

yes, I am thinking to some bat using the "for" syntax, but before doing it, which will be somehow cumbersome, I thought that maybe among the zillions of possibilities of ImageMagik there was already one fitting my needs, I'm lazy ;-)

Stefano
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get just the best image in multiple image files like icons

Post by snibgo »

I'm lazy too, and a BAT script is easier for me to figure out (and understand, and explain) than an IM-only method like this one. Windows BAT syntax:

Code: Select all

magick ^
  in.ico ^
  ( -clone 0--1 ^
    -layers Merge ^
  ) ^
  -channel A -evaluate Multiply %%[fx:w==u[-1].w?1:0]% +channel ^
  +delete ^
  -background None -layers merge ^
  out.png
I think this will set out.png to the widest image in in.ico. (Test it to be sure!)

We clone the images and flatten them. The result is the width of the widest input (and the height of the highest input, but that doesn't matter). This is now the last image in the list.

For every image in the list, we multiply the alpha channel by zero or one. We multiply by one only if its width is equal to the width of the last image in the list. Otherwise we make it transparent.

Delete that last image in the list and flatten the rest. All except the largest of the images will be entirely transparent. (If two or more images are equally widest, this will composite them over each other.)
snibgo's IM pages: im.snibgo.com
Post Reply