Skip to content

Instantly share code, notes, and snippets.

@mieki256
Created September 12, 2022 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mieki256/ae1dcf5672cd0ecac4517c17fbee1139 to your computer and use it in GitHub Desktop.
Save mieki256/ae1dcf5672cd0ecac4517c17fbee1139 to your computer and use it in GitHub Desktop.
GIMPの全レイヤー情報をテキストで保存するPython-Fu(GIMP-Python)スクリプト。Windows7 x64、GIMP 2.8.0 Portable + Python 2.7.2、GIMP 2.6.11 + Python 2.6.6 で動作確認した。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- mode: python; Encoding: utf8n -*-
u"""
全レイヤーの、左上座標、幅、高さ、をテキスト出力保存する.
by mieki256
0.0.1 2012.08.06 GIMP2.8、GIMP2.6用に作成。
"""
from gimpfu import *
import os.path
def is_old_gimp():
u"""GIMP 2.6.x上で動かしているか調べる."""
version = pdb.gimp_version()
if version.startswith("2.6."):
return True
if version.startswith("2.4."):
return True
else:
return False
def get_header(kind, imgname):
u"""テキスト出力のヘッダ部分を返す."""
if kind == 0:
# テキスト形式
return ""
else:
# xml形式
str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
str += "<TextureAtlas imagePath=\"%s\">\n" % (imgname)
return str
def get_footer(kind):
u"""テキスト出力のフッター部分を返す."""
if kind == 0:
# テキスト形式
return ""
else:
# xml形式
return "</TextureAtlas>\n"
def get_line_str(kind, name, x, y, w, h):
u"""1行分のテキストを得る."""
if kind == 0:
# テキスト形式
return "%s,%d,%d,%d,%d\n" % (name,x,y,w,h)
else:
# xml形式
return " <SubTexture name=\"%s\" x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n" % (name,x,y,w,h)
def get_layer_info(kind, layer):
u"""1枚分のレイヤーの情報を得る."""
lname = layer.name
lx = layer.offsets[0]
ly = layer.offsets[1]
lw = layer.width
lh = layer.height
return get_line_str(kind, lname, lx, ly, lw, lh)
def get_layer_ids(layer_ids):
u"""レイヤーIDリストを再帰で辿って取得する.(GIMP2.8以降用)"""
ids = []
for i in layer_ids:
item = gimp.Item.from_id(i)
if pdb.gimp_item_is_group(item) == 1:
# レイヤーグループだったので、子供レイヤーのIDリストを取得
num_children, child_ids = pdb.gimp_item_get_children(item)
# 子供レイヤーのIDリストを結合
ids.extend(get_layer_ids(child_ids))
else:
# 通常レイヤーだった
ids.append(i)
return ids
def python_fu_dump_layers_offsets(image, drawable, save_dir, fname, kind):
u"""メインの処理"""
# 出力ファイルのパスを生成
output_path = os.path.join(save_dir, fname)
str = get_header(kind, image.name)
# レイヤーIDリストを取得
num_layers, layer_ids = pdb.gimp_image_get_layers(image)
if is_old_gimp():
# GIMP 2.6以前用の処理
for layer in image.layers:
str += get_layer_info(kind, layer)
else:
# GIMP 2.8以降用の処理
# 再帰的に辿って、レイヤーIDリストを全取得
ids = get_layer_ids(layer_ids)
for i in ids:
# レイヤーIDからitem情報を取得
item = gimp.Item.from_id(i)
# 1行分のテキストを取得
str += get_layer_info(kind, item)
str += get_footer(kind)
# テキストファイルとして出力
fw = open(output_path, 'w')
fw.write(str)
fw.close()
# pdb.gimp_message("%s に保存しました." % (output_path))
# ----------------------------------------
# GIMPへのメニュー登録、ダイアログの指定
register(
# プロシジャの名前
"python-fu-dump-layers-offsets",
# プロシジャの説明文
"全レイヤーの座標やサイズをテキストファイルに出力",
# PDBに登録する追加情報
"dump layers offsets",
# 作者名
"mieki256",
# ライセンス情報
"Public Domain.",
# 作成日
"2012.08.06",
# メニュー表示場所・名前
"<Image>/Layer/全レイヤー情報をダンプ",
# 対応する画像タイプ
"RGB*, GRAY*, INDEXED",
# ダイアログの指定
[
# ディレクトリ名の選択欄(変数名, 項目名, 初期値)
(PF_DIRNAME, "save_dir", "保存ディレクトリ", "."),
# 保存ファイル名の入力欄
(PF_STRING, "fname", "保存ファイル名", 'dump_temp.txt' ),
# 出力形式の選択欄
(PF_OPTION, "kind", "出力形式", 0, ["text", "xml"])
],
# 戻り値の定義
[],
# 処理を埋け持つ関数名
python_fu_dump_layers_offsets
)
# プラグインを駆動させるための関数
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment