Проблема с обрезкой изображения Netvips с непрямоугольным изображением - PullRequest
0 голосов
/ 04 октября 2019

Я использую Netvips , чтобы обрезать изображение и на основе четырех угловых точек, но изображение нельзя обрезать, используя только верхние точки, потому что верхняя ширина и нижняя ширина не совпадают.

Ниже приведен код и ссылка для различных предоставленных образцов

Image image = Image.NewFromFile(args[0]);
string outFilename = args[1];
int topLeftX = int.Parse(args[2]);
int topLeftY = int.Parse(args[3]);
int topRightX = int.Parse(args[4]);
int topRightY = int.Parse(args[5]);
int bottomRightX = int.Parse(args[6]);
int bottomRightY = int.Parse(args[7]);

// the angle the top edge is rotated by
int dx = topRightX - topLeftX;
int dy = topRightY - topLeftY;
double angle = (180 / Math.PI) * Math.Atan2(dx, dy);
if (angle < -45 || angle >= 45)
{
    angle = 90 - angle;
}

// therefore the angle to rotate by to get it straight
angle = -angle;

image = image.Rotate(angle);

// the new position of the rectangle in the rotated image
double radians = (Math.PI * angle) / 180.0;
double c = Math.Cos(radians);
double s = Math.Sin(radians);

int left = Convert.ToInt32(topLeftX * c - topLeftY * s);
int top = Convert.ToInt32(topLeftX * s + topLeftY * c);
int width = Convert.ToInt32(Math.Sqrt(Math.Pow(topRightX - topLeftX, 2) +
                                      Math.Pow(topRightY - topLeftY, 2)));
int height = Convert.ToInt32(Math.Sqrt(Math.Pow(topRightX - bottomRightX, 2) +
                                       Math.Pow(topRightY - bottomRightY, 2)));

// after a rotate, the new position of the origin is given by .Xoffset, .Yoffset
Image tile = image.Crop(left + image.Xoffset, top + image.Yoffset, width, height);

tile.WriteToFile(outFilename);

Образец изображения - http://i.stack.imgur.com/x21me.png

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...