Class: DynamicImage::ImageProcessor
- Inherits:
-
Object
- Object
- DynamicImage::ImageProcessor
- 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.
Defined Under Namespace
Modules: Colors, Frames, Transform
Instance Attribute Summary collapse
- #image ⇒ Vips::Image readonly
-
#target_format ⇒ Object
readonly
Returns the value of attribute target_format.
Instance Method Summary collapse
-
#convert(new_format) ⇒ DynamicImage::ImageProcessor
Convert the image to a different format.
-
#initialize(image, target_format: nil) ⇒ ImageProcessor
constructor
A new instance of ImageProcessor.
-
#read ⇒ String
Returns the image data as a binary string.
-
#size ⇒ Vector2d
Returns the image size as a Vector2d.
-
#write(path) ⇒ void
Write the image to a file.
Methods included from Transform
Methods included from Frames
Constructor Details
#initialize(image, target_format: nil) ⇒ ImageProcessor
Returns a new instance of ImageProcessor.
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
#image ⇒ Vips::Image (readonly)
36 37 38 |
# File 'lib/dynamic_image/image_processor.rb', line 36 def image @image end |
#target_format ⇒ Object (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.
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 |
#read ⇒ String
Returns the image data as a binary string.
72 73 74 75 |
# File 'lib/dynamic_image/image_processor.rb', line 72 def read image.write_to_buffer(target_format.extension, **target_format.) end |
#size ⇒ Vector2d
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.
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.
94 95 96 |
# File 'lib/dynamic_image/image_processor.rb', line 94 def write(path) image.write_to_file(path, **target_format.) end |