You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
imghdr doesn't support jpegs that include an ICC Profile.
This is because imghdr looks for "JFIF" somewhere at the beginning of the file, but the ICC_PROFILE shifts that further.
(The ICC spec is here http://www.color.org/specification/ICC1v43_2010-12.pdf, annex B)
Not necessary, 2.7 only gets bugs fixes.
OTOH it would be nice to have some tests for this new features (and for the module in general), but there doesn't seem to be any Lib/test/test_imghdr.py file. The module itself seems to contain some kind of tests at the end though.
It looks like the test just walks a directory recursively while trying to identify its files, there's no "classic" test of the "this is a JPEG, is it detected correctly"-type
bpo-21230 reports a parallel problem with recognizing photoshop images.
We need a patch with tests covering the variant types we know about. I don't have a strong opinion on the simple two byte test versus the more complex test in msg220346, but following 'file' makes sense to me.
changed the title [-]imghdr doesn't support jpegs with an ICC profile[/-][+]imghdr doesn't recognize variant jpeg formats[/+]on Jun 13, 2014
mvignali commented on Jun 21, 2014
mvignalimannequin
Mannequin
More actions
I'm okay with just testing the first two bytes, it's the method we currently use for our
internal tools.
But maybe it can be interesting, to add another test, in order to detect incomplete file
(created when a camera make a recording error for example, and very useful to detect, because an incomplete jpeg file, make a crash for a lot of application)
We use this patch of imghdr :
--------------------------------------
deftest_jpeg(h, f):
"""JPEG data in JFIF or Exif format"""ifnoth.startswith(b'\xff\xd8'):#Test empty files, and incorrect start of filereturnNoneelse:
iff:#if we test a file, test end of jpegf.seek(-2,2)
iff.read(2).endswith(b'\xff\xd9'):
return'jpeg'else:#if we just test the header, consider this is a valid jpeg and not test end of filereturn'jpeg'
kovid commented on Jun 22, 2014
kovidmannequin
Mannequin
More actions
You cannot assume the file like object passed to imghdr is seekable. And IMO it is not the job of imghdr to check file validity, especially since it does not do that for all formats.
The imghdr module is now deprecated following the acceptance of PEP 594 by the Steering Council. Bugfixes and improvements to the module are therefore now considered to be very low priority. Given the large backlog of issues in the CPython repo, and the fact that this module has no active maintainer in the core dev team, I am therefore closing this issue as per the policy laid out in the dev guide.
Activity
joril commentedon Nov 20, 2012
imghdr doesn't support jpegs that include an ICC Profile.
This is because imghdr looks for "JFIF" somewhere at the beginning of the file, but the ICC_PROFILE shifts that further.
(The ICC spec is here http://www.color.org/specification/ICC1v43_2010-12.pdf, annex B)
ezio-melotti commentedon Nov 20, 2012
Can you provide a patch?
joril commentedon Nov 20, 2012
I can try, yes. I'll add one ASAP
joril commentedon Nov 20, 2012
Here it is... It is against the latest hg version, should I write one for 2.7 too?
ezio-melotti commentedon Nov 20, 2012
Thanks for the patch.
Not necessary, 2.7 only gets bugs fixes.
OTOH it would be nice to have some tests for this new features (and for the module in general), but there doesn't seem to be any Lib/test/test_imghdr.py file. The module itself seems to contain some kind of tests at the end though.
joril commentedon Nov 21, 2012
It looks like the test just walks a directory recursively while trying to identify its files, there's no "classic" test of the "this is a JPEG, is it detected correctly"-type
kovid commentedon Mar 20, 2013
The attached patch is insufficient, for example, it fails on http://nationalpostnews.files.wordpress.com/2013/03/budget.jpeg?w=300&h=1571
Note that the linux file utility identifies a files as "JPEG Image data" if the first two bytes of the file are \xff\xd8.
A slightly stricter test that catches more jpeg files:
intgr commentedon Sep 18, 2013
I vote we forget about JFIF/Exif headers and only use \xff\xd8 to identify the file. They are optional and there are tons of files out in the wild without such headers, for example: https://coverartarchive.org/release/5044b557-a9ed-4a74-b763-e20580ced85d/3354872309.jpg
Proposed patch at https://bitbucket.org/intgr/cpython/commits/012cde305316e22a999d674a0a009200d3e76fdb
PCManticore commentedon Jun 12, 2014
Using \xff\xd8 sounds good to me.
kovid commentedon Jun 12, 2014
FYI, the test I currently use in calibre, which has not failed so far for millions of users:
bitdancer commentedon Jun 13, 2014
bpo-21230 reports a parallel problem with recognizing photoshop images.
We need a patch with tests covering the variant types we know about. I don't have a strong opinion on the simple two byte test versus the more complex test in msg220346, but following 'file' makes sense to me.
[-]imghdr doesn't support jpegs with an ICC profile[/-][+]imghdr doesn't recognize variant jpeg formats[/+]mvignali commentedon Jun 21, 2014
I'm okay with just testing the first two bytes, it's the method we currently use for our
internal tools.
But maybe it can be interesting, to add another test, in order to detect incomplete file
(created when a camera make a recording error for example, and very useful to detect, because an incomplete jpeg file, make a crash for a lot of application)
We use this patch of imghdr :
--------------------------------------
kovid commentedon Jun 22, 2014
You cannot assume the file like object passed to imghdr is seekable. And IMO it is not the job of imghdr to check file validity, especially since it does not do that for all formats.
AlexWaygood commentedon Apr 12, 2022
@jcea, @bitdancer, @intgr, @PCManticore
The imghdr module is now deprecated following the acceptance of PEP 594 by the Steering Council. Bugfixes and improvements to the module are therefore now considered to be very low priority. Given the large backlog of issues in the CPython repo, and the fact that this module has no active maintainer in the core dev team, I am therefore closing this issue as per the policy laid out in the dev guide.