旋转图片
def rotate_image(image_path, angle, scale=1.0):
# 读取图像
image = cv2.imread(image_path)
if image is None:
print("Error: Could not read the image.")
return None
# 获取图像的高度和宽度
(h, w) = image.shape[:2]
# 计算图像中心
center = (w / 2, h / 2)
# 生成旋转矩阵
M = cv2.getRotationMatrix2D(center, angle, scale)
# 执行仿射变换
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
裁切圆形
def crop_circle(image_path):
# 读取图像
image = cv2.imread(image_path)
if image is None:
print("Error: Could not read the image.")
return None
# 获取图像的高度和宽度
(h, w) = image.shape[:2]
# 计算圆的中心 和 半径
center_x, center_y = w // 2, h // 2
radius = min(center_x, center_y)
# 创建一个掩膜,初始全黑(零)
mask = np.zeros((h, w), dtype="uint8")
# 在掩膜上绘制白色圆圈
cv2.circle(mask, (center_x, center_y), radius, 255, -1)
# 通过掩膜应用图像
masked_image = cv2.bitwise_and(image, image, mask=mask)
# 为了去除多余的黑色部分,将结果放入新的最小矩形中
x = center_x - radius
y = center_y - radius
cropped_image = masked_image[y:y + 2 * radius, x:x + 2 * radius]
return cropped_image
待续...