Please help! How replicate the function of GIMP's "colorize"

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?".
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

As promised, here is my vbscript code to convert all images in a folder to a specific color. This will create a copy of each image in purple, red, green, blue and orange and place the images in folders by those same color names. This script was only possible thanks to Fred's help! So thanks Fred, I can now batch convert images instead of spending hours developing carpal tunnel via Gimp. Just place this script code in a text file in the same directory as your images. Name it anything you want, just give it a VBS extension. Double click to launch. You will see a whole bunch of CMD prompts open and close as images are created. Just let it finish processing before clicking in any other windows.

Code: Select all

purple = "270,75%%,50%%"
green = "137,75%%,50%%"
blue = "154,75%%,50%%"
red = "0,100%%,52%%"
orange = "106,75%%,50%%"

lightval = 52

If lightval = 0 Then
	fillcolor="black"
else
	fillcolor="white"
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("Wscript.Shell")
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

If Not objFSO.FolderExists(sCurPath & "\purple") Then objFSO.CreateFolder(sCurPath & "\purple")
If Not objFSO.FolderExists(sCurPath & "\green") Then objFSO.CreateFolder(sCurPath & "\green")
If Not objFSO.FolderExists(sCurPath & "\blue") Then objFSO.CreateFolder(sCurPath & "\blue")
If Not objFSO.FolderExists(sCurPath & "\red") Then objFSO.CreateFolder(sCurPath & "\red")
If Not objFSO.FolderExists(sCurPath & "\orange") Then objFSO.CreateFolder(sCurPath & "\orange")


Set objFolder = objFSO.GetFolder(sCurPath)
For Each objFile In objFolder.Files
	If Right(objFile.Name,3) = "png" Then
		WSHSHell.Run "cmd.exe /c convert " & objFile.Name & " -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"& Chr(34) & "hsl(" & purple & ")"& Chr(34) & " xc:white +append -filter cubic -resize "& Chr(34) & "256x1!"& Chr(34) & " ) -clut -fill " & fillcolor & "  -colorize 0 " & sCurPath & "\purple\" & objFile.Name
		WSHSHell.Run "cmd.exe /c convert " & objFile.Name & " -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"& Chr(34) & "hsl(" & green & ")"& Chr(34) & " xc:white +append -filter cubic -resize "& Chr(34) & "256x1!"& Chr(34) & " ) -clut -fill " & fillcolor & "  -colorize 0 " & sCurPath & "\green\" & objFile.Name
		WSHSHell.Run "cmd.exe /c convert " & objFile.Name & " -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"& Chr(34) & "hsl(" & blue & ")"& Chr(34) & " xc:white +append -filter cubic -resize "& Chr(34) & "256x1!"& Chr(34) & " ) -clut -fill " & fillcolor & "  -colorize 0 " & sCurPath & "\blue\" & objFile.Name
		WSHSHell.Run "cmd.exe /c convert " & objFile.Name & " -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"& Chr(34) & "hsl(" & red & ")"& Chr(34) & " xc:white +append -filter cubic -resize "& Chr(34) & "256x1!"& Chr(34) & " ) -clut -fill " & fillcolor & "  -colorize 0 " & sCurPath & "\red\" & objFile.Name
		WSHSHell.Run "cmd.exe /c convert " & objFile.Name & " -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"& Chr(34) & "hsl(" & orange & ")"& Chr(34) & " xc:white +append -filter cubic -resize "& Chr(34) & "256x1!"& Chr(34) & " ) -clut -fill " & fillcolor & "  -colorize 0 " & sCurPath & "\orange\" & objFile.Name
	End If
Next
Last edited by markdmac on 2012-11-20T16:29:53-07:00, edited 1 time in total.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Thanks Fred. Here is a sample of before and after. The left side of the image in blue is the stock android and the right side in red is the after affect.
http://www.mediafire.com/view/?hd9rr5jczdqxmnn

Ideally I would love to be able to pick the specific color using standard RGB Hex colors. I can easily add any single IM command into my script and change up specific values for variables, it is figuring out the base IM command that throws me. I see that there are two books available regarding IM, I may pick one up, regretfully neither focuses on Windows but they might help me to understand the vast possibilities. I still can't believe that this one program can do so much.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Well, I am hoping you can suggest something better Fred, the purple, green and blue look awesome but I can't seem to nail down a bright enough red and orange color.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

From those simple images, I would convert turn off the alpha, convert to non-linear grayscale, colorize with whatever color you want (or if the underlying color is not constant, use +level-colors black,somecolor or +level-colors somecolor,white), then turn alpha back on. I think you are making the process more complex than it needs. Try

convert sample.png -alpha off -set colorspace RGB -colorspace gray -fill orange -colorize 100 -alpha on sample_tmp1.png

Just change the orange to any other color name, hex color or rgb(..) color. See http://www.imagemagick.org/script/color.php. There is even a color convert gui link near the top to test various colors.

Also see
http://www.imagemagick.org/Usage/color_ ... vel-colors
http://www.imagemagick.org/Usage/color_mods/#colorize
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Regretfully all that I got from that last example was a full block of color. It filled everything.

The other command worked very well with the exception of red & orange, however I found it also messed with the color black which I can't allow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

The image I downloaded had an alpha channel. Is that what you had? My command should recolor your RGB channels and leave the alpha channel alone. So I got two orange rings fading with transparency. What version of IM are you using?

If black should be preserved, then try

convert sample.png -alpha off -set colorspace RGB -fill orange +opaque black -alpha on sample_tmp2.png

That will make everything but black into orange.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Afraid I got the same results. The entire image gets filled with color. Even my .9.png files which have the special black borders were fully filled in.

So frustrating, I know I am close to getting this working. As always, thanks for your suggestions.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Fred, just wondering if from the previous example that I had mostly success with:

convert zelda.png -set colorspace RGB -colorspace gray ( -size 1x1 xc:black xc:"hsl(56,100%%,52%%)" xc:white +append -filter cubic -resize "256x1!") -clut -fill white -colorize 0 zeldaTinted.png

Is there any way to add the statements for the +opaque black? I can make do with having to manually adjust the red and orange colors. Having my blue, green and purple generated is still a huge time saver for me.

Maybe you can suggest a way to get the levels right for red and orange so that the red is hex red f10834 and the orange is a hex orange ff6b00?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

markdmac wrote:Afraid I got the same results. The entire image gets filled with color. Even my .9.png files which have the special black borders were fully filled in.

So frustrating, I know I am close to getting this working. As always, thanks for your suggestions.

What version of IM are you using? If it is very old, then it may be before -alpha. Or there is a bug in that version with -alpha turning back on again.

I don't understand why my last command is not working for you. It worked fine for me. Is your black really black or near black.

try

convert sample.png -alpha off -set colorspace RGB -fuzz 1% -fill orange +opaque black -alpha on sample_tmp3.png

http://www.fmwconcepts.com/misc_tests/c ... e_tmp3.png

Change the fuzz value until you get it to work.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

I am using version 6.7.9-Q16, will check if that is the latest.

The black is a pure black, hex 000000. Android requires it to be that color. What I am finding is that the tint is changing it slightly, for example making it 0007b. This is only an issue for the .9 patch files. What I did is add a second command to replace the color 00070b with 000000. That seems to have done the trick, though I will test your latest suggestion as I prefer the cleaner one step process if it is possible.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

markdmac wrote:I am using version 6.7.9-Q16, will check if that is the latest.

The black is a pure black, hex 000000. Android requires it to be that color. What I am finding is that the tint is changing it slightly, for example making it 0007b. This is only an issue for the .9 patch files. What I did is add a second command to replace the color 00070b with 000000. That seems to have done the trick, though I will test your latest suggestion as I prefer the cleaner one step process if it is possible.
That version should be recent enough for -alpha. The current version is 6.8.0.5.

Be sure the specify hex colors with quotes and the # symbol. -fill "#ff0000" as red for example.
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Fred, I updated to version 6.8.0-Q16 and the command worked great. I tried it out with the specific hex colors I need and it worked perfectly. So once again, thank you for all your help.

If I may, I'd like to ask one follow up question.

After the +opaque black, is there a way to specify a second color to ignore? I want to be able to add #404040 to the list of colors not to touch. This only affects a small number of images that have a second color in them.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

markdmac wrote: After the +opaque black, is there a way to specify a second color to ignore? I want to be able to add #404040 to the list of colors not to touch. This only affects a small number of images that have a second color in them.
To do that you could create a mask of the colors you want to preserve. You would have to first save your alpha channel. Create the mask, then use the mask to recolor everything else and then add your alpha channel back.

You can also do it with the same kind of command above, so long as you have a known safe color that is not being used in the image or use transparent, but in the latter case you would have to save off the alpha channel, process and then add it back again.

Provide me an example image and I will show you. Too bad you are not using Linux, Mac or Windows/cygwin. My script, mapcolors, does that. http://www.fmwconcepts.com/imagemagick/ ... /index.php
markdmac
Posts: 20
Joined: 2012-10-31T09:52:40-07:00
Authentication code: 67789

Re: Please help! How replicate the function of GIMP's "color

Post by markdmac »

Fred, I did download and instal CygWin, but have no idea how to run your scripts within it.

I'd be most happy to take full advantage of that script if you can provide some direction there. With some testing, I might even be able to automate the process for all the images in a folder by combining VBscript and using the SendKeys method to send file names to your script.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Please help! How replicate the function of GIMP's "color

Post by fmw42 »

You probably do not need Cygwin. If you post a link to a more complex example I can probably give you the IM commands to process it.

Unfortunately, I do not use Windows and know nothing about it. Best I can do is refer you to http://www.imagemagick.org/Usage/windows/#cygwin. You will need to be sure that you have all the unix commands installed with Cygwin, such as: bc (often missing), expr, sed, awk, tr, sort, etc.

Perhaps some kind Windows/Cygwin user can offer more pointers.
Post Reply