0

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

Jason White
  • 666
  • 4
  • 10
  • 23
  • 2
    Have you looked into the Python Imaging Library (PIL)? – mgilson Oct 08 '12 at 19:21
  • 1
    Do you have an (unstated) ultimate goal in mind once converted, or are you really just seeking a way to turn the tidy, standardized image format into an unwieldy list of lists? – hexparrot Oct 08 '12 at 19:25
  • thought I'd try my hand at some ocr stuff. Seemed like it should be easy enough. Famous last words eh? – Jason White Oct 08 '12 at 20:00

1 Answers1

2
  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.

khan
  • 7,005
  • 15
  • 48
  • 70
  • The PIL library doesn't seem to support versions of Python above 3. Is there another way to do that? – Jason White Oct 08 '12 at 20:00
  • @JasonWhite try working out with the PIL's 2.7 version - I think it'll work. – khan Oct 08 '12 at 20:17
  • Even better, Unofficial Windows Binaries for Python Extension Packages at http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil. Found this in another answer here at http://stackoverflow.com/questions/6188552/are-there-image-processing-modules-for-python-3 – Jason White Oct 08 '12 at 20:49