将jpg转换为列表列表 [英] Convert a jpg to a list of lists

查看:94
本文介绍了将jpg转换为列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何将jpg转换为列表列表(使用python 3.2.3),例如:

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.

我认为这会很容易,只需编写一个小脚本即可从jpg文件中提取数据.哈哈!经过研究,我意识到这比我真正想做的工作要多得多.

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.

谢谢

推荐答案

  1. 安装 PIL 并打开您的图片:

打开图像AS Numpy数组(确保已安装numpy):

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

image = numpy.asarray(Image.open('pic.jpg'))

  • 使用numpy.split将结果数组拆分为列表:

  • Use numpy.split to split your resulting array into lists:

    lists = numpy.split(image)
    

  • 播放列表.

    这篇关于将jpg转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆