-
Sample code below may cause image after transform to show beyong original size. from PIL import Image
import numpy as np
import cv2
# 读取图像
image = Image.open(r"F:\23.png")
# 定义透视变换矩阵
matrix = np.array([
[1, 0.2, 0], # x 轴拉伸因子
# [0.5, 1, 0], # y 轴拉伸因子
[0, 1, 0], # y 轴拉伸因子
[0, 0, 1] # 平移因子
])
# # matrix = np.array([
# # # [1, 0.5, 0], # x 轴拉伸因子
# # [0.5, 0, 0.5], # x 轴拉伸因子
# # # [0.5, 1, 0], # y 轴拉伸因子
# # [0, 1, 0], # y 轴拉伸因子
# # [0, 0, 1] # 平移因子
# # ])
#
# width, height = image.size
# # 计算变换后的图像尺寸
# new_width = int(width * 1.5)
# new_height = int(height * 1.5)
# # 进行透视变换
# # trans_image = image.transform(image.size, Image.AFFINE, matrix.flatten())
# trans_image = image.transform((new_width, new_height), Image.AFFINE, matrix.flatten())
# # 保存变换后的图像
# trans_image.save("output_image.jpg")
img = cv2.cvtColor(np.asarray(trans_image),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey() |
Beta Was this translation helpful? Give feedback.
Answered by
radarhere
Jun 4, 2024
Replies: 1 comment 13 replies
-
from PIL import Image
import numpy as np
image = Image.open("F23.png")
matrix = np.array([
[1, 0.2, 0],
[0, 1, 0],
[0, 0, 1]
])
new_width = int(image.width * 1.5)
new_height = int(image.height * 1.5)
transformed_image = image.transform((new_width, new_height), Image.AFFINE, matrix.flatten())
transformed_image.save("output_image.jpg") gives You can use But I think you're asking for a way to reverse the matrix operation? You can simply from PIL import Image
import numpy as np
image = Image.open("F23.png")
matrix = np.array([
[1, 0.2, 0],
[0, 1, 0],
[0, 0, 1]
])
new_width = int(image.width * 1.5)
new_height = int(image.height * 1.5)
transformed_image = image.transform((new_width, new_height), Image.AFFINE, matrix.flatten())
matrix = np.flip(matrix)
reversed_image = image.transform(image.size, Image.AFFINE, matrix.flatten())
reversed_image.save("reversed_image.png") |
Beta Was this translation helpful? Give feedback.
13 replies
Answer selected by
radarhere
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gives
You can use
crop()
to manually remove the black box.But I think you're asking for a way to reverse the matrix operation? You can simply
flip()
the matrix.