Skip to content

Commit bd4c588

Browse files
committed
I04_1-1014: add blank image when image is none
1 parent 46f6e4d commit bd4c588

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

dls_util/image/image.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cv2 as opencv
22
import math
33
import numpy as np
4+
import logging
45

56
from PyQt5 import QtCore
67
from PyQt5.QtGui import QImage, QPixmap
@@ -13,6 +14,10 @@ class Image:
1314
"""
1415
def __init__(self, img):
1516
self.img = img
17+
if img is None:
18+
log = logging.getLogger(".".join([__name__]))
19+
log.info("image is none")
20+
self.img = np.full((1, 1, 3), 0.0, np.uint8)
1621

1722
size = self.img.shape
1823
self.width = size[1]
@@ -36,7 +41,13 @@ def is_valid(self):
3641
@staticmethod
3742
def from_file(filename):
3843
""" Return a new image by loading from the specified image file. """
39-
img = opencv.imread(filename, opencv.IMREAD_UNCHANGED)
44+
try:
45+
img = opencv.imread(filename, opencv.IMREAD_UNCHANGED)
46+
except:
47+
log = logging.getLogger(".".join([__name__]))
48+
log.info("check file path")
49+
img = None
50+
4051
return Image(img)
4152

4253
@staticmethod

tests/unit_tests/test_dls_util/test_image/test_image.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,14 @@ def test_sub_image_is_created_if_the_center_is_outside_but_the_radius_ovelaps_wi
232232
self.assertEqual(width, 1)
233233
self.assertEqual(height, 1)
234234

235-
#def test_calculate_brightness
235+
def test_empty_image_returned_when_image_is_none(self):
236+
empty_image = Image(None)
237+
self.assertEqual(empty_image.width, 1)
238+
self.assertEqual(empty_image.height, 1)
239+
240+
def test_from_file_returns_none_no_image_found_in_the_path(self):
241+
wrong_file_path = "wrong_file.jpg"
242+
img = Image.from_file(wrong_file_path)
243+
244+
self.assertEqual(img.width, 1)
245+

0 commit comments

Comments
 (0)