You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eynollah/qurator/eynollah/utils/pil_cv2.py

29 lines
816 B
Python

from PIL import Image
import numpy as np
from ocrd_models import OcrdExif
from cv2 import COLOR_GRAY2BGR, COLOR_RGB2BGR, cvtColor, imread
# from sbb_binarization
def cv2pil(img):
return Image.fromarray(img)
def pil2cv(img):
# from ocrd/workspace.py
color_conversion = COLOR_GRAY2BGR if img.mode in ('1', 'L') else COLOR_RGB2BGR
pil_as_np_array = np.array(img).astype('uint8') if img.mode == '1' else np.array(img)
return cvtColor(pil_as_np_array, color_conversion)
def check_dpi(img):
try:
exif = OcrdExif(cv2pil(img))
resolution = exif.resolution
if resolution == 1:
raise Exception()
if exif.resolutionUnit == 'cm':
resolution /= 2.54
return int(resolution)
except Exception as e:
print(e)
return 230