在文件夹中保存带有不同名称的图像
我尝试将图像保存在这样的文件夹中,它保存了不同的图像,但是每个下一个图像都有以前图像的所有名称。
db = h5py.File('results/Results.h5', 'r')
dsets = sorted(db['data'].keys())
for k in dsets:
db = get_data()
imnames = sorted(db['data'].keys())
slika = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'spremljene_slike/ime_{imnames}.png', slika)
因此,我尝试过这样的尝试,并保存了不同的名称,但只有最后生成的图片才在文件夹中添加,因此不同的名称 - 相同的图片
NUM_IMG = -1
N = len(imnames)
global NUM_IMG
if NUM_IMG < 0:
NUM_IMG = N
start_idx,end_idx = 0,N #min(NUM_IMG, N)
**In different function:**
for u in range(start_idx,end_idx):
imname = imnames[u]
cv2.imwrite(f'spremljene_slike/ime_{imname}.png', imname)
有人可以帮助,我找不到。 我有脚本可以生成带有渲染文本的图像,然后将其保存在.h5文件中,然后从那里我想在不同文件夹中保存使用相应名称的图片。
I tried save images in folder like this, it saves different images but every next image have all names of previously images.
db = h5py.File('results/Results.h5', 'r')
dsets = sorted(db['data'].keys())
for k in dsets:
db = get_data()
imnames = sorted(db['data'].keys())
slika = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'spremljene_slike/ime_{imnames}.png', slika)
So i tried like this and it saves different names but only last generated picture is imwrited in folder, so different names - the same picture
NUM_IMG = -1
N = len(imnames)
global NUM_IMG
if NUM_IMG < 0:
NUM_IMG = N
start_idx,end_idx = 0,N #min(NUM_IMG, N)
**In different function:**
for u in range(start_idx,end_idx):
imname = imnames[u]
cv2.imwrite(f'spremljene_slike/ime_{imname}.png', imname)
Can someone help, I can't figure out.
I have script which generate images with rendered text and save it in .h5 file, and then from there I want to save this pictures with corresponding names in different folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根本看不到这是如何工作的。在第1行上,您定义
db = h5py.file()
,然后在第4行,将其重新定义为db = get_data()
。什么是get_data()
?没有模式很难编写代码。下面的答案是我的最佳猜测,假设您的图像是
db ['data']
中的数据集,并且您想将数据集名称(又称密钥)用作图像名称。那应该是您需要做的全部。您将获得名为
ime_ {imName} .png
(其中imName
是匹配的数据集名称)的每个数据集的1 .png。另外,您可以消除所有中间变量(DSET,IMG_ARR和SLIKA)。将上述代码压缩为几行:
Don't see how this works at all. On line 1 you define
db=h5py.File()
, then on line 4, you redefine it asdb=get_data()
. What isget_data()
?It's hard to write code without the schema. Answer below is my best-guess assuming your images are datasets in
db['data']
and you want to use the dataset names (aka keys) as the image names.That should be all you need to do. You will get 1 .png for each dataset named
ime_{imname}.png
(whereimname
is the matching dataset name).Also, you can eliminate all of the intermediate variables (dsets, img_arr and slika). Compress the code above into a few lines: