简体   繁体   English

如何使用python将以下代码中生成的所有图像保存到Zip文件中?

[英]How can I save all the generated images from the following code to a Zip file using python?

I need to decode the data in the content_arrays list and generate an image , The following code does that 我需要解码content_arrays列表中的数据并生成一个图像,以下代码可以做到这一点

    content_arrays = ['ljfdslkfjaslkfjsdlf' , 'sdfasfsdfsdfsafs'] // Contains a list of base64 encoded data
    i=0
    for content in content_arrays:
        img_data = (content_arrays[i])
        with open(filename, "wb") as fh:
            fh.write(base64.b64decode(img_data))
        i=i+1

How can I store all the generated images directly to a single zip file which contains all the images that are generated by decoding the base64 string from the above list[content_arrays]. 如何将所有生成的图像直接存储到单个zip文件中,该文件包含通过解码上述列表[content_arrays]中的base64字符串生成的所有图像。

Current File Structure of the downloaded data :: 下载数据的当前文件结构::

 -- Desktop 
     -- image1.png
     -- image2.png

Required File Structure of the downloaded data :: 下载数据的必需文件结构::

 -- Desktop
     -- Data.zip 
        -- image1.png
        -- image2.png

I've used python zipfile module , but couldn't figure out things. 我使用了python zipfile模块,但无法弄清楚。 If there is any possible way , please do give your suggestions .. 如果有任何可能的方法,请提出您的建议。

In your case, you can iterate over the list of filenames 就您而言,您可以遍历文件名列表

with ZipFile('images.zip', 'w') as zip_obj:
   # Add multiple files to the zip
   for filename in filenames:
       zip_obj.write(filename)

you can just use the zipfile module and then write the content to separate files in the zip. 您可以只使用zipfile模块,然后将内容写入zip中的单独文件。 In this example i am just writing the content to a file inside the zip for each item in contents list. 在此示例中,我只是将内容写入目录列表中每个项目的zip文件中。 I am also using the writestr method here so i dont have to have physical files on the disk i can just create my content in memory and write it in my zip rather then having to first create it as a file on the OS and then write the file in the zip 我也在这里使用writestr方法,因此我不必在磁盘上拥有物理文件,我只需在内存中创建内容并将其写入zip中即可,而不必先在OS上将其创建为文件,然后将其写入压缩文件

from zipfile import ZipFile

with ZipFile("data.zip", "w") as my_zip:
    content_arrays = ['ljfdslkfjaslkfjsdlf', 'sdfasfsdfsdfsafs']
    for index, content in enumerate(content_arrays):
        #do what ever you need to do here with your content
        my_zip.writestr(f'file_{index}.txt', content)

OUTPUT 输出值 在此处输入图片说明

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个生成多个图像的 python 代码,有没有办法将生成的所有图像保存到 pdf 文件中? - I have a python code that generates multiple images, is there a way to save all the images that are generated to a pdf file? 我如何将所有从 jpg 图像生成的哈希保存到 csv 文件中,而不仅仅是最后一个? - How could i save all the hashes generated from jpg images into a csv file and not only the last one? 如何将 python 文件中的数据保存到文本文件中? 以下是我的代码行 - How can I save my data in python file to a text file? Following are my lines of code 如何使用python从一个zip中的URL保存多个文件? - How to save multiple file from URLs in one zip using python? 我试图将从所有图像中提取的文本保存到单个 .txt 文件中,但出现以下错误 AttributeError: - I am trying to save text extracted from all images into single .txt file but getting the following error AttributeError: 如何在python中保存来自网站的所有图像 - how to save all images from a website in python 如何在 python 中打开 .zip 文件? - How can i open a .zip file in python? 如何将所有 python 代码打包成一个 zip 文件? - How can you bundle all your python code into a single zip file? 如何在 JSON 文件中查找和替换所有出现的单词并使用 python 保存生成的 JSON? - How can I find and replace all occurrences of words in a JSON file and save the resulting JSON using python? 如何使用 Python 将 zip 中的文本文件解压缩为 zip - How to extract the text file from zip into zip using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM