Skip to content

Instantly share code, notes, and snippets.

@NoobsArePeople2
Created November 21, 2013 03:45
Show Gist options
  • Save NoobsArePeople2/7575780 to your computer and use it in GitHub Desktop.
Save NoobsArePeople2/7575780 to your computer and use it in GitHub Desktop.
Export grouped and named layers from a PSD at the original document size and positioning
require 'chunky_png'
require 'fileutils'
require 'psd'
file = ARGV[0] || nil
if file == nil
puts "You must specify a PSD as the first argument."
exit
end
output = ARGV[1] || "output"
psd = PSD.new(file, parse_layer_images: true)
psd.parse!
hash = psd.tree.to_hash
w = hash[:document][:width]
h = hash[:document][:height]
psd.tree.descendant_layers.each do |layer|
path = layer.path.split('/')[0...-1].join('/')
FileUtils.mkdir_p("#{output}/#{path}")
pixels = layer.image.to_png
png = ChunkyPNG::Image.new(w, h, ChunkyPNG::Color::TRANSPARENT)
png.compose!(pixels, layer.left, layer.top)
png.save("#{output}/#{layer.path}.png", :fast_rgba)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment