酸洗图像对象? [英] pickling an image object?
本文介绍了酸洗图像对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是pickle的完全新手,我有一堆(大约100,000张)图片需要pickle.
I'm a complete novice with pickle, and I have a bunch of (about 100,000) images that need to be pickled.
它们首先作为图像对象加载,并转换为数据如下:
They are first loaded as image object, and converted to data as following:
image = {
'pixels': im.tostring(),
'size': im.size,
'mode': im.mode,
}
现在如何将它们腌制到一个 pkl 文件中?
Now how do I pickle them into one pkl file?
推荐答案
你可以这样做:
file = open('data.pkl', 'wb')
# Pickle dictionary using protocol 0.
pickle.dump(image, file)
file.close()
要在字典中阅读,您可以这样做:
To read in the dictionary, you can do it like this:
file = open('data.pkl', 'rb')
image = pickle.load(pkl_file)
print image
file.close()
也可以将数据转储两次:
It is also possible to dump the data twice:
import pickle
# Write to file.
file = open("data.pkl", "wb")
pickle.dump(image1, file)
pickle.dump(image2, file)
file.close()
# Read from file.
file = open("data.pkl", "rb")
image1 = pickle.load(file)
image2 = pickle.load(file)
file.close()
这篇关于酸洗图像对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文