{"record":{"id":"80b4273993a477b6","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"image-format-not-jpeg","errorCode":null,"errorMessage":"Image '{}' format not JPEG","messagePattern":"Image '(.+?)' format not JPEG","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/faster_rcnn/my_dataset.py","lineNumber":72,"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\n            # 进一步检查数据，有的标注信息中可能有w或h为0的情况，这样的数据会导致计算回归loss为nan\n            if xmax <= xmin or ymax <= ymin:\n                print(\"Warning: in '{}' xml, there are some bbox w/h <=0\".format(xml_path))\n                continue\n            \n            boxes.append([xmin, ymin, xmax, ymax])\n            labels.append(self.class_dict[obj[\"name\"]])","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/faster_rcnn/my_dataset.py#L54-L90","documentation":"VOCDataSet.__getitem__ opens the image named in the annotation XML and requires its format to be exactly 'JPEG' (PIL's format attribute). Any other format — PNG, BMP, GIF, MPO, or a JPEG whose PIL format reads differently — raises ValueError with the image path. The code assumes a standard VOC2012 dataset where all images are JPEG.","triggerScenarios":"The XML annotation's <filename> points to a non-JPEG file (e.g. image.png/image.bmp), a file with a wrong/misleading name, or a corrupted image PIL cannot decode as JPEG.","commonSituations":"Mixing a custom dataset into VOC layout, images converted/re-encoded with wrong extensions, downloading images that are PNG but renamed .jpg, or camera images with MPO format.","solutions":["Convert all dataset images to JPEG: e.g. `for f in *.png; do convert $f ${f%.png}.jpg; done` and update the XML filenames.","Check data['filename'] in the failing XML matches an actual .jpg file with JPEG content.","Re-encode with PIL: Image.open(p).convert('RGB').save(p, 'JPEG').","Remove or fix non-conforming samples from the XML/ImageSets lists.","Relax the check (image.convert('RGB')) if you intentionally support other formats."],"exampleFix":"# before\nimage = Image.open(img_path)\nif image.format != \"JPEG\":\n    raise ValueError(...)\n# after\nimage = Image.open(img_path)\nif image.format != \"JPEG\":\n    image = image.convert(\"RGB\")  # tolerate PNG/BMP inputs","handlingStrategy":"validation","validationCode":"from PIL import Image\nimg_path = os.path.join(self.img_root, data['filename'])\nwith Image.open(img_path) as im:\n    assert im.format == 'JPEG', f\"{img_path} is {im.format}, convert to JPEG\"","typeGuard":"def is_jpeg(path: str) -> bool:\n    try:\n        with Image.open(path) as im:\n            return im.format == 'JPEG'\n    except Exception:\n        return False","tryCatchPattern":"try:\n    image, target = dataset[i]\nexcept ValueError as e:\n    print(f'Skipping non-JPEG sample: {e}')\n    continue","preventionTips":["Audit dataset images for PNG/BMP files renamed .jpg","Re-encode all images with PIL to JPEG during dataset preparation","Keep XML <filename> entries in sync with actual files","Convert to RGB in __getitem__ if you must tolerate other formats"],"tags":["dataset","image-format","pil","voc"],"backgroundTag":"invalid-image-format","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}