アストラルプリズム

PC、スマホ、ゲームなどの備忘録と日記

複数の画像にテキストレイヤーを指定した数だけ追加してGIMPの.xcf形式で保存するプラグイン作った

複数の画像にテキストレイヤーを指定した数だけ追加してGIMPの.xcf形式で保存するプラグイン
フォントの種類・サイズと縦書きか横書きか選べる。
元から開いてる画像にレイヤーを増やすか、フォルダーを指定して画像を開くか選べる。
テキストレイヤーの位置はランダム、4つのときだけきれいに配置される(自分が4を使おうと思ったから)。
xcfの保存先は画像があったフォルダー。場所の指定はできない。
Githabとかにファイルを置くべきなんだろうがどうも管理できる気がしない。

インストール方法

以下をコピペしてファイル名をadd_tategaki_text_save_xcf.pyにしてutf-8で保存後、GIMPプラグインフォルダーに入れる。
GIMPのメニューの「レイヤー」→「add tategaki text & save xcf」をクリックすると動作する。
プラグインフォルダーはGIMPを起動し「編集」→「設定」→「フォルダー」→「プラグイン」で確認の事。
初回のフォント指定(MSゴシック)を変更するにはプラグイン下の方の行の"MS Gothic"のところを好きなフォント名に書き換えればよい。
フォント名はGIMPでテキストボックスを選択してツールオプションの好みのフォントを設定する時の名前をコピペすればよい

#!/usr/bin/python
# -*- coding: utf-8 -*-
import random , os ,glob
from gimpfu import *

def tategaki_text_save_xcf(text_number , font , font_size ,tategaki ,already_open_image , folder_path):
	#一番下のレイヤーの情報取得
	#指定したフォルダー内のJPG画像を開いていく
	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():
		#4の時だけきれいにテキストレイヤーを並べる。個人的な機能
		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)
			#
			#テキストレイヤーを縦書きにする。横書きにしたければ末尾の2を0にすること。
			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),
		#源暎アンチックを使いたい人はフォントをインストール後に
		#"MS Gothic"のところを"GenEi Antique Pv5 Medium"に書き換える
		(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