Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array into image

Tags:

python

opencv

I have a 2Darray named C containing pixels in its indexes. How to view the image that is formed by that pixels value, A image can be represented by array. No w i am trying the reverse process, I wanna see only grayscale image since array named C has only one value per index. Doing programming in python

Here goes my code http://pastebin.com/qmnKrtzu

getting error in this line

ime = Image.fromarray(c)
ime.save("your_file.jpeg")

ime = Image.fromarray(c_array) arr = obj.__array_interface__ AttributeError: 'list' object has no attribute '__array_interface__'

like image 404
Raghib Ahsan Avatar asked Jun 29 '26 03:06

Raghib Ahsan


2 Answers

That's much better with the code; thanks. In the future, please remember to cut down the code to the minimal level and post it in the original question. An external link doesn't get nearly as much attention.

Your problem is that Image.fromarray requires an array as input; you gave it a list. You did originally convert your list, c, to an array, but you didn't keep the array. I think you need this at lines 18 and 41 from your pastebin code, where I've added references to c_array:

c = [[0]*k*im.width for i in range(k*im.height)]
c_array = np.asarray(c)
...
ime = Image.fromarray(c_array)
ime.save("your_file.jpeg")
like image 187
Prune Avatar answered Jun 30 '26 18:06

Prune


You can simply use c.values to convert dataframe into an array

ime = Image.fromarray(c.values)
ime.save("your_file.jpeg")
like image 21
Harsh Shah Avatar answered Jun 30 '26 20:06

Harsh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!