Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在内存中保存文件?_Python_Python Imaging Library_Bytesio - Fatal编程技术网

Python 在内存中保存文件?

Python 在内存中保存文件?,python,python-imaging-library,bytesio,Python,Python Imaging Library,Bytesio,我只想打开文件夹中的图像文件,并将它们转换为jpeg,如果它们还不是jpeg。唯一的问题是我需要保存在内存中的文件,而不是文件。原因是,事实上,我正在从tfrecod文件tensorflow数据文件格式读取图像,从中提取图像,检查文件格式,如果不是jpeg,则转换为jpeg,然后在正确解码后写回tfrecord文件。因为tensorflow对象检测api不接受jpeg以外的任何图像格式。不管怎样,这就是我为什么需要它的原因 要做到这一点,我需要将文件保存在内存中。这是我的代码: 对于计数器,枚举

我只想打开文件夹中的图像文件,并将它们转换为jpeg,如果它们还不是jpeg。唯一的问题是我需要保存在内存中的文件,而不是文件。原因是,事实上,我正在从tfrecod文件tensorflow数据文件格式读取图像,从中提取图像,检查文件格式,如果不是jpeg,则转换为jpeg,然后在正确解码后写回tfrecord文件。因为tensorflow对象检测api不接受jpeg以外的任何图像格式。不管怎样,这就是我为什么需要它的原因

要做到这一点,我需要将文件保存在内存中。这是我的代码:

对于计数器,枚举文件名中带有路径的文件名\u: e=nextitertf.data.TFRecordDataset[文件名\u和路径] 示例=tf.train.example example.ParseFromStringe.numpy parsed=example.features.feature image_raw=parsed['image/encoded'].bytes_list.value[0] 这一点很重要 流=字节图像\u原始 image=image.openstream图像是枕头图像 关闭 如果image.format!='JPEG': tempFile=BytesIO 图像。转换为“RGB” image.savetempFile,格式=JPEG newStream=BytesIOtempFile img=Image.opennewStream newStream.close printfilename,image.format 打印文件名,img.format 当我运行此操作时,我得到ValueError:对关闭的文件执行I/O操作。在线

image.savetempFile,格式=JPEG 知道为什么会出现错误吗?我认为这是写入内存文件的建议方式:

错误不是关于tempFile,而是关于stream。在处理完图像之前,不应执行stream.close。这是一个懒惰的API,因此它可以更有效地处理大型图像

for counter, filename_with_path in enumerate(filenames):
    ...

    stream = BytesIO(image_raw)
    image = Image.open(stream) # Image is pillow image
    # remove this line:
    # stream.close()

    if image.format != 'JPEG':
        tempFile = BytesIO()
        image.convert('RGB')
        image.save(tempFile, format="JPEG")

        # this wants bytes, not another BytesIO object, so read it
        newStream = BytesIO(tempFile.read())
        img = Image.open(newStream)
        # same thing, don't close until you are done with img
        # newStream.close()

        print(filename, image.format)
        print(filename, img.format)
从枕头上:

这是一个懒惰的操作;此函数用于标识文件,但在您尝试处理数据或调用load方法之前,文件将保持打开状态,并且不会从文件中读取实际图像数据

错误不是关于tempFile,而是关于stream。在处理完图像之前,不应执行stream.close。这是一个懒惰的API,因此它可以更有效地处理大型图像

for counter, filename_with_path in enumerate(filenames):
    ...

    stream = BytesIO(image_raw)
    image = Image.open(stream) # Image is pillow image
    # remove this line:
    # stream.close()

    if image.format != 'JPEG':
        tempFile = BytesIO()
        image.convert('RGB')
        image.save(tempFile, format="JPEG")

        # this wants bytes, not another BytesIO object, so read it
        newStream = BytesIO(tempFile.read())
        img = Image.open(newStream)
        # same thing, don't close until you are done with img
        # newStream.close()

        print(filename, image.format)
        print(filename, img.format)
从枕头上:

这是一个懒惰的操作;此函数用于标识文件,但在您尝试处理数据或调用load方法之前,文件将保持打开状态,并且不会从文件中读取实际图像数据


我相信建议如何编写内存文件的链接答案是错误的。我建议你编辑你的问题,去掉tensorflow和Tfrecd文件的内容——这与你真正想问的没有任何关系——并提供一个其他人可以轻松运行和修改的文件,向你展示如何正确地完成它。你是否尝试过用一个带open“…”的,“w”作为f:我认为建议如何编写内存文件的链接答案是错误的。我建议你编辑你的问题,去掉tensorflow和Tfrecd文件的内容——这与你真正想问的没有任何关系——并提供一个其他人可以轻松运行和修改的文件,向你展示如何正确地完成它。你是否尝试过用一个带open“…”的,“w”as f:总体而言,我认为延迟加载功能造成的心痛比它在I/O中保存的要多。总体而言,我认为延迟加载功能造成的心痛比它在I/O中保存的要多。