Cause Analysis
When we use PIL to process image data. If some of the images in our dataset are partially damaged or cannot be opened directly, some errors will occur. For example, this error OSError: image file is truncated, or PIL.UnidentifiedImageError: cannot identify image file.
————————————————————————————————————————
solution
1. Manually replace the damaged image
This method is suitable for data sets with a small amount of damaged data
2. Throw away useless data directly
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
The use of this method means that when encountering a picture with data truncated, PIL will directly break, jump out of the function, and proceed to the next one without reporting an error. This method works for error OSError: image file is truncated
3. Use try except to skip this exception
try:
real_input = Image.open(img_dir).convert('RGB')
input_images = self.img_transform(real_input)
input_images = input_images[2, :, :]
input_images = np.expand_dims(input_images, axis=0)
bufImg = Image.open(gt_dir).convert('RGB')
dilated_bufImg = self.img_transform(bufImg)
dilated_bufImg = dilated_bufImg[2, :, :]
output_images = np.expand_dims(dilated_bufImg, axis=0)
sample_info = {
}
sample_info['input_images'] = input_images
sample_info['output_images'] = output_images
return sample_info
except:
print('ERROR, Path: ', img_dir)
img_dir = os.path.join(self.imageset_dir, "004255_2.png")
real_input = Image.open(img_dir).convert('RGB')
input_images = self.img_transform(real_input)
input_images = input_images[2, :, :]
input_images = np.expand_dims(input_images, axis=0)
sample_info = {
}
sample_info['input_images'] = input_images
sample_info['output_images'] = input_images
return sample_info
I am here to replace the damaged picture with a normal picture and return it. If no value is returned, the batch will lack a piece of data, and a TypeError error will be reported later. This method works for PIL.UnidentifiedImageError: cannot identify image file