Class: DynamicImage::ImageProcessor

Inherits:
Object
  • Object
show all
Includes:
Colors, Frames, Transform
Defined in:
lib/dynamic_image/image_processor.rb,
lib/dynamic_image/image_processor/colors.rb,
lib/dynamic_image/image_processor/frames.rb,
lib/dynamic_image/image_processor/transform.rb

Overview

ImageProcessor

The image processing pipeline.

Every operation returns a new processor instead of modifying the one it was called on, so operations chain. Images are converted to sRGB and EXIF rotation is applied when the processor is built, so the pipeline always starts from a normalized image.

Examples:

DynamicImage::ImageProcessor
  .new(file)
  .crop(crop_start, crop_size)
  .resize(size)
  .convert(:jpeg)
  .read

See Also:

Defined Under Namespace

Modules: Colors, Frames, Transform

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transform

#crop, #resize, #rotate

Methods included from Frames

#frame, #frame_count

Constructor Details

#initialize(image, target_format: nil) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.

Parameters:

  • image (Vips::Image, Pathname, IO, String)

    the image, either an already loaded vips image or something DynamicImage::ImageReader can read

  • target_format (DynamicImage::Format, nil) (defaults to: nil)

    the format to write in, defaulting to the format the image was read from



42
43
44
45
46
47
48
49
50
51
# File 'lib/dynamic_image/image_processor.rb', line 42

def initialize(image, target_format: nil)
  if image.is_a?(Vips::Image)
    @image = image
    @target_format = target_format
  else
    reader = DynamicImage::ImageReader.new(image)
    @image = screen_profile(reader.read.autorot)
    @target_format = reader.format
  end
end

Instance Attribute Details

#imageVips::Image (readonly)

Returns:

  • (Vips::Image)


36
37
38
# File 'lib/dynamic_image/image_processor.rb', line 36

def image
  @image
end

#target_formatObject (readonly)

Returns the value of attribute target_format.



36
# File 'lib/dynamic_image/image_processor.rb', line 36

attr_reader :image, :target_format

Instance Method Details

#convert(new_format) ⇒ DynamicImage::ImageProcessor

Convert the image to a different format.

Converting a multi-frame image to a format that doesn't support animation keeps the first frame.

Parameters:

Returns:



59
60
61
62
63
64
65
66
67
# File 'lib/dynamic_image/image_processor.rb', line 59

def convert(new_format)
  new_format = DynamicImage::Format.find(new_format) unless new_format.is_a?(DynamicImage::Format)

  if frame_count > 1 && !new_format.animated?
    self.class.new(extract_frame(0), target_format: new_format)
  else
    self.class.new(image, target_format: new_format)
  end
end

#readString

Returns the image data as a binary string.

Returns:

  • (String)

    the encoded image



72
73
74
75
# File 'lib/dynamic_image/image_processor.rb', line 72

def read
  image.write_to_buffer(target_format.extension,
                        **target_format.save_options)
end

#sizeVector2d

Returns the image size as a Vector2d. For multi-frame images this is the size of a single frame, not of the filmstrip vips holds them in.

Returns:

  • (Vector2d)


81
82
83
84
85
86
87
88
# File 'lib/dynamic_image/image_processor.rb', line 81

def size
  Vector2d.new(
    image.get("width"),
    image.get(
      image.get_fields.include?("page-height") ? "page-height" : "height"
    )
  )
end

#write(path) ⇒ void

This method returns an undefined value.

Write the image to a file.

Parameters:

  • path (String)

    the path to write to



94
95
96
# File 'lib/dynamic_image/image_processor.rb', line 94

def write(path)
  image.write_to_file(path, **target_format.save_options)
end