首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何将多个图像保存到一个泡菜文件中?而泡菜文件包含图像数据(像素信息),还是只包含图像文件的名称?

如何将多个图像保存到一个泡菜文件中?而泡菜文件包含图像数据(像素信息),还是只包含图像文件的名称?
EN

Stack Overflow用户
提问于 2022-01-16 14:08:05
回答 1查看 1.6K关注 0票数 0

我有一个名为1.jpg、2.jpg、3.jpg.11344.jpg、11345.jpg的“鸟”文件夹,其中有11345个鸟图像。我需要将这些鸟类图像保存为"filenames.pickle“,以便在远程机器学习模块中使用它。数据应以这种方式排列:数据集/列车/文件.泡菜、数据集/测试/文件.泡菜

我需要创建一个单一的泡菜文件filenames.pickle,以获得所有的11345鸟图像。我非常困惑该如何将这些图像添加到泡菜中,这样我的代码就可以获取泡菜文件,但是它最终会到达这些图像来训练机器学习模型。

代码语言:javascript
代码运行次数:0
运行
复制
from PIL import Image  
import pickle

'''
I am just trying to convert one image into pickle to get an idea. 
if is succefully convert into pickle then I will read all the 
images inside the "bird" folder and convert all of them into one 
single pickle file
'''

# converting an image into pickle 
img = Image.open('059.jpg')
with open('059.pickle', 'wb') as f:
   pickle.dump(img, f)


## read the pickle file
with open('059.pickle','rb') as f:
file = pickle.load(f)
   print(file)

# after reading 059.pickle file : 
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x375 at 0x2115BE59190>

# I dont want ( <PIL.JpegImagePlugin.JpegImageFile image mode=RGB 
 size=500x375 at 0x2115BE59190>) this result into pickle file. 
# I want pickle file to save result like this: ['59.jpg']. 

 
 ## to convert whole images inside bird folder
 ## input folder = bird\\images\\all_images_in_jpg_format
  
 image = "bird\\images\\"
 fout = open("bird\\filenames.pickle",'wb')
 pickle.dump(image,fout)
 fout.close()

with open("bird\\filenames.pickle",'rb') as f:
file = pickle.load(f)
   print(file)
# output : bird\images\
## the above output is wrong


 '''
 becasue when I am done reading all the images and create one 
  pickle file as "filenames.pickle:, it should save images like 
  this: 
 ['01.jpg','0342.jpg','06762.jpg', '06752.jpg', '05122.jpg', 
  '05144.jpg', '06635.jpg','06638.jpg', 
 '05632.jpg',......'11345.jpg'] 
 and after reading this pickle file, somehow model will 
 automatcally read the images via pickle file.

 '''

我不太熟悉泡菜文件和它的格式。有人能帮我吗?或者给我一些建议,我该如何处理和解决这个问题?模型将如何通过泡菜文件读取图像?泡菜文件包含什么(图像数据和像素信息,或者仅仅是图像文件的名称),以便模型能够在训练时间内获取泡菜文件并学习图像?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-16 17:53:40

修改了我原来的答案。现在,我在一个文件中选择文件名,并将图像保存到另一个文件中。

代码语言:javascript
代码运行次数:0
运行
复制
from PIL import Image
import os
import pickle
from glob import glob

## to convert whole images inside bird folder
## input folder = bird\\images\\all_images_in_jpg_format

PICKLE_FILE = "bird\\filenames.pickle"
SOURCE_DIRECTORY = "bird\\images\\"
PICKLE_IMAGES = "bird\\images.pickle"

path_list = glob(os.path.join(SOURCE_DIRECTORY, "*.jpg"))

# pickle images into big pickle file

with open(PICKLE_IMAGES,"wb") as f:
    for file_name in path_list:
        pickle.dump(Image.open(file_name),f)
        
# get short names from the path list 

file_list = list(
    map(
        lambda x: os.path.basename(x), path_list)
)

# pickle short name list

pickle.dump(file_list, open(PICKLE_FILE, 'wb'))

# test that we can reread the list

recovered_list = pickle.load(open(PICKLE_FILE,"rb"))

if file_list == recovered_list:
    print("Lists Match!")
else:
    print("Lists Don't Match!!!")


# read a couple images out of the image file:

display_count = 5


with open(PICKLE_IMAGES,"rb") as f:
    while True:
        try:
            pickle.load(f).show()
            display_count -= 1
            if display_count <= 0:
                break
        except EOFerror as e:
            break

它可能仍然是你的教练想要个别的腌制图像,或者它不喜欢PIL使用的图像格式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70730835

复制
相关文章

相似问题

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