-1

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) 

enter image description here

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.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87

1 Answers1

0

Don't see how this works at all. On line 1 you define db=h5py.File(), then on line 4, you redefine it as db=get_data(). What is get_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.

with h5py.File('results/Results.h5', 'r') as db:
    dsets = sorted(db['data'].keys())
    for imname in dsets:
        img_arr = db['data'][imname][()]
        slika = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
        cv2.imwrite(f'spremljene_slike/ime_{imname}.png', slika)

That should be all you need to do. You will get 1 .png for each dataset named ime_{imname}.png (where imname 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:

with h5py.File('results/Results.h5', 'r') as db:
    for imname in sorted(db['data'].keys()):
        cv2.imwrite(f'spremljene_slike/ime_{imname}.png', \
                    cv2.cvtColor(db['data'][imname][()], cv2.COLOR_BGR2RGB))
kcw78
  • 7,131
  • 3
  • 12
  • 44