简体   繁体   English

如何对 PIL 中的单个点应用仿射变换?

[英]how to apply a affine transformation to a single dot in PIL?

In PIL I have rotated a rectangle by an angle and pasted it back to the background image.在 PIL 中,我将一个矩形旋转了一个角度并将其粘贴回背景图像。 I'd like to know what will be the new coordinates of a specific corner.我想知道特定角落的新坐标是什么。

function(old_coord) => new coordinates函数(旧坐标)=> 新坐标

I have read in the documentation it mentioned the center of rotation by default is the center of the image.我在文档中读过它提到默认旋转中心是图像的中心。

Here is my code:这是我的代码:

import PIL
from PIL import Image, ImageDraw
import os

background = Image.open('background.jpg')
rectangle = Image.open("rectangle.png")
my_angle = 30

rectangle_rotate = Image.Image.rotate(rectangle, angle=my_angle, resample=Image.BICUBIC, expand=True)

# box: 2-tuple giving the upper left corner
px = int(background.size[0] / 2)
py = int(background.size[1] / 2)
background.paste(im=rectangle_rotate,
                     box=(px, py),
                     mask=rectangle_rotate)
# The new position I'm getting is wrong, how come?????
pos_xNew = px * math.cos(math.radians(my_angle)) + py * math.sin(math.radians(my_angle))
pos_yNew = -px * math.sin(math.radians(my_angle)) + py * math.cos(math.radians(my_angle))
print('pos_xNew:', pos_xNew)
print('pos_yNew:', pos_yNew)
draw_img_pts = ImageDraw.Draw(background)
r = 10
# Drawing a simple small circle circle for visualization
draw_img_pts.ellipse((pos_xNew - r, pos_yNew - r, pos_xNew + r, pos_yNew + r), fill='red')

background.save('example_with_roatation.png')

how can I find the new coordinates value?如何找到新的坐标值? I keep getting wrong value .我不断得到错误的价值

Background image (input):背景图像(输入): 背景

Rectangle image (input):矩形图像(输入):

长方形

The Output I got with zero rotation as expected:我得到的 Output 如预期的那样零旋转:

零旋转

Output I got after 30 degree rotation: Output 旋转30度后得到:

30度旋转

Comments inline内联评论

import math
import os

import numpy as np  # many operations are more concise in matrix form
import PIL
from PIL import Image, ImageDraw

def get_rotation_matrix(angle):
    """ For background, https://en.wikipedia.org/wiki/Rotation_matrix
    rotation is clockwise in traditional descartes, and counterclockwise, 
    if y goes down (as in picture coordinates)
    """
    return np.array([
        [np.cos(angle), -np.sin(angle)],
        [np.sin(angle), np.cos(angle)]])

def rotate(points, pivot, angle):
    """ Get coordinates of points rotated by a given angle counterclocwise 
    
    Args:
        points (np.array): point coordinates shaped (n, 2)
        pivot (np.array): [x, y] coordinates of rotation center
        angle (float): counterclockwise rotation angle in radians
    Returns:
        np.array of new coordinates shaped (n, 2)
    """
    relative_points = points - pivot
    return relative_points.dot(get_rotation_matrix(angle)) + pivot

background = Image.open('background.jpg')
rectangle = Image.open("rectangle.png")
my_angle_deg = 30
my_angle = math.radians(my_angle_deg)

rsize_x, rsize_y = rectangle.size
# to get shift introduced by rotation+clipping we'll need to rotate all four corners
# starting from top-right corners, counter-clockwise
rectangle_corners = np.array([
    [rsize_x, 0],  # top-right
    [0, 0],  # top-left
    [0, rsize_y],  # bottom-left
    [rsize_x, rsize_y]  # bottom-right
])
# rectangle_corners now are:
# array([[262,   0],
#       [  0,   0],
#       [  0,  67],
#       [262,  67]])

rotated_corners = rotate(rectangle_corners, rectangle_corners[0], my_angle)
# as a result of rotation, one of the corners might end up left from 0,
# e.g. if the rectangle is really tall and rotated 90 degrees right
# or, leftmost corner is no longer at 0, so part of the canvas is clipped
shift_introduced_by_rotation_clip = rotated_corners.min(axis=0)

rotated_shifted_corners = rotated_corners - shift_introduced_by_rotation_clip

# finally, demo
# this part is just copied from the question
rectangle_rotate = Image.Image.rotate(rectangle, angle=my_angle_deg, resample=Image.BICUBIC, expand=True)

# box: 2-tuple giving the upper left corner
px = int(background.size[0] / 2)
py = int(background.size[1] / 2)
background.paste(im=rectangle_rotate,
                     box=(px, py),
                     mask=rectangle_rotate)

# let's see if dots land right after these translations:
draw_img_pts = ImageDraw.Draw(background)
r = 10
for point in rotated_shifted_corners:
    pos_xNew, pos_yNew = point + [px, py]
    draw_img_pts.ellipse((pos_xNew - r, pos_yNew - r, pos_xNew + r, pos_yNew + r), fill='red')    

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM