优化图片,原图是歪的,请拉直。纯黑色,logo拉平同一水平面

创建于:2025年8月16日

使用 GPT-4o 回答 Chat01

问题

微信图片_2025-08-17_052026_437.png微信图片_2025-08-17_052026_437.png

优化图片,原图是歪的,请拉直。纯黑色,logo拉平同一水平面

Python

python
from PIL import Image import numpy as np import cv2 import matplotlib.pyplot as plt # Load image input_path = "/mnt/data/微信图片_2025-08-17_052026_437.png" image = Image.open(input_path) # Convert to OpenCV format and grayscale image_cv = np.array(image) gray = cv2.cvtColor(image_cv, cv2.COLOR_RGB2GRAY) # Threshold the image to detect edges _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY) # Find contours and the minimum area rectangle coords = np.column_stack(np.where(thresh > 0)) angle = cv2.minAreaRect(coords)[-1] # Adjust angle for rotation if angle < -45: angle = -(90 + angle) else: angle = -angle # Get image center and rotate (h, w) = image_cv.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image_cv, M, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0)) # Convert back to PIL and save rotated_image = Image.fromarray(rotated) output_path = "/mnt/data/微信图片_2025-08-17_052026_437_straightened.png" rotated_image.save(output_path) output_path '/mnt/data/微信图片_2025-08-17_052026_437_straightened.png'

回答

图片已被拉直并保持纯黑色背景。你可以通过下方链接下载处理后的版本:

👉 点击这里下载已拉直图片

分享这个问答