0

I'm doing machine learning in Python with TensorFlow on image files and while reading a set of .jpg-images at some point I get the error:

OSError: image file is truncated (49 bytes not processed)

I've been googling for a definition of a truncated image file without finding a clear definition (many sources talk about truncated image file without first explaining what it is), so my simple question is: What is a truncated image file? Definition? What does it mean?

P.S. this post does NOT answer my question (it deals with handling truncated image error, not what it is as I understood):

OSError: image file is truncated

jjepsuomi
  • 4,223
  • 8
  • 46
  • 74
  • As far as I understand it means that the image is missing some data. – deceze Jun 09 '22 at 07:42
  • @deceze perfect, thank you for your help. If you can provide more info on what this missing data is / can be, then I could accept your comment as answer :-) – jjepsuomi Jun 09 '22 at 07:44
  • 2
    Well, it's just… missing. Cut off. Not there. *Truncated.* See the sample image linked in the other question (note the bottom right corner): https://github.com/experiencor/kangaroo/blob/master/images/00090.jpg – deceze Jun 09 '22 at 07:46
  • @deceze excellent, thank you. English is not my native language so it's not always unambiguous to what things means (sometimes a term can have multiple meanings) :). I was thinking like, can this missing info be the actual pixel data, header info or something else. But your example made it now 100% clear. So, truncated image file is, for example, an image with some pixel data missing. I can accept your answer if you repost it as an answer :) – jjepsuomi Jun 09 '22 at 07:53
  • If you would like us to tell you what specific kind of truncation your image is suffering, perhaps you could give us a sample image? Compression might be a cause, so please make sure to post the image you are trying to load without manipulating or compressing it further. – Decclo Jun 09 '22 at 07:59
  • Hi @Decclo thank you for your comment. I checked the image and it was like the example deceze gave. Pixels missing, almost 90% of them. But I was generally interested what does image truncation explicitly mean. – jjepsuomi Jun 09 '22 at 08:02
  • Have a look here... https://stackoverflow.com/a/68415308/2836621 – Mark Setchell Jun 09 '22 at 09:30

1 Answers1

0

I am currently trying to handle properly truncated images (for instance knowing what percentage of the image is truncated etc). For that I truncate images myself, so I think it can help to understand what a truncated image is:

import os
from PIL import Image, ImageOps, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = False # to be sure that truncated image raise an error

# In Jupyter notebook I use IPython for displaying images. 
# Keep in mind that IPython.display.display catches everything: 
# display but also exceptions and errors 
# so you cannot catch exceptions on a try: display
from IPython.display import display
 
# Display image
image = Image.open(image_path)
display(image)

##### Truncate the image to 80% of its size #####
# get info about image file size (in bytes)
image_file_size = os.path.getsize(image_path) 
print(f"image file size: {image_file_size}") 
# compute 80% of the image size (file version of the image, not pixels)
truncated_image_size = int(image_file_size * 0.8) 

# open image, as a file (not an image), in append mode, and truncate it
with open(image_path,"a") as image_file: 
    image_file.truncate(truncated_image_size)
################################################

# Display the truncated image 
image = Image.open(image_path)
try:
    image_bytes = image_truncated.tobytes() # Raise an error if image is truncated
except OSError as e:  
    print(f"exception catched: {e}")
    # The exception write a number of unread bytes, but which does not correspond to the actual number of missing pixels
    
    # Actually display truncated image
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    display(image)

    ##### Compute actual number of missing pixels #####
    img_as_pixels = image_truncated.getdata()      
    
    # Check which pixels are grey (missing values)
    grey_pixel_tuple = (128, 128, 128)
    is_grey_pixels = [True if pixel == grey_pixel_tuple else False for pixel in img_as_list]
    
    # Get the position of the last "not grey" pixel
    last_not_truncated_pixel = next(i for i in reversed(range(len(is_grey_pixels))) if is_grey_pixels[i] == False)

    # Compute the percentage of the image not missing
    percentage_of_available_image =  last_not_truncated_pixel / len(img_reshape)
    print(f"percentage_of_available_image: {percentage_of_available_image}")
        
    ImageFile.LOAD_TRUNCATED_IMAGES = False
    
Ken
  • 442
  • 5
  • 11