Skip to content

Handle jpeg with COM segment #688

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged

Handle jpeg with COM segment #688

merged 1 commit into from
Jul 1, 2025

Conversation

KeremTurgutlu
Copy link
Contributor

@KeremTurgutlu KeremTurgutlu commented Jun 30, 2025

Problem Summary

Issue: imghdr.what() fails to detect valid JPEG images generated by video processing tools (like ffmpeg/libavcodec), causing _is_img() to return False even though PIL can read the images perfectly.

Root Cause: These programmatically generated JPEGs have a COM (comment) segment (0xFFFE) immediately after the SOI marker (0xFFD8), containing encoder metadata like "Lavc61.19.101". The existing test_jpeg function only checks for specific 4-byte patterns that don't include 0xFFD8FFFE.

Proposed Solution

Add b'\xff\xd8\xff\xfe' to the existing pattern list in test_jpeg.

Generalization Assessment

This change is appropriately generic and would help many cases:

  1. Common pattern: Video-to-image extraction tools (ffmpeg, OpenCV, etc.) routinely add COM segments
  2. Standards compliant: COM segments are valid JPEG markers per the JPEG specification
  3. Low risk: Adding this pattern won't cause false positives since 0xFFFE is a reserved JPEG marker

@KeremTurgutlu KeremTurgutlu requested a review from jph00 June 30, 2025 15:53
@jph00 jph00 merged commit afe7005 into main Jul 1, 2025
14 checks passed
@jph00
Copy link
Contributor

jph00 commented Jul 1, 2025

Thanks @KeremTurgutlu !

@jph00 jph00 added the enhancement New feature or request label Jul 1, 2025
@jph00 jph00 changed the title test jpeg with COM segment Handle jpeg with COM segment Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants