Convert a jpg to a list of lists

2024/10/14 19:22:30

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

Answer
  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://en.xdnf.cn/q/117924.html

Related Q&A

TypeError: No to_python (by-value) converter found for C++ type

Im trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what Im trying to do:struct Base {virtual ~Base() {};virtual char const *Hello() {printf("Base.Hell…

USB interface in Python

I have this (http://www.gesytec.de/en/download/easylon/p/16/) USB device connected to my Win7. I am just trying to read the vendor ID and product ID. I have Python 2.7.Here is the code,import usb busse…

Django M2M Through extra fields with multiple models

Im trying to figure out the best way to set up the following django model (genericised for security reasons).ThingA:User(M2M through "UserRelation")ThingB:User(M2M through "UserRelation&…

Openpyxl: Worksheet object has no attribute values

My goal is to read in an excel file and view the codes in a pandas dataframe (i.e. = A3) rather than the resulting values from excel executing the codes, which is the pandas default if read in using pa…

Calculation of Contact/Coordination number with Periodic Boundary Conditions

I have data for N atoms including the radius of each atom, the position of each atom and the box dimensions. I want to calculate the number of atoms each atom is in contact with and store the result. T…

how can I export multiple images using zipfile and urllib2

I am trying to add multiple image files into my zip. I have searched around and knows how to add a single one. I tried to loop through multiple images then write into it but it didnt work. I kind of d…

XPATH for Scrapy

So i am using SCRAPY to scrape off the books of a website. I have the crawler working and it crawls fine, but when it comes to cleaning the HTML using the select in XPATH it is kinda not working out ri…

Kivy Removing elements from a Stack- / GridLayout

I made a pop-up. It is basically some rows of options (max. 5 rows).If I press the + button, there will be a new line of options.If I press the - button the last row should diasappear. Unfortunatelly i…

Bad timing when playing audio files with PyGame

When I play a sound every 0.5 second with PyGame:import pygame, timepygame.mixer.init() s = pygame.mixer.Sound("2.wav")for i in range(8):pygame.mixer.Channel(i).play(s)time.sleep(0.5)it doesn…

Using read_batch_record_features with an Estimator

(Im using tensorflow 1.0 and Python 2.7)Im having trouble getting an Estimator to work with queues. Indeed, if I use the deprecated SKCompat interface with custom data files and a given batch size, the…