Skip to content

Commit

Permalink
Using IM distort to good effect
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-wallis committed Jan 23, 2018
1 parent 3e35ae5 commit 9187895
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion coincidence/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

.DEFAULT_GOAL:= test-images

RUBY := ruby2.4
#RUBY := ruby2.4
RUBY := ruby
MAKE_TEST_IMAGES := make-test-images.rb
TRANSFORM_IMAGES := transform-images.rb
TESTDATADIR := testdata
Expand Down
13 changes: 13 additions & 0 deletions coincidence/transform-images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ def rad_to_deg(r)

min_dist = image_set.map {|i| i.dist}.min

# Use first image to define reference point 'destinations'
dest_points = image_set.first.points
ref_image = Magick::ImageList.new(image_set.first.filename)
viewport = "#{ref_image.columns}x#{ref_image.rows}+0+0"
puts viewport

image_set.each {|i|
img = Magick::ImageList.new(i.filename)
img.background_color = 'blue'
filename = "#{$options[:output_dir]}/#{File.basename(i.filename)}"
img_rotated = img.rotate(rad_to_deg(mean_angle - i.angle))
img_rotated.write(filename)

aff_filename = "#{$options[:output_dir]}/aff_#{File.basename(i.filename)}"
scaling = min_dist/i.dist
rotation = mean_angle - i.angle
Expand All @@ -71,5 +78,11 @@ def rad_to_deg(r)
matrix = Magick::AffineMatrix.new(scaling, rotation, rotation, scaling, tx, tx)
img_affined = img.affine_transform(matrix)
img_affined.write(aff_filename)

img_distorted = img.distort(Magick::AffineDistortion, i.points.zip(dest_points).flatten) {
self.define "distort:viewport", viewport
}
distort_filename = "#{$options[:output_dir]}/distort_#{File.basename(i.filename)}"
img_distorted.write(distort_filename)
}

0 comments on commit 9187895

Please sign in to comment.