Ask Your Question
1

Image corrupted vs Image not found vs File format not supported

asked 2018-03-14 02:19:18 -0600

Santhosh1 gravatar image

updated 2018-03-14 02:39:15 -0600

Case 1 Image File Corrupted

Case 2Image File not found in the location

Case 3 Image file format not supported

How do I differentiate between these?

My OpenCV version 3.4.0

I used cv2.imread(filelocation)

When I tried to print the image after using imread

I got the same output None for all the three cases.

I would like to try catch the different scenarios so that I can know exactly where I have an error.

Any suggestion?

PS: By location I mean a url link. Without using another type of library in python.

edit retag flag offensive close merge delete

Comments

PS: imread cannot read from (web) urls at all. you can rule out that problem.

berak gravatar imageberak ( 2018-03-14 02:43:13 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-03-14 04:14:02 -0600

Santhosh1 gravatar image

updated 2018-03-14 07:11:31 -0600

@berak Couldn't add this in the comment

I found code few code online using urllib and requests, which solves problem number 2

url='https://'
re=requests.get(url)
print(re.status_code)
if re.status_code == 200:
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()),dtype="uint8")
    image = cv2.imdecode(image,cv2.IMREAD_COLOR)

Only part is how do I get the same None value for both corrupted as well as unsupported files that I use to get using imread

PS

imdecode return empty matrix/image if the buffer is short or contains invalid data hopefully None in python

imread checks if the image cannot be read, then return same as decode empty martix and None in python

How to I add this improper permissions, unsupported or invalid format using python on numpy array of the image I already have?

Or is it not required?

edit flag offensive delete link more

Comments

yes, that's what i would do, too. though:

Without using another type of library in python.

PS. iirc, this won't work anymore in python3, so careful !

berak gravatar imageberak ( 2018-03-14 04:20:34 -0600 )edit
1

I did check now, it does work fine. I'm using python 3.6 and OpenCV 3.4.0

Santhosh1 gravatar imageSanthosh1 ( 2018-03-14 04:36:06 -0600 )edit

but please, screenshots of text are bad here, and you're refering to outdated 2.4 documentation

berak gravatar imageberak ( 2018-03-14 04:47:37 -0600 )edit

Sure, I did check it, those are from version 3.0.0. I did check with a PDF file its giving NONE worried about corrupted files now

Santhosh1 gravatar imageSanthosh1 ( 2018-03-14 04:53:38 -0600 )edit

maybe just a link to current master docs will do ?

berak gravatar imageberak ( 2018-03-14 04:56:43 -0600 )edit

I can confirm, that imdecode is behaving the same way as imread behaves with the corrupted .jpg files and for .pdf for unsupported files. I'm getting None for both these cases

Santhosh1 gravatar imageSanthosh1 ( 2018-03-14 05:26:03 -0600 )edit

yes, true, it behaves quite similar to imread.

also, no support for pdf in opencv at all. (it is not an image format, anyway)

still, as long as you have a raw byte buffer, you could check file magic, or things like 0xffd8 / 0xfdb for jpg

can we remove the outdated / misleading screenshots ?

berak gravatar imageberak ( 2018-03-14 05:34:44 -0600 )edit
0

answered 2018-03-14 02:34:23 -0600

berak gravatar image

sad, but impossible with opencv means. all you can do is check for None in python (or empty() in c++)

you can do additional (manual) checks, like f = open(imgpath) for 2. or try to match the first bytes of the file against some 'file magic' list for 3. , but the real hard case is 1., since you'd need to know about the inner structure of the file format , you're trying to read.

the idea of try/catch won't work either, imho. you cannot "repair" a broken file on the fly.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-14 02:19:18 -0600

Seen: 4,171 times

Last updated: Mar 14 '18