LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Changing colour of images with Gimp or Paint Shop Pro (http://www.linuxquestions.org/questions/programming-9/changing-colour-of-images-with-gimp-or-paint-shop-pro-530936/)

rblampain 02-21-2007 01:16 AM

Changing colour of images with Gimp or Paint Shop Pro
 
I have 8 small black images (arrows) and I would like to have these small arrows in other colours, doing it by hand would require 1152 times changing the colour of these arrows and saving the new file.

Using GIMP or Paint Shop Pro (on a windows 95 machine) is there a way to automate this job?

I know nothing about GIMP so I would need clear instructions, I have a very basic knowledge of Paint Shop Pro.

Thank you for your help.

colucix 02-21-2007 02:57 AM

Quote:

Originally Posted by rblampain
Using GIMP or Paint Shop Pro (on a windows 95 machine) is there a way to automate this job?

The answer to your question is script-fu! You can automate almost every GIMP action by writing a little script-fu script. A brief and good tutorial is the Mike Terry's black belt school of script-fu. Anyway I can give you a hand if you let me download an example image from somewhere and tell exactly the result you want to obtain.

rblampain 02-21-2007 11:45 PM

Thank you very much for your reply and your very kind offer to help.

I will investigate how to make the examples available to you, I have never done it before.

I will also investigate your link.

What I want to do is simple (I think), I have 8 images of arrows 20 pixels by something like 12 pixels coloured in black, up arrow, down arrow, 45 degrees arrows etc and I like to get a copy of each in every "safe" colour (but not required in the light colours).

So, basically, change the colour of the original image and save as a new file. The background of the image can be set to a light colour that could blend with any other light colour, it's only a few pixels in the area of the rectangle that is not part of the arrow.

Just for information:
I have a number of charts included in a site that contains a lot of text, these charts are used to introduce colours to release the eye strain of the reader, (this is a not-for-profit organisation) and I have found the black arrows in the charts ruin that purpose.

These charts HTML scripts are created by a "BASIC" program and I'd like these arrows to have the same colour than the HTML "table" border.

rblampain 02-22-2007 02:56 AM

I have put these arrows in:
www.1espero.net
navigate to the directory /www/www/colucix
username colucix
password xiculoc

Thank you.

colucix 02-22-2007 03:21 AM

Got them! Thank you. Just a note: the server did not asked for any user/passwd! Just a question: do you have a list of the colors in which you want the arrows to be changed (e.g. a list of the Red Green Blue values for each color)?

PS: Just a curiosity: I see you live in Australia and posted the last message half an hour ago. But what time is it now, there? Here in Italy is mid-morning now (10:30 AM)!

rblampain 02-22-2007 07:54 AM

I was aware the files are available without password.

The Linuxquestions site says my first answer was posted at 4.56 in the afternoon and you answered at 5.21, about half an hour later. Now, it is 22:54

Here are the color codes:
000000 000033 000066 000099 0000CC 0000FF
003300 003333 003366 003399 0033CC 0033FF
006600 006633 006666 006699 0066CC 0066FF
009900 009933 009966 009999 0099CC 0099FF
330000 330033 330066 330099 3300CC 3300FF
333300 333333 333366 333399 3333CC 3333FF
336600 336633 336666 336699 3366CC 3366FF
339900 339933 339966 339999 3399CC 3399FF
660000 660033 660066 660099 6600CC 6600FF
663300 663333 663366 663399 6633CC 6633FF
666600 666633 666666 666699 6666CC 6666FF
669900 669933 669966 669999 6699CC 6699FF
990000 990033 990066 990099 9900CC 9900FF
993300 993333 993366 993399 9933CC 9933FF
996600 996633 996666 996699 9966CC 9966FF
999900 999933 999966 999999 9999CC 9999FF
CC0000 CC0033 CC0066 CC0099 CC00CC CC00FF
CC3300 CC3333 CC3366 CC3399 CC33CC CC33FF
CC6600 CC6633 CC6666 CC6699 CC66CC CC66FF
CC9900 CC9933 CC9966 CC9999 CC99CC CC99FF
FF0000 FF0033 FF0066 FF0099 FF00CC FF00FF
FF3300 FF3333 FF3366 FF3399 FF33CC FF33FF
FF6600 FF6633 FF6666 FF6699 FF66CC FF66FF
FF9900 FF9933 FF9966 FF9999 FF99CC FF99FF

It is the 216 "safe" colours except for those light colours with code "cc" or "ff" in the green range (144 clolours in total).

Western Australia "tested" day light saving for the first time and I noticed the computer time does not match the correction made against the UTC

matthewg42 02-22-2007 08:52 AM

I wrote a script-fu program to change the format of a large number of files (SVG to PNG) using The Gimp. It could be adapted for your purpose. In case you're interested, you can find it here.

All you need to do is to find the script-fu commands to do the colour change.

rblampain 02-22-2007 11:13 PM

Thank you very much for that, I will follow it.

colucix 02-23-2007 09:27 AM

Ok. I have done a little script-fu for you! I hope this can be just a way to introduce you to the Script-Fu Mastery! ;)

First of all I took the list of colors and put them in a file with only one color per line: necessary condition to make the script work! I just pasted your list in a textfile and issued
Code:

gawk '{for (i = 1; i <= NF; i++) printf "%s\n",$i}' textfile > colors
Then I saved the following script-fu in $HOME/.gimp-2.2/scripts/script-fu-change-color.scm. Depending on the Gimp version, you will have a similar path in your home directory.
Code:

(define (script-fu-change-color)
; --------------------------------------------------------------
;  User defined parameters:
;
;      indir = directory where original arrows are stored
;      oudir = directory where saved image will be stored
;      colors = ascii file with color specifications
;      arrows = list with names of arrows
;
;  Note: put a slash at the end of indir and oudir!
; --------------------------------------------------------------
(set! indir "/home/colucix/test/prova/script-fu/")
(set! oudir "/home/colucix/test/prova/script-fu/out/")
(set! colors "/home/colucix/test/prova/script-fu/colors")
(set! arrows '("da" "la" "lda" "lua" "ra" "rda" "rua" "ua"))
; --------------------------------------------------------------
;  Initialize a counter
; --------------------------------------------------------------
(set! count 1000)
; --------------------------------------------------------------
;  Read the file with color specifications: each color will be
;  a single element of the list inList
; --------------------------------------------------------------
(set! inFile (fopen colors "r"))
(set! inAll  (fread 10000 inFile))
(set! inList (strbreakup inAll "\n"))
(fclose inFile)
; --------------------------------------------------------------
;  Compute the length of the first element in the list
; --------------------------------------------------------------
(set! lenHexStr (string-length (car inList)))
; --------------------------------------------------------------
;  Loop over colors: condition is "loop until the length of
;  the color string is 0"
; --------------------------------------------------------------
(while (not (= 0 lenHexStr))

    (set! count (+ count 1))
    ; ----------------------------------------------------------
    ;  Set the current color
    ; ----------------------------------------------------------
    (set! HexStr (car inList))
    ; ----------------------------------------------------------
    ;  Reset the arrows list
    ; ----------------------------------------------------------
    (set! arrow arrows)
   
    ; ----------------------------------------------------------
    ;  Loop over arrows: condition is "loop until the arrows
    ;  list is empty"
    ; ----------------------------------------------------------
    (while (not (null? arrow))

      (set! infile (string-append indir (car arrow) ".png"))
      ; -------------------------------------------------------
      ;  If the line below is un-commented, the output files
      ;  will be saved by color specification (e.g. 339933_da.png)
      ; -------------------------------------------------------
      (set! oufile (string-append oudir HexStr "_" (car arrow) ".png"))
      ; -------------------------------------------------------
      ;  If the line below is un-commented, the output files
      ;  will be saved by progressive number (e.g. 044_da.png)
      ; -------------------------------------------------------
      ;(set! oufile (string-append oudir (substring (number->string count) 1 4) "_" (car arrow) ".png"))
   
      (set! theImage (car (file-png-load 1 infile infile)))
      (set! colormap (car (cdr (gimp-image-get-colormap theImage))))

      (set! Triple (hexstr->bytes HexStr))
      (aset colormap 0 (aref Triple 0))
      (aset colormap 1 (aref Triple 1))
      (aset colormap 2 (aref Triple 2))

      (gimp-image-set-colormap theImage 6 colormap)
      (set! theDrawable (car (gimp-image-get-active-layer theImage)))
      (file-png-save 1 theImage theDrawable oufile oufile 0 9 0 0 0 1 1)
   
      (gimp-image-delete theImage)
     
      ; -------------------------------------------------------
      ;  Shift to the next arrow
      ; -------------------------------------------------------
      (set! arrow (cdr arrow))
   
    )

    ; ----------------------------------------------------------
    ;  Shift to the next color
    ; ----------------------------------------------------------
    (set! inList (cdr inList))
    (set! lenHexStr (string-length (car inList)))

)

; --------------------------------------------------------------
;  Done!
; --------------------------------------------------------------
(gimp-quit 0)

)

(script-fu-register
"script-fu-change-color"
"<Toolbox>/Xtns/Script-Fu/Test/change-color"
"This procedure change colormap of indexed images (not generic)"
"colucix"
"Copyright 2007, colucix"
"February 23, 2007"
""
)

I put some comments to let you understand this code. BTW, feel free to ask for any explanation! At this point you have to change only the very first lines under "User defined parameters" according to the pathnames on your machine. You will eventually change the last part under "script-fu-register", too.

At this point you are ready to run. The simply way to do is to run Gimp in batch mode (without GUI and any other kind of interaction)
Code:

gimp --no-data --no-splash -i --console-messages -b '(script-fu-change-color)'
You can also run the script-fu in interactive mode, that is launch Gimp, under Xtns -> Script-Fu -> Test launch change-color. Note that the position of the script inside the menu is customizable from the registry section at the end of the script
Code:

"<Toolbox>/Xtns/Script-Fu/Test/change-color"
Anyway, since the code above contains a quit statement, the Gimp will close at the end of the execution.

Just one more hint: to test your own script-fu, you will find very useful the interactive script-fu console (under Xtns -> Script-Fu) and the browser of all the available script-fu functions (accessible from the script-fu console itself).

An useful link is SIOD: Scheme in One Defun which contains a list of all the Scheme functions (the old-style programming language used by Script-Fu).

If you enjoy all this, your are on your way to the Script-Fu Mastery! :jawa:

rblampain 02-23-2007 09:48 PM

Thank you very much colucix, this is fantastic.
I will use your script this week end.

colucix 02-24-2007 01:16 AM

You're welcome! Let me know about the results. I tested it and it took about 20 seconds to run! Bye :)

rblampain 02-24-2007 01:25 AM

I tried it and I receive an error:
ERROR: end of file inside list.

What I did:
I have "cut and paste" the script.
I changed the path and filenames to reflect my setup.
I did not have GAWK installed so I edited the coulour list with "gedit" (1 entry per line)
Saved the script as suggested then run script in interactive mode:
Xtns >> Script-fu >> Refresh Scipts
(note: at this stage the script is not listed as an option but its full path and name appears in the error message, so I assumed it was found correctly)

I tried again after installing GAWK and running the command as you suggested (I am completely new to GAWK also)

As I understand it, the error is about my list of colours.

When I look at this list with "gedit", the list created by gawk is a continuous string containing each colour in six positions each followed by "/n", the list created by gedit is one string for each colour followed by a carriage return.

But both give the same error.

Should I provide the script as I modified it?

colucix 02-24-2007 02:16 AM

Quote:

Originally Posted by rblampain
When I look at this list with "gedit", the list created by gawk is a continuous string containing each colour in six positions each followed by "/n"

This is not correct, since the newline control character should be \n (backslash n). Check the syntax of the gawk command... maybe you have typed /n instead of \n.
Quote:

the list created by gedit is one string for each colour followed by a carriage return.
In the script-fu I based the breakup of the file content into elements of the list on the \n presence:
Code:

(set! inList (strbreakup inAll "\n"))
If you mean that Gedit put a \r (carriage return) or a \r\n sequence, you can try to substitute the correct one in the statement above, e.g.
Code:

(set! inList (strbreakup inAll "\r"))  ;or
(set! inList (strbreakup inAll "\r\n"))


rblampain 02-25-2007 09:31 AM

I typed "/" instead of "\" but this is not what creates the error. I got the same result every time and trying all sorts of things.

Finally, I made the ascii file containing the colour specifications unavailable to the script and the result is exactly the same.

It seems the error is about something else. I also run Gimp 2.2 and the set up is as you predicted.

I noticed the line "(set! arrows ..." contains only one single quote instead of the usual pair but adding one between the two end parentheses gives another error and removing the single quote has no apparent effect, the same original error appears.

I have tried with color specifications files of different formats but all containing 1 colour per line and different strbreakup as you suggested.

My feeling is that it may be very difficult to find what the problem is over the net and it may be easier if, perhaps, I can somehow download the resulting arrows that you obtained from your test and look at this later when I have learned a bit more about Script-fu.

I think that you have been generous enough in providing the script, the error may be due to some differences in settings or something of that nature.

As you recommended I only changed 2 lines in the script: "indir" and "oudir", without missing the slash at the end, I also called the colour specification file "colors" as in the original script, so the only problems I could have found was the original arrows not been found or a write permission on the output directory.

I welcome any suggestions anyway.

Thank you again for your help.

colucix 02-25-2007 05:17 PM

Quote:

Originally Posted by rblampain
I noticed the line "(set! arrows ..." contains only one single quote instead of the usual pair

Indeed, it is correct. The syntax '( ) is the way to assign elements to lists.
Quote:

My feeling is that it may be very difficult to find what the problem is over the net
Totally agree. Furthermore the error messages from script-fu are often obscure. BTW there are two ways for debugging:
1) by means of the script-fu console, typing one command at a time (skipping the while loops) and seeing the results of the command itself at once.
2) Adding a statement which will give a message output in different locations of the script, to discover the point of failure, e.g.
Code:

(gimp-message "I am here")
Quote:

and it may be easier if, perhaps, I can somehow download the resulting arrows that you obtained from your test and look at this later when I have learned a bit more about Script-fu.
No problem for this. Just send me an e-mail (see my profile) so I can replay and send the pictures as attachment. Obviously, if we find the script-fu arcane, we will post the solution here in the thread!


All times are GMT -5. The time now is 08:45 PM.