Skip to content

Unable to determine image type for JPG image #12111

@samuelrobyr

Description

@samuelrobyr

Describe the bug
Medusa v1.0.25 does not properly retrieve JPEG image size on some files.

To Reproduce
Steps to reproduce the behavior:

  1. Add the attached image (banner.jpg) to a show
  2. Open the show page
  3. Click on "Re-scan files"
  4. WARNING will appear in the log
2025-11-14 18:27:50 WARNING	SHOWQUEUE-REFRESH :: [27e1376] Unable to determine image type for /path/to/Sheriff.Country/banner.jpg
2025-11-14 18:27:50 WARNING	SHOWQUEUE-REFRESH :: [27e1376] Aspect ratio (31.142857142857142) does not match any known types.

After some digging, I found two bugs:

BUG 1: The function get_image_size(image_path) in medusa/helpers/init.py does not properly parse the JPEG. After finding the SOF (Start of Frame) marker, the code incorrectly skips bytes, causing it to read the segment length field as part of the image dimensions:

# JPEG check
elif head.startswith(b'\xff\xd8') or img_ext in ('jpg', 'jpeg'):
    f.seek(0)
    size = 2
    ftype = 0
    while True:
        f.seek(size, 1)
        byte = f.read(1)
        if not byte:
            return None  # EOF reached unexpectedly
        while ord(byte) == 0xff:
            byte = f.read(1)
        ftype = ord(byte)
        if 0xc0 <= ftype <= 0xcf and ftype != 0xc4 and ftype != 0xc8 and ftype != 0xcc:
            break # Found SOF marker
        size_bytes = f.read(2)
        if len(size_bytes) != 2:
            return None
        size = struct.unpack('>H', size_bytes)[0] - 2
    
    # --- THIS IS THE FIX ---
    # We are currently positioned after the SOF marker (e.g., 0xC0).
    # We must skip the 2-byte segment length and 1-byte precision.
    f.seek(3, 1)  # Skip 3 bytes (Length[2] + Precision[1])
    
    # Now we can read the height and width
    height, width = struct.unpack('>HH', f.read(4))
    return width, height

BUG 2: The function def which_type(path) in /medusa/image_cache.py invert height and width.

image_dimension = get_image_size(path)
if not image_dimension:
    log.debug('Skipping image. Unable to get metadata from {0}', path)
    return

# --- THIS IS THE FIX ---
# get_image_size returns (width, height), not (height, width)
width, height = image_dimension 

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions