python如何将图片摆正?
a4纸张文件图片拍歪了,怎么在python中批量摆正?
网友回复
Python提供了多种库和方法来实现这一功能。以下是几种常用的图片摆正方法:
1. 基于OpenCV的图像校正
OpenCV是最常用的图像处理库,提供了多种图像校正方法:
1.1 基于边缘检测和霍夫变换的校正
import cv2 import numpy as np import math def deskew_image(image_path, output_path=None): # 读取图像 image = cv2.imread(image_path) # 转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 边缘检测 edges = cv2.Canny(gray, 50, 150, apertureSize=3) # 霍夫线变换 lines = cv2.HoughLines(edges, 1, np.pi/180, 200) if lines is not None: # 计算倾斜角度 angles = [] for line in lines: rho, theta = line[0] # 只考虑接近水平或垂直的线 if (theta < np.pi/4 or theta > 3*np.pi/4): angles.append(theta) if angles: # 计算平均角度 median_angle = np.median(angles) # 将角度转换为度数 angle = median_angle * 180 / np.pi # 调整角度(确保角度在-45到45度之间) if angle > 45: angle = 90 - angle elif angle < -45: angle = -90 - angle # 获取图像中心 (h, w) = image.shape[:2] center = (w // 2, h // 2) # 旋转图像 M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) # 保存结果 if output_path: cv2.imwrite(output_path, rotated) return rotated # 如果没有检测到线条,返回原图 if output_path: cv2.imwrite(output_path, image) return image # 使用示例 deskew_image("tilted_image.jpg", "corrected_image.jpg")
1.2 基于文本行的校正(适用于文档图像)
import cv2 import numpy as np def correct_skew(image_path, output_path=None, delta=1, limit=5): # 读取图像 image = cv2.imread(image_path) # 转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 二值化处理 thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # 计算各个角度的投影,找到文本行最整齐的角度 scores = [] angles = np.arange(-limit, limit + delta, delta) for angle in angles: # 旋转图像 (h, w) = thresh.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(thresh, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) # 计算水平投影 hist = cv2.reduce(rotated, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32S) # 计算投影的方差(方差越大,说明文本行越整齐) score = np.var(hist) scores.append(score) # 找到得分最高的角度 best_angle = angles[np.argmax(scores)] # 旋转原始图像 (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, best_angle, 1.0) rotated...
点击查看剩余70%