Open Nakilon opened 1 year ago
After playing around I suppose rotation shifts the image, i.e. you don't know the odx/ody ahead, because it all shifts to hold the top left corner pixel inside the image. It feels wrong.
Hi @Nakilon,
Also the https://www.libvips.org/API/current/libvips-resample.html#vips-rotate doc has the ody arg twice.
Oops! Thanks, I patched it.
I'll make you a rotate example, I have some code somewhere.
Sorry, just to be clear, you'd like an image the same width and height as the input, and with the circle in the same position, but the circle should be rotated so the leaf points roughly upwards, is that right?
Here's a thing that does ^^^ my guess.
#!/usr/bin/ruby
require 'vips'
x, y, angle = 398, 290, -2.4995608216061647
image = Vips::Image.new_from_file ARGV[0]
# add an alpha so we can see the image edges
image = image.add_alpha
# the [[a, b], [c, d]] affine transform we want (simple rotate)
a = Math.cos(angle)
b = -Math.sin(angle)
c = -b
d = a
# point (x, y) in the input goes to this coordinate in the output
x_after = a * x + b * y
y_after = c * x + d * y
# therefore to keep (x, y) in the same place, we need to move the output area
# by the difference
dx = x_after - x
dy = y_after - y
image = image.affine [a, b, c, d], oarea: [dx, dy, image.width, image.height]
image.write_to_file ARGV[1]
I see:
$ ./naklion.rb ~/pics/naklion.png x2.png
To make:
You don't need the alpha, but it's an easy way to see where the image edges are.
... perhaps rotate
should have an oarea
parameter. It would make this a little simpler.
Yes, you understood right. Thank you. I predicted it would need to calculate these sinuses by yourself. And yes, it's very inconvinient. As a user of this method I don't expect the rotation to change the size of image, because it's kind of unpredictable. After have no luck with it I ended up with just pixel-by-pixel projecting by myself ..(
Vips::Image.bandjoin( img.bandsplit.map do |band|
array = band.to_a
Vips::Image.new_from_array( array.map.with_index do |row, i|
row.map.with_index do |c, j|
(inner+outer)/2 < Math.hypot(x-j, y-i) ? c : ((
ii = i - y
jj = j - x
array[jj * Math::sin(angle) + ii * Math::cos(angle) + y]\
[jj * Math::cos(angle) - ii * Math::sin(angle) + x]
))
end
end ).cast :uchar
end )
otherwise I would need kind of reverse engineer this transformation method.
The goal was to transform this to this
I need to rotate this image around the center of the circle with known coordinates
but it looks wrong, the position has shifted
how do I do it without shifting the resulting circle?
Also the https://www.libvips.org/API/current/libvips-resample.html#vips-rotate doc has the
ody
arg twice.