GIMP Script-Fu First Dan. First greeting

Our first Hello World in GIMP!

Our goal is to draw a greeting on an image in GIMP. But in order to display something on an image, we must have this image. We can create it manually. Simply using GIMP, or create it directly from the Script-fu plugin.

Let's teach the plugin to create images by defining the image creation function. What you need to know is that the image consists of layers – layer, in our new image there will initially be only one layer. The image will be created and filled with the CURRENT background color set in Gimp, but we could also set the background color of the created image.

(define (create-1-layer-img w h)
   (let* ((i1  (car (gimp-image-new w h RGB)))
         (l1  (car (gimp-layer-new i1 w h
                                RGB "layer 1" 100 LAYER-MODE-NORMAL-LEGACY))))
      (gimp-image-undo-disable i1)
      (gimp-image-add-layer i1 l1 0)
      ;;(gimp-palette-set-background '(0 127 50))
      (gimp-edit-fill l1 FILL-BACKGROUND)
      (gimp-display-new i1)
      (gimp-image-undo-enable  i1)
      i1))

Yes, I forgot to say, this code is suitable for version 2.10. And GIMP, although slowly, is constantly changing, and I worked in two versions, so you can compare

code for version 2.6:
(define (create-1-layer-img w h)
   (let* ((i1  (car (gimp-image-new w h RGB)))
         (l1  (car (gimp-layer-new i1 w h
                                RGB "layer 1" 100 NORMAL))))
      (gimp-image-undo-disable i1)
      (gimp-image-add-layer i1 l1 0)
      ;;(gimp-palette-set-background '(0 127 50))
      (gimp-edit-fill l1 BG-IMAGE-FILL)
      (gimp-display-new i1)
      (gimp-image-undo-enable  i1)
      i1))

The changes are cosmetic, in the names of constants, but it is precisely such small changes that can make the code inoperable when running it in different versions of Gimp.

Let's use this function and create an image:

(define img (create-1-layer-img 640 480))
;;можно получить img или ошибку:
;;Error: eval: unbound variable: LAYER-MODE-NORMAL-LEGACY
;;если применить код для версии 2.10 в gimp версии 2.6

The img variable will store handle(handle), which can be translated as pensomething that we can hold onto and manipulate this object, which is a kind of numeric identifier that by passing it back to Gimp using Gimp functions, we can work with the internal Gimp object. The types of these objects can be completely different: an image, a layer, or some other type.

We can also create an image using Gimp or even load it from a file. But in order to work with it, we need to know the handle of this image. You can get a list of all images using the function:

(gimp-image-list)
;;(1 #(1))

(define img 1)
;;img

and simply assign a known numeric identifier to the img variable and work with it in the same way as with a handle.

Next, we need to decide which font to display the text in. A list of all known fonts to Gimp can be obtained using the function:

;;посмотреть список фонтов присутствующих в системе
(gimp-fonts-get-list ".*")
;;очень длинный вывод
(gimp-fonts-get-list "Sans")
;;у меня список из 218 элементов в Линукс и 23 в Видовс.

The function searches for a font name pattern and returns all fonts whose names match the pattern, regular expression. From this list we can select the font with which we will draw our text.

(gimp-text-layer-new img "Привет Мир!" "DejaVu Sans" 1 1)
;;(3)

By entering this command into the script-fu console, we unfortunately will not see the added text, but it will again return a certain handle (like a list) of the created text layer. Using it you can get some information about the created text layer. Unfortunately, until the layer is added to the image, all functions for working with a text layer throw an error. Therefore, we will add a layer with text using the function:

(define (insert-text1 i str font size unit)
  (let ((l (car (gimp-text-layer-new i str font size unit))))
    (gimp-image-add-layer i l 0)
	l))
	
(insert-text1 img "Привет Мир!" "DejaVu Sans" 1 1)
;;4

Despite the units, or rather thanks to the last unit (denoting text output in certain units), in Linux the text turned out to be gigantic, in Windows it turned out to be quite normal. But we'll try a second approach. And yes, if the image is “damaged”, an unsuccessful function in GIMP can easily be rolled back to the previous version of the image using “History of Action”this is a list of images, select the image that you consider normal, we have the only empty image so far, select it.

(insert-text1 img "Привет Мир!" "DejaVu Sans" 100 0)
;;6

The last 0 in the function call denotes the dimension in pixels, and 100 denotes the conditional height of the text. The resulting layer approximately corresponds to this value.

"Hello World!"

“Hello World!”

You can get a lot of interesting information from the text layer, remember the handle we received for this layer 6, and it is this that we use in the functions below, if you have a different handle, use it:

(gimp-text-layer-get-text 6)
;;("Привет Мир!")
(gimp-text-layer-get-language 6)
;;("ru_RU")
(gimp-text-layer-get-font-size 6)
;;(100.0 0)
(gimp-text-layer-get-font 6)
;;("DejaVu Sans")
(gimp-text-layer-get-base-direction 6)
;;(0)
(gimp-drawable-height 6)
(gimp-drawable-width  6)
;;(117)(668)

The results may differ, for example, because in Windows I do not have such a font, but the system does not swear, but silently tries to select some kind of font close to the given one.

But what is equally important, you can set this information, that is, change the text layer itself.

(gimp-text-layer-set-color 6 '(255 255 0))
(#t)
(gimp-text-layer-set-font 6  "URW Gothic Oblique")
(gimp-text-layer-set-font 6   "Terminus")
(#t)
(gimp-text-layer-set-text 6   "Хай, Бро!\nКак живёшь?")
(#t)
(gimp-text-layer-set-base-direction 6  4);;в 2.6 направления 4 нет, есть 0 или 1.
(#t)

And here is the result:

"We've done it!"

“We're done!”

Thus, you can see that Gimp offers a very rich set of functions for displaying text on an image. And just as important, the layer created by the function gimp-text-layer-new is not just a pixel map, but an object that dynamically changes its appearance depending on the parameters set for it.

How to remember the results of our work? You need to merge the layers into one and give GIMP the save command:

(gimp-image-flatten img)
;;(7)
(gimp-file-save 1 img (car (gimp-image-active-drawable img))
                "D:\\WORK\\gimp\\test-hello1.png" "test-hello1.png" ) 
;;(#t)

Let it be indicated for Windows, for Linkus everything is the same, but with a regular slash, of course, without a disk. And yes, why do I always CARwhy? Because many procedures, such as gimp-image-active-drawable return a list, although it would seem, why a list? there can only be one active layer, but it returns a list, and we take 1 element from it. That's why CAR.

And that's all for now.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *