File size: 3,896 Bytes
52d7e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea67a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import cv2
import numpy as np
import pytesseract
import platform
from PIL import Image
from deskew import determine_skew
from shutil import which
import os


def _configure_tesseract_path():
    current_os = platform.system()
    if current_os == "Windows":
        win_path = r"C:\Users\Wissen\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"
        if os.path.exists(win_path):
            pytesseract.pytesseract.tesseract_cmd = win_path
        else:
            raise FileNotFoundError("Tesseract not found at expected Windows path.")
    else:
        if which("tesseract") is None:
            raise EnvironmentError("Tesseract not found in PATH. Install using: sudo apt install tesseract-ocr")


# Configure Tesseract path once
_configure_tesseract_path()


def _get_rotation_angle(image: np.ndarray) -> int:
    try:
        rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        pil_image = Image.fromarray(rgb_image)
        osd = pytesseract.image_to_osd(pil_image)
        for line in osd.split('\n'):
            if 'Rotate:' in line:
                return int(line.split(":")[1].strip())
    except Exception as e:
        print("Tesseract OSD failed:", e)
    return 0


def _rotate_image(image: np.ndarray, angle: int) -> np.ndarray:
    if angle == 0:
        return image

    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    matrix = cv2.getRotationMatrix2D(center, -angle, 1.0)

    cos = np.abs(matrix[0, 0])
    sin = np.abs(matrix[0, 1])
    new_w = int((h * sin) + (w * cos))
    new_h = int((h * cos) + (w * sin))

    matrix[0, 2] += (new_w / 2) - center[0]
    matrix[1, 2] += (new_h / 2) - center[1]

    return cv2.warpAffine(image, matrix, (new_w, new_h),
                          flags=cv2.INTER_CUBIC,
                          borderMode=cv2.BORDER_REPLICATE)


def _deskew_image(image: np.ndarray, max_skew: float = 90.0) -> np.ndarray:
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
    _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    binary = cv2.bitwise_not(binary)

    angle = determine_skew(binary)

    if abs(angle) > max_skew:
        raise ValueError(f"Detected skew angle {angle:.2f}° exceeds max_skew limit of {max_skew}°.")

    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    matrix = cv2.getRotationMatrix2D(center, angle, 1.0)

    cos = np.abs(matrix[0, 0])
    sin = np.abs(matrix[0, 1])
    new_w = int((h * sin) + (w * cos))
    new_h = int((h * cos) + (w * sin))

    matrix[0, 2] += (new_w / 2) - center[0]
    matrix[1, 2] += (new_h / 2) - center[1]

    return cv2.warpAffine(image, matrix, (new_w, new_h),
                          flags=cv2.INTER_LINEAR,
                          borderMode=cv2.BORDER_REPLICATE)


def fix_orientation_and_deskew(image: np.ndarray, max_skew: float = 90.0) -> np.ndarray:
    angle = _get_rotation_angle(image)
    rotated = _rotate_image(image, angle)
    deskewed = _deskew_image(rotated, max_skew=max_skew)
    return deskewed


if __name__ == "__main__":
    img_path = r"C:\Users\Wissen\Downloads\image (3).png"
    img = cv2.imread(img_path)

    if img is None:
        raise FileNotFoundError(f"Image not found at path: {img_path}")

    out_img = fix_orientation_and_deskew(img)
    cv2.imwrite("out_img_fixed.png", out_img)
    print("Image processing complete. Saved as 'out_img_fixed.png'.")


import subprocess

cert_file = "certis3.crt"

try:
    result = subprocess.run(
        ["openssl", "x509", "-in", cert_file, "-text", "-noout"],
        capture_output=True,
        text=True,
        check=True
    )
    print("Certificate Details:\n")
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print("Error running openssl command:")
    print(e.stderr)
except FileNotFoundError:
    print("OpenSSL executable not found. Ensure it's installed and in PATH.")