rename images based on color of first pixel

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
bruno@epigraph.us
Posts: 2
Joined: 2020-01-23T17:02:03-07:00
Authentication code: 1152

rename images based on color of first pixel

Post by bruno@epigraph.us »

Is it possible to use ImageMagick to rename hundreds of images based on the hex code of the 1st pixel in the image. (The first pixel being the top left pixel)?

If so would it be possible to add that hex code to the suffix of the image file name?

As such:
Image_fileName_3282FE.jpg

Any help will be very appreciated!

Bruno

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

Re: rename images based on color of first pixel

Post by snibgo »

IM cannot rename files. It could read your input JPEGs and write new JPEGs with new names, but that would be a lossy process.

IM can find the hex code you want, and then you could use an operating system command to rename.
snibgo's IM pages: im.snibgo.com

bruno@epigraph.us
Posts: 2
Joined: 2020-01-23T17:02:03-07:00
Authentication code: 1152

Re: rename images based on color of first pixel

Post by bruno@epigraph.us »

Thank you Snibgo,

That's encouraging to know and a bit more complex than I expected.
On a windows machine what would be the most efficient workflow to pass those hex codes to a cmd function that could rename the images?

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

Re: rename images based on color of first pixel

Post by snibgo »

Code: Select all

setlocal enabledelayedexpansion

for %%F in (*.jpg) do (
  for /F "usebackq" %%A in (`magick ^
    %%F ^
    -depth 8 -format "ONECOL=%%[hex:p{0,0}]" ^
    info:`) do set %%A

  echo ONECOL=!ONECOL!

  ren %%F %%~nF_!ONECOL!.jpg
)
Do a backup first. If it doesn't do what you want, your filenames will be snarfed.

The "echo" command isn't needed. Remove it if you want.

It could be made slightly faster by doing the "ren" directly instead of via the "set", but I doubt this makes significant difference to the time.
snibgo's IM pages: im.snibgo.com

Post Reply