Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libpng error not catched #16

Closed
emicol opened this issue Mar 3, 2023 · 1 comment
Closed

libpng error not catched #16

emicol opened this issue Mar 3, 2023 · 1 comment

Comments

@emicol
Copy link

emicol commented Mar 3, 2023

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
Copy link
Owner

Thank you for directly providing this solution!
I added this function. Update with: pip install -U clustimage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants