2022年5月2日月曜日

GIMP 2.99 Python 寄り道

少し気になる 記述 を見掛けました。
GIMP 2.99 絡みの Python Script に関連したもの です。
おおっ!、 試して見るか! ですね。
でも、 其れが難儀する事に。


見付けたもの は 以下のリンクのものです。
開発者さん同士 の 遣り取り に含まれていた Python に纏わるもの。

そして、本投稿で試したかった事は、
OS Windows で、Console (Command Prompt cmd.exe) から、
環境変数を使って、Python Code を動かす事。 *1.

https://gitlab.gnome.org/GNOME/gimp/-/issues/7376#note_1429331

Anyway here is me testing:

中略

It loads the file in.png, then exports it as out.jpg and it exits successfully. Only thing is indeed that right now, the process is left hanging so I have to break it (e.g. with ctrl-c on Linux).
The stdin variant also works fine, where I would basically write the code in a file bla.py and pipe it to GIMP's standard input, which makes much nicer and easier to read code:

Translate by Google

ファイルin.pngをロードし、それをout.jpgとしてエクスポートすると、正常に終了します。 唯一のことは、現在、プロセスがハングしたままになっているため、それを中断する必要があるということです(たとえば、Linuxのctrl-cを使用)。
stdinバリアントも正常に機能します。ここでは、基本的にファイルbla.pyにコードを記述し、それをGIMPの標準入力にパイプします。これにより、コードがはるかに読みやすくなります。

さて、コードの引用

gimp-console-2.99 --batch-interpreter python-fu-eval -b "import gi;from gi.repository import Gimp;from gi.repository import GObject;file=Gio.file_new_for_path('/home/jehan/in.png');r=Gimp.get_pdb().run_procedure('file-png-load', [GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE),GObject.Value(Gio.File, file)]);file=Gio.file_new_for_path('/home/jehan/out.jpg');img=r.index(1);layers=img.get_layers();Gimp.get_pdb().run_procedure('file-jpeg-save', [GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE),GObject.Value(Gimp.Image, img), GObject.Value(GObject.TYPE_INT, len(layers)),GObject.Value(Gimp.ObjectArray, Gimp.ObjectArray.new(Gimp.Drawable, layers, False)), GObject.Value(Gio.File, file)]);"

更に踏み込んで Python で書かれたコード部分を抜粋 ; で改行
(後日、改訂されたコードが載りますが、此れは 最初のもの 。)

import gi;
from gi.repository import Gimp;
from gi.repository import GObject;
file=Gio.file_new_for_path('/home/jehan/in.png');
r=Gimp.get_pdb().run_procedure('file-png-load', [GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE),GObject.Value(Gio.File, file)]);
file=Gio.file_new_for_path('/home/jehan/out.jpg');
img=r.index(1);layers=img.get_layers();
Gimp.get_pdb().run_procedure('file-jpeg-save', [GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE),GObject.Value(Gimp.Image, img), GObject.Value(GObject.TYPE_INT, len(layers)),GObject.Value(Gimp.ObjectArray, Gimp.ObjectArray.new(Gimp.Drawable, layers, False)), GObject.Value(Gio.File, file)]);

で、
必要箇所を置換し、自分の環境 (Windows) で試して見るも、
無残な結果 ... 。

---------------
Error!
---------------

"%ProgramFiles%¥gimp 2.99¥bin¥gimp-console-2.99" --batch-interpreter python-fu-eval -b "import gi;from gi.repository import Gimp;from gi.repository import GObject;file=Gio.file_new_for_path(r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.png');inp=Gimp.get_pdb().run_procedure('gimp-file-load', [ Gimp.RunMode.INTERACTIVE, file]);"

で実行させると、上手くいくのですが ... 。
現状の 2.99.10 では Ctrl + C で逃げ無いと GIMP 動きっぱなし です。
(注: 将来の対応策は 最下 を参照)

大元のコードの様に、.png から .jpg を生成させようとしたのですが、
技量が足らず、落ちて仕舞います。
あ~、 情け無い 私 ... 。
なので、上のコードでは、.png を開く迄、です。  トホホ。
いいえ、実際には、ちゃんと、開けてはいません!。
Python Console で環境変数を置換して、実行させれば、分かります。

差し当っての Command Prompt の様子は こちら。


Microsoft Windows [Version 10.0.19044.1645]
(c) Microsoft Corporation. All rights reserved.

C:¥Users¥UserA>"%ProgramFiles%¥gimp 2.99¥bin¥gimp-console-2.99" --batch-interpreter python-fu-eval -b "import gi;from gi.repository import Gimp;from gi.repository import GObject;file=Gio.file_new_for_path(r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.png');inp=Gimp.get_pdb().run_procedure('gimp-file-load', [ Gimp.RunMode.INTERACTIVE, file]);"
This is a development version of GIMP.  Debug messages may appear here.

gimp_value_to_gp_param: GValue contains unsupported type 'GLocalFile'
batch command executed successfully
^C
C:¥Users¥UserA>


Python Console で格闘する事、暫し。
いいえ、実際には、暫し 処 では無く、 難航また難航 でした。
Dir(Gimp) やら、.get_arguments() やら、を総動員して、得られた結果が こちら。

  詳細は不明ですが、
  Linux と Windows とで、相違があるのか、
  大元のコードは pdb call で コケていました。
  (後日追加された見本コードでは 記述 が変更されていますが、其れは本稿執筆後に気付きました。)
  其れを Gimp Class から呼び出す形に変え、
  一件落着 です。

"%ProgramFiles%¥gimp 2.99¥bin¥gimp-console-2.99" --batch-interpreter python-fu-eval -b "import gi;from gi.repository import Gimp;from gi.repository import GObject;file=r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.png';img=Gimp.file_load(Gimp.RunMode.NONINTERACTIVE, Gio.file_new_for_path(file));file=r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.jpg';layers=img.get_layers();Gimp.file_save( Gimp.RunMode.NONINTERACTIVE, img, layers, Gio.file_new_for_path(file));"

Command Prompt の様子。


C:¥Users¥UserA>"%ProgramFiles%¥gimp 2.99¥bin¥gimp-console-2.99" --batch-interpreter python-fu-eval -b "import gi;from gi.repository import Gimp;from gi.repository import GObject;file=r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.png';img=Gimp.file_load(Gimp.RunMode.NONINTERACTIVE, Gio.file_new_for_path(file));file=r'%USERPROFILE%¥Documents¥Temp¥GimpBatchTest.jpg';layers=img.get_layers();Gimp.file_save( Gimp.RunMode.NONINTERACTIVE, img, layers, Gio.file_new_for_path(file));"
This is a development version of GIMP.  Debug messages may appear here.

gimp_plug_in_destroy_proxies: ERROR: GimpImage proxy with ID 1 was refed by plug-in, it MUST NOT do that!
gimp_plug_in_destroy_proxies: ERROR: GimpLayer proxy with ID 2 was refed by plug-in, it MUST NOT do that!
batch command executed successfully
^C
C:¥Users¥UserA>

此のコードですと、.png から .jpg が バッチで 生成されます。
やったぜぇ~!。  ;) ;)


  尚、本件の大元の着目点は、
  Console から動かす GIMP 2.99 が上手く終了処理し無い点 だと思います。
  幸い、新しい API --quit を新設する事で、対応が可能になる様です。



[2022/05/02] 投稿の趣旨を追記 *1.

0 件のコメント:

コメントを投稿