Class: Assembly::Image
- Inherits:
-
Object
- Object
- Assembly::Image
- 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.2'
Instance Attribute Summary collapse
-
#tmp_path ⇒ Object
stores the path to the tmp file generated during the JP2 creation process.
Instance Method Summary collapse
-
#add_exif_profile_description(profile_name, force = false) ⇒ Object
Add an exif color profile descriptions to the image.
-
#create_jp2(params = {}) ⇒ Assembly::Image
Create a JP2 file for the current image.
-
#dpg_jp2_filename ⇒ string
Returns the full DPG equivalent jp2 path and filename that would match with the given image.
-
#height ⇒ integer
Get the image height from exif data.
-
#jp2_filename ⇒ string
Returns the full default jp2 path and filename that will be created from the given image.
-
#profile ⇒ string
Get the image color profile.
-
#valid? ⇒ boolean
Examines the input image for validity.
-
#width ⇒ integer
Get the image width from exif data Example: source_img=Assembly::Image.new(‘/input/path_to_file.tif’) puts source_img.width # gives 100.
Instance Attribute Details
#tmp_path ⇒ Object
stores the path to the tmp file generated during the JP2 creation process
14 15 16 |
# File 'lib/assembly-image/image.rb', line 14 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')
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/assembly-image/image.rb', line 70 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/AbcSize rubocop:disable Metrics/MethodLength
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/assembly-image/image.rb', line 121 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. source_path = if mimetype != 'image/tiff' @tmp_path = make_tmp_tiff(tmp_folder: params[:tmp_folder]) else @path end jp2_command = jp2_create_command(source_path: source_path, output: output) result = `#{jp2_command}` raise "JP2 creation command failed: #{jp2_command} with result #{result}" unless $CHILD_STATUS.success? File.delete(source_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_filename ⇒ string
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
99 100 101 |
# File 'lib/assembly-image/image.rb', line 99 def dpg_jp2_filename jp2_filename.gsub('_00_', '_05_') end |
#height ⇒ integer
Get the image height from exif data
Example:
source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.height # gives 100
44 45 46 |
# File 'lib/assembly-image/image.rb', line 44 def height exif.imageheight end |
#jp2_filename ⇒ string
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
89 90 91 |
# File 'lib/assembly-image/image.rb', line 89 def jp2_filename File.extname(@path).empty? ? "#{@path}.jp2" : @path.gsub(File.extname(@path), '.jp2') end |
#profile ⇒ string
Get the image color profile
Example:
source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.profile # gives 'Adobe RGB 1998'
34 35 36 |
# File 'lib/assembly-image/image.rb', line 34 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
24 25 26 |
# File 'lib/assembly-image/image.rb', line 24 def valid? valid_image? # behavior is defined in assembly-objectfile gem end |
#width ⇒ integer
Get the image width from exif data Example:
source_img=Assembly::Image.new('/input/path_to_file.tif')
puts source_img.width # gives 100
53 54 55 |
# File 'lib/assembly-image/image.rb', line 53 def width exif.imagewidth end |