Created
December 22, 2015 14:05
-
-
Save endel/ed4a66a0c5c2f56811b3 to your computer and use it in GitHub Desktop.
Export Photoshop Layers into power-of-two sized images.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!