Add text with script fu - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Add text with script fu (/Thread-Add-text-with-script-fu) |
Add text with script fu - carlbcx - 09-01-2019 Hello from France, I have been using Gimp for a long time, having been using GNU/Linux for 20 years. I'm working on a poster without date. I need, from this file, make 30 similar posters, just adding a different date to each one. Last year I did it by changing the date by hand. I wonder if it would not be possible with a Fu script. - Added slap - add the text field to the right place and the right size - choice of the police, size, color, alignment - adding text (if possible with a dialog box) - record new file ? I do not know fu scripts yet but I have programming basics. That does not put me off. Nevertheless, I just read a lot of pages and I found no example to add a simple text on an image. So if you could tell me if it's possible and give me some leads, it would be great. Thank you all in advance, Carl RE: Add text with script fu - rich2005 - 09-01-2019 It might be possible with script-fu or python-fu, an expert might look in soon. However, my first thought was not Gimp but ImageMagick. That should be in your linux repository. If I create a text file for each date, 01.poster 02.poster ....30.poster then for a single file Code: magick poster.jpg -font Futura-Bold.ttf -pointsize 24 -fill red -annotate +420+1030 @01.poster 01-poster.jpg writes the content of 01.poster text at the given offset. For 30 new images then something like a bash file Code: #!/bin/bash creates a new image for each text file and names them 01.poster.jpg 02.poster.jpg .... As a note I am using kubuntu 18.04 which comes with ImageMagick (IM) 6.9.7 That would use the convert in place of the magick command. For some reason convert throws up errors. My compiled IM 7.0.8 and magick works fine. If you go down this route and have problems ask on the IM forum https://www.imagemagick.org/discourse-server/ In the Users section. (edit: The not working convert problem is due to the IM6 security policy held in a policy.xml file. Disabled that and IM6 convert works fine ) RE: Add text with script fu - Ofnuts - 09-01-2019 Doable with script-fu, or python-fu, but this is really best done outside of Gimp with a shell script that uses ImageMagick. RE: Add text with script fu - carlbcx - 09-01-2019 Thank a lot for these answer. I didn't think about Imagemagick I use for other think. Is a good idea. I'll try that. 1. Learning basic string operations in Script-Fu - Gimphried - 10-15-2021 Hello Carlbcx, Carlbcx Wrote:I'm working on a poster without date. I need, from this file, make 30 similar posters, just adding a different date to each one. Chapter 1: Learning basic string operations in Script-Fu
Code: (string-append "C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate" DIR-SEPARATOR "template" ".png")
"C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate\\template.png" In Linux, adapt the following path "/home/Tool/Gimp/gimp-forum.net/WriteDate" to your needs:
Consider that the path-in parameter is the root folder of the project called WriteDate. path-in contains template.png that is to say the poster of reference. The goal is to generate the dated posters in the subfolder target. According to your operating system, you must create manually the target folder under path-in. path-in = C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate path-out = C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate\\target We generate the dated posters in the subfolder target in order to remove quickly its content before running again the script. Regards. Chapter 2: Defining and running your first Script-Fu function Define your first Script-Fu function called GenFileOut. let* introduces the initialization of local variables such as filePoster until file-out. As usual, copy in the clipboard the following code and paste it in the Script-Fu console: Code: (define (GenFileOut path-in template ext date counter) GenFileOut meaning that your definition is ok. The aligned parenthesis are well balanced. Run your first function. We split the full path of the poster of reference as several parameters in order to avoid scanning and parsing the path. Code: (GenFileOut "C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate" "template" ".png" "10/15/2021" 0) Code: filePoster: C:\Tool\Gimp\forum\gimp-forum.net\WriteDate\template.png If displayln is unknown, it is normally defined in C:\Program Files\GIMP 2\share\gimp\2.0\scripts\palette-export.scm by: Code: (define displayln (lambda (obj) (display obj) (display "\n"))) The if instruction is followed by the condition, "Then" part and "Else" part. In Pascal or Basic-like: Code: If counter < 10 then strZidx = "0" + strIdx Else strZidx = strIdx Code: (if (closure? GenFileOut)(display "GenFileOut is a known function")(display "GenFileOut is UNknown")) Code: (if (closure? 'rascalFiloute)(display "rascalFiloute is a known function")(display "rascalFiloute is UNknown")) Chapter 3: WriteDate from template.png Ofnuts Wrote:Doable with script-fu We are ready to experiment our new function WriteDate. Before running WriteDate, prepare with Gimp an empty square image 500 x 500 pixels of yellow color RGB ffff00 Gimp menu File> Export as "template.png" in your working folder "C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate". Code: (define (WriteDate path-in template ext date counter) WriteDate Code: (WriteDate "C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate" "template" ".png" "10/15/2021" 0) file-out: C:\Tool\Gimp\forum\gimp-forum.net\WriteDate\target\template00.png date: 10/15/2021 (#t) template00.png has been SUCCESSFULLY generated in the target folder. We recognize the yellow square of template.png but with the written date "10/15/2021"! Chapter 4: Basic iterator WriteDates We wish to call WriteDate for each date of the given list: '("10/15/2021" "10/11/2021") To get the first date: Code: (car '("10/15/2021" "10/11/2021")) To get the remaining dates in a list: Code: (cdr '("10/15/2021" "10/11/2021")) car and cdr are the basic extractors of a Script-Fu list. WriteDates is a basic iterator based on let loop that increments counter from idxStart = 0. Note that we save the Gimp context by gimp-context-push because we change the fore and background colors. We restore this context before the end by gimp-context-pop. Before running WriteDates, remove manually all previously generated *.png from the target folder. Code: ; Main iterator Let us generate two posters dated from the list of dates '("10/15/2021" "10/11/2021") Code: (WriteDates "C:\\Tool\\Gimp\\forum\\gimp-forum.net\\WriteDate" "template" ".png" '("10/15/2021" "10/11/2021") 0) WriteDates is running... file-out: C:\Tool\Gimp\forum\gimp-forum.net\WriteDate\target\template00.png date: 10/15/2021 file-out: C:\Tool\Gimp\forum\gimp-forum.net\WriteDate\target\template01.png date: 10/11/2021 2 posters have been SUCCESSFULLY generated! (#t) Take in consideration that, in the if that checks if the list of dates is not empty, the Then and Else parts have more than one statement. We need to encapsulated them in begin. If you read this thread until here, it will be a first step in the Script-Fu world of another era. Would you also share your experimentation with ImageMagick according to the rich2005's bash file? Regards. RE: 1. Learning basic string operations in Script-Fu - Ofnuts - 10-15-2021 (10-15-2021, 08:53 PM)Gimphried Wrote: Not really. In Windows it is either, but 1) the console (.BAT) only accepts \, and the APIs return \, but APIs also accept / in their inputs. This avoids a lot of counting pairs of \\ (and on an AZERTY keyboard, the / is a lot easier to enter than the \). RE: Add text with script fu - carlbcx - 09-04-2022 Hi Gimphried, Like every year, I go back to my poster story. I actually didn't see your long answer last year. So sorry for the delay and a big thank you for that! And so I'll get right to it... Best regard RE: Add text with script fu - carlbcx - 09-04-2022 I test Imagemagick to but finaly I made with a script-fu and Gimphried help (thanks again). Because I need reverse date format at begin of each file this is my final script. So I change just size, font, color, position and output file name (remove the counter). Perfect for me with an img2pdf script after to have both image and pdf files. Code: (define (WriteDate path-in template ext date counter) |