### Install Pre-commit Hooks Source: https://github.com/sbrunner/deskew/blob/master/README.md Commands to install pre-commit hooks for development contributions. ```bash pip install pre-commit pre-commit install --allow-missing-config ``` -------------------------------- ### Install Deskew via pip Source: https://github.com/sbrunner/deskew/blob/master/README.md Commands to install or upgrade the deskew package using pip. ```bash python3 -m pip install deskew python3 -m pip install -U deskew ``` -------------------------------- ### Deskew CLI Usage Source: https://context7.com/sbrunner/deskew/llms.txt Demonstrates how to install and use the deskew command-line interface for detecting and correcting image skew. Covers basic usage, saving output, and custom parameter adjustments like sigma, num-peaks, num-angles, and background color. ```bash # Install deskew pip install deskew # Get the skew angle of an image (prints to stdout) deskew input.png # Output: Estimated angle: -3.0 # Deskew an image and save the result deskew --output output.png input.png # Deskew with custom parameters deskew --output output.png --sigma 3.0 --num-peaks 20 --num-angles 180 input.png # Deskew with custom background color (r,g,b for color images) deskew --output output.png --background 255,255,255 input.png # Deskew grayscale image with gray background deskew --output output.png --background 128 grayscale_input.png ``` -------------------------------- ### Deskew with scikit-image Source: https://github.com/sbrunner/deskew/blob/master/README.md Example of using deskew with scikit-image to determine skew and rotate an image. ```python import numpy as np from skimage import io from skimage.color import rgb2gray from skimage.transform import rotate from deskew import determine_skew image = io.imread('input.png') grayscale = rgb2gray(image) angle = determine_skew(grayscale) rotated = rotate(image, angle, resize=True) * 255 io.imsave('output.png', rotated.astype(np.uint8)) ``` -------------------------------- ### Deskew CLI Usage Source: https://github.com/sbrunner/deskew/blob/master/README.md Command-line interface examples to detect the skew angle of an image or perform deskewing and save the result. ```bash deskew input.png deskew --output output.png input.png ``` -------------------------------- ### Deskew with OpenCV Source: https://github.com/sbrunner/deskew/blob/master/README.md Example of using deskew with OpenCV, including a custom rotation function to handle image dimensions correctly. ```python import math from typing import Tuple, Union import cv2 import numpy as np from deskew import determine_skew def rotate( image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]] ) -> np.ndarray: old_width, old_height = image.shape[:2] angle_radian = math.radians(angle) width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width) height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height) image_center = tuple(np.array(image.shape[1::-1]) / 2) rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0) rot_mat[1, 2] += (width - old_width) / 2 rot_mat[0, 2] += (height - old_height) / 2 return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background) image = cv2.imread('input.png') grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) angle = determine_skew(grayscale) rotated = rotate(image, angle, (0, 0, 0)) cv2.imwrite('output.png', rotated) ``` -------------------------------- ### Integrate deskew with OpenCV workflows Source: https://context7.com/sbrunner/deskew/llms.txt Shows how to use the deskew library alongside OpenCV for image processing. It includes a custom rotation function to handle background colors during the deskewing process. ```python import math from typing import Tuple, Union import cv2 import numpy as np from deskew import determine_skew def rotate_image( image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]] ) -> np.ndarray: """Rotate image by angle with specified background color.""" old_width, old_height = image.shape[:2] angle_radian = math.radians(angle) width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width) height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height) image_center = tuple(np.array(image.shape[1::-1]) / 2) rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0) rot_mat[1, 2] += (width - old_width) / 2 rot_mat[0, 2] += (height - old_height) / 2 return cv2.warpAffine( image, rot_mat, (int(round(height)), int(round(width))), borderValue=background ) # Load image with OpenCV image = cv2.imread('input.png') grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect skew angle angle = determine_skew(grayscale) if angle is not None: # Rotate with black background rotated = rotate_image(image, angle, (0, 0, 0)) cv2.imwrite('output.png', rotated) print(f"Image deskewed by {angle} degrees") ``` -------------------------------- ### Python API: Angle Constraints Source: https://context7.com/sbrunner/deskew/llms.txt Demonstrates how to constrain the skew angle detection to a specific range using `min_angle` and `max_angle` parameters in the Python API. This helps improve accuracy by filtering out false positives and is useful when the expected skew direction is known. ```python from skimage import io from skimage.color import rgb2gray from deskew import determine_skew image = io.imread('scanned_document.png') grayscale = rgb2gray(image) # Constrain to small positive angles only (clockwise skew) angle = determine_skew( grayscale, min_angle=0.0, max_angle=15.0 ) print(f"Positive skew angle: {angle}") # Constrain to small negative angles only (counter-clockwise skew) angle = determine_skew( grayscale, min_angle=-15.0, max_angle=0.0 ) print(f"Negative skew angle: {angle}") # Returns None if no angle found within constraints angle = determine_skew( grayscale, min_angle=30.0, # If document isn't skewed this much max_angle=45.0 # No angle will be detected ) if angle is None: print("No skew detected within specified range") ``` -------------------------------- ### Python API: Extended Angle Range Detection Source: https://context7.com/sbrunner/deskew/llms.txt Illustrates using the Python API to detect skew angles in an extended range (-90 to 90 degrees) using the `angle_pm_90` parameter. This is useful for documents scanned at arbitrary orientations. It shows how to determine the skew angle and apply rotation to correct the image. ```python from skimage import io from skimage.color import rgb2gray from skimage.transform import rotate import numpy as np from deskew import determine_skew image = io.imread('heavily_rotated_document.png') grayscale = rgb2gray(image) # Default mode: angle between -45 and 45 degrees angle_default = determine_skew(grayscale, angle_pm_90=False) print(f"Default range angle: {angle_default}") # Extended mode: angle between -90 and 90 degrees angle_extended = determine_skew(grayscale, angle_pm_90=True) print(f"Extended range angle: {angle_extended}") # Use extended range for documents with extreme rotation if angle_extended is not None: rotated = rotate(image, angle_extended, resize=True) * 255 io.imsave('corrected_document.png', rotated.astype(np.uint8)) ``` -------------------------------- ### Detect and correct skew with scikit-image Source: https://context7.com/sbrunner/deskew/llms.txt Demonstrates the basic usage of the determine_skew function to calculate the skew angle of a grayscale image and correct it using rotation. It covers both default and custom parameter configurations for the Hough transform. ```python import numpy as np from skimage import io from skimage.color import rgb2gray from skimage.transform import rotate from deskew import determine_skew # Load and convert image to grayscale image = io.imread('input.png') grayscale = rgb2gray(image) # Detect skew angle with default parameters angle = determine_skew(grayscale) print(f"Detected skew angle: {angle} degrees") # Detect skew with custom parameters angle = determine_skew( grayscale, sigma=3.0, num_peaks=20, angle_pm_90=False, min_angle=-10.0, max_angle=10.0, min_deviation=1.0 ) # Rotate the image to correct the skew if angle is not None: rotated = rotate(image, angle, resize=True) * 255 io.imsave('output.png', rotated.astype(np.uint8)) ``` -------------------------------- ### Debug skew detection process Source: https://context7.com/sbrunner/deskew/llms.txt Uses determine_skew_debug_images to retrieve both the skew angle and diagnostic images. This is useful for tuning parameters when the default detection is not accurate. ```python from skimage import io from deskew import determine_skew_debug_images # Load image image = io.imread('input.png') # Get skew angle and debug images angle, debug_images = determine_skew_debug_images( image, sigma=3.0, num_peaks=20, angle_pm_90=False, min_angle=None, max_angle=None, min_deviation=1.0 ) print(f"Detected angle: {angle} degrees") # Save debug images for analysis for name, debug_image in debug_images: import cv2 cv2.imwrite(f'debug_{name}.png', debug_image) print(f"Saved debug image: debug_{name}.png") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.