クイックリファレンス

PSD.rb Photoshopパーサー チートシート

PSD.rbクイックリファレンス:RubyによるPhotoshop PSDファイルの解析、レイヤーツリー走査、テキスト・フォント情報の抽出、画像出力。

· 公開日

[!NOTE] アーカイブされたライブラリ: PSD.rb は、Adobe Photoshop を必要とせずに .psd ファイルを解析できるRuby製スタンドアロンライブラリです。

PSD.rb は、Adobe PhotoshopやネイティブC拡張に依存することなく、PSDファイルを構造化されたRubyオブジェクトとして解析します。

ファイルの読み込みと解析

require 'psd'

psd = PSD.new('design.psd', parse_layer_images: true)
psd.parse!

レイヤーツリーの走査 (Tree Traversal)

# ドキュメントのルートノード
node = psd.tree

# 走査ヘルパーメソッド
node.root
node.descendants
node.ancestors
node.siblings
node.subtree

# レイヤー種別によるフィルタ
groups = node.descendant_groups
layers = node.descendant_layers

レイヤーのプロパティ

layer = node.children.first

layer.name         # => "Header Background"
layer.top          # => 0 (Y coordinate)
layer.left         # => 0 (X coordinate)
layer.width        # => 1440
layer.height       # => 800

layer.visible?     # => true
layer.hidden?      # => false
layer.layer?       # => true
layer.group?       # => false

layer.blending_mode # => "normal", "multiply", etc.
layer.opacity       # => 0..255 (divide by 255 for CSS opacity)

テキストとタイポグラフィ情報の抽出

if layer.text
  puts layer.text[:value]             # テキスト文字列
  puts layer.text[:font][:name]       # フォントファミリー名 (例: 'Helvetica')
  puts layer.text[:font][:sizes]      # 使用されているフォントサイズの配列
  puts layer.text[:font][:colors]     # RGBAカラー値の配列
  puts layer.text[:font][:css]        # 計算済みのCSSフォント定義
end

レイヤー画像のエクスポート

# レイヤー画像をPNGファイルとして保存
layer.image.save_as_png("layer_#{layer.name}.png")

参考資料

関連テーマ