Class: Assembly::Image

Inherits:
ObjectFile
  • Object
show all
Defined in:
lib/assembly/image.rb,
lib/assembly/image/jp2_creator.rb

Overview

The Image class contains methods to operate on an image.

Defined Under Namespace

Classes: Jp2Creator

Instance Method Summary collapse

Instance Method Details

#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('/dir/path_to_file.tif')
jp2_img = source_img.create_jp2(overwrite: true)
jp2_img.mimetype # 'image/jp2'
jp2_img.path # '/dir/path_to_file.jp2'

Parameters:

  • output (String)

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

  • overwrite (Boolean)

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

  • tmp_folder (Dir)

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

Returns:



60
61
62
# File 'lib/assembly/image.rb', line 60

def create_jp2(**params)
  Jp2Creator.create(self, **params)
end

#extract_first_page(output_path) ⇒ Object

Extract and save only the first page from a multi-image TIFF

Parameters:

  • output_path (String)

    path to save the extracted first page



31
32
33
34
35
36
37
38
# File 'lib/assembly/image.rb', line 31

def extract_first_page(output_path)
  raise "Cannot extract first page from mimetype #{mimetype}" unless mimetype == 'image/tiff'

  first_page = Vips::Image.new_from_file(path, page: 0).autorot
  first_page.write_to_file(output_path)

  true
end

#has_profile?Boolean

Does the image include an ICC profile?

Returns:

  • (Boolean)


76
77
78
# File 'lib/assembly/image.rb', line 76

def has_profile?
  vips_image.get_fields.include?('icc-profile-data')
end

#heightinteger

Returns image height in pixels.

Returns:

  • (integer)

    image height in pixels



11
12
13
# File 'lib/assembly/image.rb', line 11

def height
  vips_image.height
end

#jp2_filenamestring

Example: given original file of ‘/dir/path_to_file.tif’, gives ‘/dir/path_to_file.jp2’

Returns:

  • (string)

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



42
43
44
45
# File 'lib/assembly/image.rb', line 42

def jp2_filename
  # path is a property on Assembly::ObjectFile
  File.extname(path).empty? ? "#{path}.jp2" : path.gsub(File.extname(path), '.jp2')
end

#multi_page?boolean

Returns true if this image is a multi-page (e.g. a TIFF with multiple pages).

Returns:

  • (boolean)

    true if this image is a multi-page (e.g. a TIFF with multiple pages)



21
22
23
24
25
26
27
# File 'lib/assembly/image.rb', line 21

def multi_page?
  return false unless mimetype == 'image/tiff'

  vips_image.get('n-pages').to_i > 1
rescue Vips::Error
  false
end

#srgb?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/assembly/image.rb', line 71

def srgb?
  vips_image.interpretation == :srgb
end

#vips_imageObject



64
65
66
67
68
69
# File 'lib/assembly/image.rb', line 64

def vips_image
  # Disable cache. Otherwise, Vips gets confused by files with the same filename.
  Vips.cache_set_max_files(0)
  # autorot will only affect images that need rotation: https://www.libvips.org/API/current/libvips-conversion.html#vips-autorot
  @vips_image ||= Vips::Image.new_from_file(path).autorot
end

#widthinteger

Returns image width in pixels.

Returns:

  • (integer)

    image width in pixels



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

def width
  vips_image.width
end