Browse Source

CLI fails to check if image saved correctly #12

* add verification after writing an image file
master
PommeDroid 3 years ago
parent
commit
a83d95a8e7
  1. 39
      utils.py

39
utils.py

@ -7,6 +7,8 @@ import coloredlogs @@ -7,6 +7,8 @@ import coloredlogs
import cv2
import numpy as np
import rook
from PIL import Image
from config import Config as conf
def read_image(path):
@ -22,8 +24,7 @@ def read_image(path): @@ -22,8 +24,7 @@ def read_image(path):
image = cv2.imdecode(np_image, cv2.IMREAD_COLOR)
# See if image loaded correctly
if image is None:
print("Error : {} file is not valid image".format(
path), file=sys.stderr)
conf.log.error("{} file is not valid image".format(path))
sys.exit(1)
return image
@ -36,6 +37,10 @@ def write_image(image, path): @@ -36,6 +37,10 @@ def write_image(image, path):
:return: <RGB> None
"""
cv2.imwrite(path, image)
if not check_image_file_validity(path):
conf.log.error(
"Something gone wrong writing {} image file. The final result is not a valid image file.".format(path))
sys.exit(1)
def check_shape(image, shape=(512, 512, 3)):
@ -46,11 +51,27 @@ def check_shape(image, shape=(512, 512, 3)): @@ -46,11 +51,27 @@ def check_shape(image, shape=(512, 512, 3)):
:return: None
"""
if image.shape != shape:
print("Error : image is not 512 x 512, got shape: {}".format(
image.shape), file=sys.stderr)
conf.log.error("Image is not 512 x 512, got shape: {}".format(image.shape))
sys.exit(1)
def check_image_file_validity(image_path):
"""
Check is a file is valid image file
:param image_path: <string> Path to the file to check
:return: <Boolean> True if it's an image file
"""
im, r = None, True
try:
im = Image.open(image_path)
im.verify()
except Exception:
r = False
finally:
if im:
im.close()
return r
def start_rook():
"""
@ -64,11 +85,21 @@ def start_rook(): @@ -64,11 +85,21 @@ def start_rook():
def setup_log(log_lvl=logging.INFO):
"""
Setup a logger
:param log_lvl: <loggin.LVL> level of the log
:return: <Logger> a logger
"""
log = logging.getLogger(__name__)
coloredlogs.install(level=log_lvl, fmt='[%(levelname)s] %(message)s')
return log
def camel_case_to_str(identifier):
"""
Return the string representation of a Camel case word
:param identifier: <string> camel case word
:return: a string representation
"""
matches = finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
return " ".join([m.group(0) for m in matches])

Loading…
Cancel
Save