首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OSError:图像文件被截断

OSError:图像文件被截断
EN

Stack Overflow用户
提问于 2020-03-08 11:06:03
回答 1查看 3.1K关注 0票数 2

当我处理一堆图像时,在其中一个图像上会出现以下错误

代码语言:javascript
复制
File "/home/tensorflowpython/firstmodel/yololoss.py", line 153, in data_generator
    image, box = get_random_data(annotation_lines[i], input_shape, random=True)
  File "/home/tensorflowpython/firstmodel/yololoss.py", line 226, in get_random_data
    image = image.resize((nw,nh), Image.BICUBIC)
  File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/Image.py", line 1858, in resize
    self.load()
  File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/ImageFile.py", line 247, in load
    "(%d bytes not processed)" % len(b)
OSError: image file is truncated (25 bytes not processed)

我已经尝试了here建议的解决方案,但它不起作用

我的代码如下所示

代码语言:javascript
复制
from PIL import Image

def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True):
    Image.LOAD_TRUNCATED_IMAGES = True    
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
    try:
       image.load()
    except IOError:
        pass # You can always log it to logger
    if not random:
        # resize image
        scale = min(w/iw, h/ih)
        nw = int(iw*scale)
        nh = int(ih*scale)
        dx = (w-nw)//2
        dy = (h-nh)//2
        image_data=0
        if proc_img:
            image = image.resize((nw,nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w,h), (128,128,128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image)/255.

        # correct boxes
        box_data = np.zeros((max_boxes,5))
        if len(box)>0:
            np.random.shuffle(box)
            if len(box)>max_boxes: box = box[:max_boxes]
            box[:, [0,2]] = box[:, [0,2]]*scale + dx
            box[:, [1,3]] = box[:, [1,3]]*scale + dy
            box_data[:len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale*h)
        nw = int(nh*new_ar)
    else:
        nw = int(scale*w)
        nh = int(nw/new_ar)

    image = image.resize((nw,nh), Image.BICUBIC) #error occurs here

我的错误与之前的解决方案之间的区别是,我的错误是操作系统错误,而解决方案是IO错误

编辑:我已经找出了导致这个错误的镜像,可以从这个link下载

EN

回答 1

Stack Overflow用户

发布于 2020-03-08 15:21:27

我尝试了你链接到截断图像的解决方案,它起作用了。您在尝试应用此解决方案时犯了一个小错误:您必须设置ImageFile.LOAD_TRUNCATED_IMAGES=True,而不是Image.LOAD_TRUNCATED_IMAGES

LOAD_TRUNCATED_IMAGES最初并不存在于Image模块中,因此当您执行Image.LOAD_TRUNCATED_IMAGES=True时,您设置了一个库不使用的新变量。

所以我认为你只需要这样做:

代码语言:javascript
复制
from PIL import ImageFile, Image
ImageFile.LOAD_TRUNCATED_IMAGES = True

image = Image.open("00090.jpg")
# resize now doesn't fail
image.resize((h, w), Image.BICUBIC)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60584155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档