abdoutelb
abdoutelb

Reputation: 1053

Combine remote images to one image using ruby-vips

I had a template image and need to append on that a specific images on X , Y positions. Is there any equivalent to that function in rmagick

ImageList.new("https://365psd.com/images/istock/previews/8479/84796157-football-field-template-with-goal-on-top-view.jpg")

and draw on that other images and generate one image.

Upvotes: 3

Views: 830

Answers (1)

jcupitt
jcupitt

Reputation: 11190

You can read and write URIs in ruby-vips like this:

#!/usr/bin/ruby

require "vips"
require "down"

def new_from_uri(uri)
  byte_source = Down.open uri

  source = Vips::SourceCustom.new
  source.on_read do |length|
    puts "reading #{length} bytes from #{uri} ..."
    byte_source.read length
  end
  source.on_seek do |offset, whence|
    puts "seeking to #{offset}, #{whence} in #{uri}"
    byte_source.seek(offset, whence)
  end

  return Vips::Image.new_from_source source, "", access: :sequential
end

a = new_from_uri "https://upload.wikimedia.org/wikipedia/commons/a/a6/Big_Ben_Clock_Face.jpg"
b = new_from_uri "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"
out = a.composite b, "over", x: 100, y: 100
out.write_to_file "x.jpg"

If you watch the console output you can see it loading the two source images and interleaving the pixels. It makes this output:

example output

The docs on Vips::Source have more details.

Upvotes: 4

Related Questions