Class: Assembly::Image

Inherits:
Object
  • Object
show all
Includes:
ObjectFileable
Defined in:
lib/assembly-image/image.rb,
lib/assembly-image/version.rb

Overview

Main Image class

Constant Summary collapse

VERSION =

Project version number

'1.7.7'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tmp_pathObject

stores the path to the tmp file generated during the JP2 creation process



15
16
17
# File 'lib/assembly-image/image.rb', line 15

def tmp_path
  @tmp_path
end

Instance Method Details

#add_exif_profile_description(profile_name, force = false) ⇒ Object

Add an exif color profile descriptions to the image. This is useful if your source TIFFs do not have color profile descriptions in the EXIF data, but you know what it should be. This will allow the images to pass the validaty check and have JP2s created successfully.

Note you will need full read/write access to the source path so that new EXIF data can be saved.

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
source_img.add_exif_profile_description('Adobe RGB 1998')

Parameters:

  • profile_name (String)

    profile name to be added, current options are ‘Adobe RBG 1998’,‘Dot Gain 20%’,‘sRGB IEC61966-2.1’

  • force (String) (defaults to: false)

    if set to true, force overwrite a color profile description even if it already exists (default: false)



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/assembly-image/image.rb', line 82

def add_exif_profile_description(profile_name, force = false)
  if profile.nil? || force
    input_profile = profile_name.gsub(/[^[:alnum:]]/, '') # remove all non alpha-numeric characters, so we can get to a filename
    path_to_profiles = File.join(Assembly::PATH_TO_IMAGE_GEM, 'profiles')
    input_profile_file = File.join(path_to_profiles, "#{input_profile}.icc")
    command = "exiftool '-icc_profile<=#{input_profile_file}' #{path}"
    result = `#{command} 2>&1`
    raise "profile addition command failed: #{command} with result #{result}" unless $CHILD_STATUS.success?
  end
rescue StandardError => e
  puts "** Error for #{filename}: #{e.message}"
end

#create_jp2(params = {}) ⇒ Assembly::Image

Create a JP2 file for the current image. Important note: this will not work for multipage TIFFs.

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
derivative_img=source_img.create_jp2(:overwrite=>true)
puts derivative_img.mimetype # 'image/jp2'
puts derivative_image.path # '/input/path_to_file.jp2'

rubocop:disable Metrics/CyclomaticComplexity:

Parameters:

  • params (Hash) (defaults to: {})

    Optional parameters specified as a hash, using symbols for options:

    • :output => path to the output JP2 file (default: mirrors the source file name and path, but with a .jp2 extension)

    • :overwrite => if set to false, an existing JP2 file with the same name won’t be overwritten (default: false)

    • :tmp_folder => the temporary folder to use when creating the jp2 (default: ‘/tmp’); also used by imagemagick

    • :preserve_tmp_source => if set to true, preserve the temporary file generated during the creation process and store path in ‘tmp_path’ attribute (default: false)

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/assembly-image/image.rb', line 132

def create_jp2(params = {})
  output = params[:output] || jp2_filename
  create_jp2_checks(output: output, overwrite: params[:overwrite])

  # Using instance variable so that can check in tests.
  @tmp_path = make_tmp_tiff(tmp_folder: params[:tmp_folder])

  jp2_command = jp2_create_command(source_path: @tmp_path, output: output)
  result = `#{jp2_command}`
  raise "JP2 creation command failed: #{jp2_command} with result #{result}" unless $CHILD_STATUS.success?

  File.delete(@tmp_path) unless @tmp_path.nil? || params[:preserve_tmp_source]

  # create output response object, which is an Assembly::Image type object
  Assembly::Image.new(output)
end

#dpg_jp2_filenamestring

Returns the full DPG equivalent jp2 path and filename that would match with the given image

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.jp2_filename # gives /input/path_to_file.jp2

Returns:

  • (string)

    full DPG equivalent jp2 path and filename



111
112
113
# File 'lib/assembly-image/image.rb', line 111

def dpg_jp2_filename
  jp2_filename.gsub('_00_', '_05_')
end

#heightinteger

Get the image height from exif data

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.height # gives 100

Returns:

  • (integer)

    image height in pixels



45
46
47
# File 'lib/assembly-image/image.rb', line 45

def height
  exif.imageheight
end

#jp2_filenamestring

Returns the full default jp2 path and filename that will be created from the given image

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.jp2_filename # gives /input/path_to_file.jp2

Returns:

  • (string)

    full default jp2 path and filename that will be created from the given image



101
102
103
# File 'lib/assembly-image/image.rb', line 101

def jp2_filename
  File.extname(@path).empty? ? "#{@path}.jp2" : @path.gsub(File.extname(@path), '.jp2')
end

#profilestring

Get the image color profile

Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.profile # gives 'Adobe RGB 1998'

Returns:

  • (string)

    image color profile



35
36
37
# File 'lib/assembly-image/image.rb', line 35

def profile
  exif.nil? ? nil : exif['profiledescription']
end

#valid?boolean

Examines the input image for validity. Used to determine if image is correct and if JP2 generation is likely to succeed.

This method is automatically called before you create a jp2 but it can be called separately earlier as a sanity check.

Example:

source_img=Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_img.valid? # gives true

Returns:

  • (boolean)

    true if image is valid, false if not.



25
26
27
# File 'lib/assembly-image/image.rb', line 25

def valid?
  valid_image? # behavior is defined in assembly-objectfile gem
end

#widthinteger

Get the image width from exif data Example:

source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.width # gives 100

Returns:

  • (integer)

    image height in pixels



54
55
56
# File 'lib/assembly-image/image.rb', line 54

def width
  exif.imagewidth
end