Skip to content

Instantly share code, notes, and snippets.

@endel
Created December 22, 2015 14:05
Show Gist options
  • Save endel/ed4a66a0c5c2f56811b3 to your computer and use it in GitHub Desktop.
Save endel/ed4a66a0c5c2f56811b3 to your computer and use it in GitHub Desktop.
Export Photoshop Layers into power-of-two sized images.
require 'json'
require 'psd'
def is_power_of_two(x)
n = x.to_i
while (((x % 2) == 0) && x > 1)
x = x/2
end
x == 1 && n != 1
end
file = ARGV[0] || './characters.psd'
psd = PSD.new(file, parse_layer_images: true)
psd.parse!
file_list = []
psd.tree.descendant_layers.each do |layer|
original = layer.image.to_png
size = [original.width, original.height].max
while !is_power_of_two(size)
size = size + 1
end
power_of_two = ChunkyPNG::Image.new(size, size, ChunkyPNG::Color::TRANSPARENT)
original.pixels.each_with_index do |color, i|
x = i % original.width
y = i / original.width
power_of_two.set_pixel(x,y,color)
end
power_of_two.save("images/#{layer.path.gsub('/', '-')}.png")
file_list << filename
end
File.open('resources.json', 'w+') do |f|
f.write(file_list.to_json)
end
@Yonderboy2
Copy link

Hi - this is just what Im looking for! I cant quite figure out how to use the .rb file in photoshop though - would you have any tips? many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment