さあ訓練を続けましょう。 こんどはスクリプトに機能を加えます。
In the previous lesson, we created an empty function and registered it with GIMP. In this lesson, we want to provide functionality to our script — we want to create a new image, add the user's text to it and resize the image to fit the text exactly.
Once you know how to set variables, define functions and access list members, the rest is all downhill — all you need to do is familiarize yourself with the functions available in GIMP's procedural database and call those functions directly. Open the 「プロシージャーブラウザー」.
新しい画像を作成するところから始めます。 theImage
という新しい変数を作り、 gimp-image-new
という GIMP の組み込み機能を実行して得た値を代入するのです。
As you can see from the DB Browser, the function
gimp-image-new
takes three parameters — the
image's width, height and the type of image. Because we'll
later resize the image to fit the text, we'll make a 10×10 pixels
RGB
image. We'll store the image's width and sizes in some
variables, too, as we'll refer to and manipulate them later in
the script.
(define (script-fu-text-box inText inFont inFontSize inTextColor) (let* ( ; define our local variables ; create a new image: (新しい画像を作る局所変数を定義する) (theImageWidth 10) (theImageHeight 10) (theImage (car (gimp-image-new theImageWidth theImageHeight RGB ) ) ) (theText) ;a declaration for the text (テキストの定義) ;we create later (あとで作成するつもり)
![]() |
注記 |
---|---|
ここでは RGB 画像を示すために |
You should also notice that we took the head of the result of the function call. This may seem strange, because the database explicitly tells us that it returns only one value — the ID of the newly created image. However, all GIMP functions return a list, even if there is only one element in the list, so we need to get the head of the list.
画像ができたのでこんどはレイヤーを追加する必要があります。 gimp-layer-new
関数を呼び出して先程作った画像の ID 番号を渡し、 レイヤーを生成させます。 (ここからの説明では関数の中身を全部ここに掲載する代わりに、 追加される行だけを載せます。 最終的なスクリプトの全貌はこの演習の 最後 でご覧いただけます。) これで使う予定の局所変数はすべて宣言し終えましたので、 変数宣言を示す括弧もここで閉じることにします。
;create a new layer for the image: (theLayer (car (gimp-layer-new theImage "layer 1" theImageWidth theImageHeight RGB-IMAGE 100 LAYER-MODE-NORMAL ) ) ) ) ;end of our local variables
新しいレイヤーができたので画像に加えなくてはなりません。
(gimp-image-insert-layer theImage theLayer 0 0)
さてここまでにスクリプトが実らせた果実の熟れ具合を見て楽しむために、 つぎの行を追加して新しい空の画像を表示させましょう。
(gimp-display-new theImage)
Save your work, restart GIMP, run the script and a new image should pop up. It will probably contain garbage (random colors), because we haven't erased it. We'll get to that in a second.
先に進みたいので先程画像を表示させるために追加した行を削除してください。 (もしくはその行の最初にセミコロン「;
」を書き加えてコメントアウトしてください。)
画像にテキストを加える前に、 画像の背景色と描画色を設定してテキストが読み取れる色づかいに設定しなくてはなりません。 これには gimp-context-set-background
関数と gimp-context-set-foreground
関数を使いましょう。
(gimp-context-set-background '(255 255 255) ) (gimp-context-set-foreground inTextColor)
色がうまく設定できたら、 描画対象を塗りつぶす方法で画像に溢れているゴミを一掃しましょう。
(gimp-drawable-fill theLayer FILL-BACKGROUND)
画像が清められたらつぎはテキストの追加にとりかかります。
(set! theText (car (gimp-text-font theImage theLayer 0 0 inText 0 TRUE inFontSize inFont) ) )
長い関数呼び出しになっていますが、 プロシージャーデータベースのブラウザーでそれぞれのパラメーターについて見てみればどの項目もありきたりなものばかりです。 要するにここでは新しいテキストレイヤーを作成して変数 theText
に代入しているのです。
さてテキストができましたので、 つぎはその幅と高さをつかめば画像とそのレイヤーの寸法をテキストの大きさに合わせられます。
(set! theImageWidth (car (gimp-drawable-get-width theText) ) ) (set! theImageHeight (car (gimp-drawable-get-height theText) ) ) (gimp-image-resize theImage theImageWidth theImageHeight 0 0) (gimp-layer-resize theLayer theImageWidth theImageHeight 0 0)
私もそうでしたが、 描画対象というのはレイヤーとどんな関係にあたるものなのか疑問に思われたことでしょう。 両者の違いについては、 レイヤーやチャンネルやレイヤーマスクや選択範囲などなどあらゆる描画可能なものを描画対象と呼ぶということで説明できます。 レイヤーは特定の描画対象だといえます。 ほとんどの場合その区別は重要ではありません。
画像の準備ができましたので、 また途中経過の表示の行を追加してもよいでしょう。
(gimp-display-new theImage)
Save your work, restart GIMP and give your first script a run!
If you try to close the image created without first saving the file, GIMP will ask you if you want to save your work before you close the image. It asks this because the image is marked as dirty, or unsaved. In the case of our script, this is a nuisance for the times when we simply give it a test run and don't add or change anything in the resulting image — that is, our work is easily reproducible in such a simple script, so it makes sense to get rid of this dirty flag.
この目的のためには「手付き」フラグの解消を画像の表示後に行なえばよろしい。
(gimp-image-clean-all theImage)
この処理は手付きの回数を 0 にするので結果的に画像が「手付きなし」となります。
このような行を挿し挟むか否かは個人的な好みに委ねられます。 私なら新たな画像を作成するスクリプトの、 今回のようにありきたりの結果が出る場合に限ります。 しかしスクリプトがとても複雑だったり、 既存の画像を加工するものならば、 おそらくこういった機能を使いたい理由がありません。