{"record":{"id":"3ceb7bb0774b8e62","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"image-format-not-jpeg-3ceb7b","errorCode":null,"errorMessage":"Image '{}' format not JPEG","messagePattern":"Image '(.+?)' format not JPEG","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/retinaNet/my_dataset.py","lineNumber":56,"sourceCode":"        with open(json_file, 'r') as f:\n            self.class_dict = json.load(f)\n\n        self.transforms = transforms\n\n    def __len__(self):\n        return len(self.xml_list)\n\n    def __getitem__(self, idx):\n        # read xml\n        xml_path = self.xml_list[idx]\n        with open(xml_path) as fid:\n            xml_str = fid.read()\n        xml = etree.fromstring(xml_str)\n        data = self.parse_xml_to_dict(xml)[\"annotation\"]\n        img_path = os.path.join(self.img_root, data[\"filename\"])\n        image = Image.open(img_path)\n        if image.format != \"JPEG\":\n            raise ValueError(\"Image '{}' format not JPEG\".format(img_path))\n\n        boxes = []\n        labels = []\n        iscrowd = []\n        assert \"object\" in data, \"{} lack of object information.\".format(xml_path)\n        for obj in data[\"object\"]:\n            xmin = float(obj[\"bndbox\"][\"xmin\"])\n            xmax = float(obj[\"bndbox\"][\"xmax\"])\n            ymin = float(obj[\"bndbox\"][\"ymin\"])\n            ymax = float(obj[\"bndbox\"][\"ymax\"])\n            boxes.append([xmin, ymin, xmax, ymax])\n            labels.append(self.class_dict[obj[\"name\"]])\n            if \"difficult\" in obj:\n                iscrowd.append(int(obj[\"difficult\"]))\n            else:\n                iscrowd.append(0)\n\n        # convert everything into a torch.Tensor","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/retinaNet/my_dataset.py#L38-L74","documentation":"VOCDataSet.__getitem__ opens each image referenced by a VOC XML annotation and requires its PIL image.format to be 'JPEG'. The dataset assumes VOC-style JPEG images; other formats (PNG, BMP, GIF) cannot pass the standard VOC torchvision-style pipeline (and later transforms may fail on non-RGB data). It throws a ValueError naming the offending image path.","triggerScenarios":"Iterating the dataset (DataLoader over VOCDataSet) when the file at os.path.join(self.img_root, data['filename']) is not a JPEG: e.g. a PNG renamed to .jpg, a grayscale/CMYK image still saved as PNG, or a mixed-format directory indexed by train.txt.","commonSituations":"Downloading images from the web where extensions lie; converting a dataset with scripts that copy files without re-encoding; VOC2007/VOC2012 datasets containing some PNG segmentation images mixed into JPEGImages; re-labeling data with annotation tools that save PNG.","solutions":["Re-encode the offending image(s) to JPEG with PIL: Image.open(p).convert('RGB').save(p, 'JPEG').","Batch-convert all non-JPEG files in the dataset directory before training (script over the img_root checking image.format).","If PNG support is needed, relax the check and convert on load instead of raising: image = Image.open(img_path).convert('RGB').","Fix filename/annotation mismatches: ensure data['filename'] in the XML points at the real JPEG file."],"exampleFix":"// before\nimage = Image.open(img_path)\nif image.format != \"JPEG\":\n    raise ValueError(\"Image '{}' format not JPEG\".format(img_path))\n// after\nimage = Image.open(img_path)\nif image.format != \"JPEG\":\n    image = image.convert(\"RGB\")  # tolerate non-JPEG input","handlingStrategy":"validation","validationCode":"from PIL import Image\nfor name in os.listdir(img_root):\n    with Image.open(os.path.join(img_root, name)) as im:\n        if im.format != \"JPEG\":\n            print(\"non-JPEG:\", name, im.format)","typeGuard":"def is_jpeg(path: str) -> bool:\n    from PIL import Image\n    try:\n        with Image.open(path) as im:\n            return im.format == \"JPEG\"\n    except Exception:\n        return False","tryCatchPattern":"try:\n    sample = dataset[i]\nexcept ValueError as e:\n    if \"format not JPEG\" in str(e):\n        img = Image.open(extract_path(e)).convert(\"RGB\")\n        img.save(extract_path(e), \"JPEG\")\n    else:\n        raise","preventionTips":["Batch-verify image formats right after downloading a dataset.","Never rely on file extensions; check actual image.format.","Re-encode through PIL/convert('RGB') during dataset preparation.","Keep annotation filenames in sync with actual image files."],"tags":["python","pytorch","dataset","image-format"],"backgroundTag":"unsupported-image-format","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}