0

i have dictionary with "text" as keys and corresponding list of "arrays" as values. How can i convert this arrays to images?

{'all': [[array([[[ 0.        ,  1.        ,  0.        ],
       [ 0.        ,  1.        ,  0.        ],
       [ 0.        ,  1.        ,  0.        ],
      ...

      [[ 0.        ,  1.        ,  0.        ],
       [ 0.        ,  1.        ,  0.        ],
       [ 0.75686276,  0.60392159,  0.54901963],

It looks like this. For each word , i will have one or more arrays.

This is how i tried

updated..

                 a=New_array["had"]
                 b=len(a)
                 for i in a:
                     for ii in i:
                         plt.imshow(ii)
                         plt.show()

And i want to save the images for each key in the dictionary.

Div
  • 1
  • 4
  • What error do you get (or not)? What shape is `i`? I would use `matplotlib`'s `imshow` function for this. In Python almost all libraries use numpy arrays to represent images - you do not need to "convert" them as such. Also what do you want the output to be? Do you want to save the images? Or just show them? If you edit your question to provide a bit more context it'll give people a better chance of helping you. – Josh Feb 25 '19 at 20:43
  • If you are using scipy, why is this question is tagged with opencv? – SilverMonkey Feb 25 '19 at 21:43
  • i tried with opencv as well. cv2.imshow() was not working for me – Div Feb 25 '19 at 21:49
  • Flagged as a duplicate as the question is about saving images which is well documented on SO. @Div, you should look for commands which write images - like plt.imsave or cv2.imwrite. Have a look at the documentation for Matplotlib and/or OpenCV. – Josh Feb 26 '19 at 14:11

1 Answers1

0

Here is some example code that uses OpenCV. Please see the OpenCV docs for more info.

import numpy as np
import cv2

image_dict = {}

image_dict["test"] = [np.random.random((100,100)) for _ in range(10)]

print(image_dict)

for key, images in image_dict.items():
    for i, image in enumerate(images):
        cv2.imwrite("./{}_{:d}.png".format(key, i), image)

The code in your post indicates that you tried imshow. This function (in both Matplotlib and OpenCV) is specifically for displaying an image to the user on screen. You should use cv2.imwrite or plt.imsave. Note that Matplotlib will typically save images with axes, which you probably don't want.

Josh
  • 2,658
  • 31
  • 36