Convert a jpg to a list of lists

删除回忆录丶 提交于 2019-12-25 00:37:57

问题


I'm trying to figure out how to convert a jpg into a list of lists (using python 3.2.3) such that:

[ 
[red,blue,red,etc..],           #line1
[blue,red,yellow, etc...],      #line2
[orange,yellow,black,etc...],   #Last Line
]

Basically each list within the main list represents a horizontal line of colour values starting from the top of the image.

I thought it was going to be easy, just write a little script to extract the data from the jpg file. Hah! After looking into that I realized that was going to be a lot more work than I really want to do.

So far I'm thinking converting it to a bitmap and then writing the data into a suitable format is my best bet. Of course then I have to figure out how to extract the info from a bitmap.

Now since I'm sure other people have had to do this before, someone must know a far easier way I can go about that. I've tried looking around but haven't had any luck so far.

Thanks


回答1:


  1. Install PIL and open your image:

  2. Open your image AS Numpy array (make sure numpy is installed):

    image = numpy.asarray(Image.open('pic.jpg'))
    
  3. Use numpy.split to split your resulting array into lists:

    lists = numpy.split(image)
    

play with your lists.



来源:https://stackoverflow.com/questions/12788087/convert-a-jpg-to-a-list-of-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!