libpng error not catched
emicol opened this issue · 1 comments
emicol commented
When a file is corrupted, libpng generates an error and it is not caught.
See for example https://stackoverflow.com/questions/46683264/libpng-error-read-error-by-using-open-cv-imread or https://stackoverflow.com/questions/70598227/libpng-error-read-error-why-cant-this-image-be-processed-by-opencv
Following the advice found on the second link, I rewrote this part (maybe In case of rgb images: make gray images compatible with RGB is unnecessary):
`
%% Read image
def _imread(filepath, colorscale=1):
"""Read image from filepath using colour-scaling.
Parameters
----------
filepath : str
path to file.
colorscale : int, default: 1 (gray)
colour-scaling from opencv.
* 0: cv2.IMREAD_GRAYSCALE
* 1: cv2.IMREAD_COLOR
* 8: cv2.COLOR_GRAY2RGB
Returns
-------
img : numpy array
raw rgb or gray image.
"""
img=None
# if os.path.isfile(filepath):
# Read the image
# img = cv2.imread(, colorscale)
img = Image.open(filepath)
# Convert Image to numpy array
# It's not the most efficient way, but it works. *(link¹)
img = np.asarray(img)
# Remove alpha channel if existent
if len(img.shape) == 3 and img.shape[2] == 4:
img = img[:, :, : 3]
# Restore RGB colors
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# In case of rgb images: make gray images compatible with RGB
if ((colorscale!=0) and (colorscale!=6)) and (len(img.shape)<3):
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
# else:
# logger.warning('File does not exists: %s', filepath)
return cv2.cvtColor(img, colorscale)`
erdogant commented
Thank you for directly providing this solution!
I added this function. Update with: pip install -U clustimage