Method: Vips::Image.new_from_array
- Defined in:
- lib/vips8/image.rb
permalink .new_from_array(array, scale = 1, offset = 0) ⇒ Image
Create a new Image from a 1D or 2D array. A 1D array becomes an
image with height 1. Use scale
and offset
to set the scale and
offset fields in the header. These are useful for integer
convolutions.
For example:
image = Vips::new_from_array [1, 2, 3]
or
image = Vips::new_from_array [
[-1, -1, -1],
[-1, 16, -1],
[-1, -1, -1]], 8
for a simple sharpening mask.
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/vips8/image.rb', line 499 def self.new_from_array(array, scale = 1, offset = 0) # we accept a 1D array and assume height == 1, or a 2D array # and check all lines are the same length if not array.is_a? Array raise Vips::Error, "Argument is not an array." end if array[0].is_a? Array height = array.length width = array[0].length if not array.all? {|x| x.is_a? Array} raise Vips::Error, "Not a 2D array." end if not array.all? {|x| x.length == width} raise Vips::Error, "Array not rectangular." end array = array.flatten else height = 1 width = array.length end if not array.all? {|x| x.is_a? Numeric} raise Vips::Error, "Not all array elements are Numeric." end image = Vips::Image.matrix_from_array width, height, array if image == nil raise Vips::Error end # be careful to set them as double image.set_double 'scale', scale.to_f image.set_double 'offset', offset.to_f return image end |