インストール方法
以下をコピペしてファイル名をadd_tategaki_text_save_xcf.pyにしてutf-8で保存後、GIMPのプラグインフォルダーに入れる。
GIMPのメニューの「レイヤー」→「add tategaki text & save xcf」をクリックすると動作する。
プラグインフォルダーはGIMPを起動し「編集」→「設定」→「フォルダー」→「プラグイン」で確認の事。
初回のフォント指定(MSゴシック)を変更するにはプラグイン下の方の行の"MS Gothic"のところを好きなフォント名に書き換えればよい。
フォント名はGIMPでテキストボックスを選択してツールオプションの好みのフォントを設定する時の名前をコピペすればよい
import random , os ,glob
from gimpfu import *
def tategaki_text_save_xcf(text_number , font , font_size ,tategaki ,already_open_image , folder_path):
if already_open_image == False:
folder_path=folder_path.decode("utf-8")
p = os.path.join(folder_path,"*.jpg")
for jpg in glob.glob(p):
image = pdb.gimp_file_load(jpg, "")
disp = pdb.gimp_display_new(image)
for image in gimp.image_list():
if text_number == 4:
new_left_up_x_4 = [image.width / 8 , image.width / 8 , 5 * image.width / 8 , 5 * image.width / 8]
new_left_up_y_4 = [image.height / 8 , 5 * image.height / 8 , image.height / 8 , 5 * image.height / 8]
save_file_name = image.filename
img_file_base_name= os.path.basename(image.filename)
img_file_name, ext = os.path.splitext(img_file_base_name)
image.filename = img_file_name + ".xcf"
for i in range(text_number):
text = unicode("■テキストレイヤー", "utf-8")
text_layer = pdb.gimp_text_layer_new(image, text, font, font_size, 0)
if text_number == 4:
new_left_up_x = new_left_up_x_4[i]
new_left_up_y = new_left_up_y_4[i]
else:
new_left_up_x = random.randint(image.width / 4, 3 * image.width / 4)
new_left_up_y = random.randint(image.height / 4 , 3 * image.height / 4)
text_layer.set_offsets(new_left_up_x, new_left_up_y)
image.add_layer(text_layer)
if tategaki == True:
pdb.gimp_text_layer_set_base_direction(text_layer,2)
else:
pdb.gimp_text_layer_set_base_direction(text_layer,0)
text_layer_height = image.height / 4
text_layer_width = image.width / 6
pdb.gimp_text_layer_resize(text_layer, text_layer_width, text_layer_height)
save_name, ext = os.path.splitext(save_file_name)
save_path = save_name + ".xcf"
pdb.gimp_file_save(image , text_layer , save_path , save_path)
for image in gimp.image_list():
for displayID in range(1,image.ID+50):
display=gimp._id2display(displayID)
if isinstance(display,gimp.Display):
break
if not display:
raise Exception("ビューがありません")
gimp.delete(display)
register(
"add_tategaki_text_and_save_xcf",
"add tategaki text & save xcf",
"add tategaki text & save xcf",
"katsumi",
"katsumi",
"2022",
"add tategaki text & save xcf",
"",
[
(PF_INT, "text_number", "テキストレイヤー数", 4),
(PF_FONT , "font", "フォント", "MS Gothic"),
(PF_INT , "font_size", "フォントサイズ", 20),
(PF_BOOL , "tategaki", "縦書きにする", True),
(PF_BOOL , "already_open_image", "すでに開いている画像にテキストをつける", True),
(PF_DIRNAME , "folder_path", "フォルダーの場所をしてしてください", ""),
],
[],
tategaki_text_save_xcf,
menu = "<Image>/Layer" )
main()
最初から開いているビューの閉じ方(displayの取得)の参考
注:結局アクティブなウィンドウ(画像)を指定することはできない事が分かった
gimpchat.com
プラグインとして動かすための解説の参考
yasuo-ssi.hatenablog.com
ダイヤログの記述例
github.com