1. Products
  2.   Image
  3.   Ruby
  4.   PSD.rb
 
  

Open Source Ruby Library for Parsing Photoshop PSD File

Ruby API that supports converting PSD to canvas, exporting PSD data, access PSD slices data, and access PSD layer inside Ruby apps.  

PSD.rb is a lightweight, easy to use open source Ruby library that enables software developers to parse Adobe Photoshop PSD file or extract Metadata and other usable information using Ruby code. The library allows users to handle Photoshop document data in a manageable tree structure. The aim behind developing PSD.rb was to provide better PSD scraping capabilities to developers working with Photoshop PSD files regularly.

The library handles all the major functionalities regarding accessing and opening the file and looking for the desired data and retrieve it in the operational form. The PSD.rb library is very easy to handle and enables developers to work with a Photoshop document in a manageable tree structure and search out important data such as document size, document structure, folder name and size, the opacity of the folder, folder visibility,  font name, font color or size, vector mask data, flattened image data and much more

Previous Next

Getting Started with PSD.rb

The recommended way to install PSD.rb is using RubyGems. Please use the following command for a smooth installation.

Install PSD.rb via GitHub

$ gem install psd

Exporting PSD Data using Ruby API

The open source PSD.rb library enables software programmers to export data from a PSD file with ease using Ruby commands. While working in the tree structure, we can recursively export any node to an object, which includes all the common information. It is also possible to export the PSD to a flattened image file with just a couple of lines of Ruby code. Please remember to save it with compatibility Mode enabled, otherwise an empty image will be produced

Export PSD Data via Ruby API

require 'fileutils'
require 'benchmark'
require './lib/psd'

file = ARGV[0] || 'examples/images/example.psd'
psd = PSD.new(file, parse_layer_images: true)

results = Benchmark.measure "Layer image exporting" do
  psd.parse!

  psd.tree.descendant_layers.each do |layer|
    path = layer.path.split('/')[0...-1].join('/')
    FileUtils.mkdir_p("output/#{path}")
    layer.image.save_as_png "output/#{layer.path}.png"
  end
end

puts Benchmark::CAPTION
puts results.to_s

Convert PSD to Canvas using Ruby

The open source PSD.rb library has provided complete functionality for converting PSD to canvas inside Ruby applications. The library supports parsing the imported PSD and converts it into canvas objects. After that, you can iterate the layers of the PSD objects and add the objects into the canvas with ease. After that order, the added objects and you are done. You can also export the canvas to PDF, SVG, PNG, PEG, and more.

Access PSD Slices Data

The PSD.rb library gives software developers the capability to access PSD slices directly inside their own applications. It also supports getting an array of all slices in the document with just a couple of lines of Ruby code. It is also possible to search slices by their name or by their ID. The library also provides support for export slices as a PNG.

Access PSD Slices Data via Ruby API

require 'benchmark'
require './lib/psd'

require 'pp'

psd = nil
file = ARGV[0] || 'examples/images/example.psd'
results = Benchmark.measure "PSD parsing" do
  psd = PSD.new(file)
  psd.parse!
end

if psd.resources[:slices]
  psd.resources[:slices].data.to_a.each do |slice|
    pp slice
  end
end

Access PSD Layer Data

The open source PSD.rb library enables software professionals to access PSD layer data using Ruby commands. You can easily get a name as well as dimensions of the layer using a couple of lines of Ruby code. Developers can also access various other types of information stored in layer info blocks such as size, color, fonts, and so on.

Access PSD Layer via Ruby API

//Get Name & Dimensions of a layer
psd.tree.descendant_layers.first.name
psd.tree.descendant_layers.first.width

//Get Other Layer Info
psd.tree.descendant_layers.first.text[:font]

# Returns
{:name=>"HelveticaNeue-Light",
 :sizes=>[33.0],
 :colors=>[[255, 19, 120, 98]],
 :css=>
  "font-family: \"HelveticaNeue-Light\", \"AdobeInvisFont\", \"MyriadPro-Regular\";\nfont-size: 33.0pt;\ncolor: rgba(19, 120, 98, 255);"}
 English