How to Batch Convert Filenames from Decimal to Hex Color Codes?

0

I asked this question first over on Graphic Design... Since then I was able to learn; and to utilize the fact that a program I was unfamiliar with (ImageMagick) has the capability to output RGB values to the filename using the following:

magick *.png -set colorspace RGB -set filename:f "%[pixel:p]" %[filename:f].png

So I took that step and it worked. Fast forward to the asking of this question here. I have a folder full of image files made up of a single color. I was able to use ImageMagick as shown above to change the filenames to correspond with the colors contained in the respective images. Here is a screenshot from inside the folder:

RGB Decimal Filenames

My only question is: How can I convert the filenames from decimal form to hex?

So, for example, I would like the names of the files to be changed from "rgb(13,12,12).png" to the corresponding hex color code: "0D0C0C.png"

This is turning out to be more difficult than I had hoped... A Windows cmd script or a BAT I could run would be ideal. Can anyone here possibly help me to pull this off?

  • I am on Windows 10 and I have installed GOW (GNU On Windows) which affords me the following *nix tool listed here.
    Editor's note: The list includes the following programs that (IMO) might be useful: sed, bash, bc, dc, expr, printf (and, of course, mv) — but, notably, not awk.

BANG

Posted 2017-06-02T21:50:13.220

Reputation: 101

Question was closed 2017-06-02T22:21:42.943

3

Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.

– DavidPostill – 2017-06-02T22:21:35.807

Answers

1

With Powershell, you could:

  • use "rgb(13,12,12).png" -replace "rgb(","" to strip the first four
  • use "13,12,12).png" -replace ").png","" to strip the closing
  • use "13,12,12" -split "," to separate the pieces
  • use "{0:x}" -f 13 to convert 13 to hex

This should get you started.

If you get stuck on a particular part of this, you can update your question.

Cory Knutson

Posted 2017-06-02T21:50:13.220

Reputation: 287

Thanks for the reply! I was hoping for something a little more automated. There are too many files to go through one by one like this – BANG – 2017-06-03T02:24:49.873

Yes, those are all steps in a script. You just need to fill in the variables to pass the pieces. – Cory Knutson – 2017-06-03T17:54:10.410

1

Your question doesn't specify what kind of script you want to do this with. You can use python:

import os

for filename in os.listdir('<your-directory>'):
    if filename.endswith('png'):
        rgb = filename.strip('rgb(').strip(').png').split(',')
        hex = [format(int(c), '02x') for c in rgb]
        new_filename = ''.join(hex) + '.png'
        os.rename(filename, new_filename)

This assumes that all the png files in that directory are in the format you specified.

TheMonkeyKing

Posted 2017-06-02T21:50:13.220

Reputation: 203

Thanks for the reply! This sounds like it would be an easy solution... but I couldn't get it to do anything. I think I was probably entering the directory part incorrectly in replacing '<your-directory' with 'C:/Users/Username/Desktop/test'. TL;DR Python is hard – BANG – 2017-06-03T02:23:07.940

@BANG it worked for me. What happens when you run the script? – TheMonkeyKing – 2017-06-03T03:50:17.720

@BANG you may also want to consider accepting this answer by clicking the gray checkmark since it answers the question =) – TheMonkeyKing – 2017-06-03T04:13:07.040

I certainly will accept your answer if I can get it to work. When I run it (in the Python console) with my directory entered exactly as it is in my comment above (with single quotes included) it seemingly does nothing at all. Do I need to include the single quotes? Do I need to enter the little < symbol as you have in '<your-directory'? I had also read elsewhere that in Python the backslashes need to be made into forward slashes. I know nothing at all about Python. I had to install it to get a color swatch converter to work. Thanks for your answers – BANG – 2017-06-03T04:28:49.007

Yes you need the quotes and no you don't use the <. Honestly dude, I cant teach you python. I wrote the whole script for you. – TheMonkeyKing – 2017-06-03T04:31:57.097

Try cding to the directory with your files and running the script with ./ as your directory. Your probably spelling the directory wrong. – TheMonkeyKing – 2017-06-03T04:39:24.463

LOL thanks 'dude' I found a working solution to this issue anyhow link I don't expect you to teach me Python man. I told you your script didn't work. If you don't use the "<" then why was it there? Anyway I tried it both ways and it did nothing. Spelling? You're - - - I did not spell the directory path wrong - I have right-click 'Copy Path' function. Thanks for the effort.

– BANG – 2017-06-03T07:26:57.643