How do I efficiently convert pandas dataframe to image array?

trippeljojo

I have a big pandas dataframe with columns X,Y, and I. X and Y are pixel coordinates, I is, let's say, an intensity value between 0 and 255 which I want to be shown at the corresponding X and Y position.

There is not an entry for each pixel of the image, so all pixels' I values that are not listed in the dataframe are set to 0.

Therefore, I initialized an two-dimensional array img with the image dimensions. Then, I already tried something like

img.at[df.X,df.Y] = df.I

which does not work. I think a simple for-loop can solve this problem, but I wonder if there is a more efficient way to do this (e.g. call a fancy numpy/opencv/whatever function I don't know...).

Daniel F

Easiest way is probably to use scipy.sparse arrays, as coo_matrix is built identically to your inputs.

from scipy.sparse import coo_matrix

sparse_image = coo_matrix((df.I, (df.X, df.Y)), shape = image.shape)
image = sparse_image.todense()

coo_matrix also has some nice functionalities if your I values are being accumulated before this step.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a numpy array into a pandas dataframe?

how do i convert a numpy array to pandas dataframe

How can I convert an array of pixel colors into an image efficiently with Python?

How do I efficiently map transformations over a pandas DataFrame

How to convert a pandas dataframe column to an image array i.e. a numpy array with shape (n,n) in Python?

How do I convert my 2D numpy array to a pandas dataframe with given categories?

How do I convert a pandas series which is multidimensional to pandas dataframe

Efficiently convert timezones in pandas dataframe

How can I convert an array of class objects to a dataframe with columns in Pandas?

How can I convert an array of dates into a Pandas DataFrame?

How do I convert vincenty distance to float in pandas dataframe

How do i convert python list into pandas dataframe with predefined columns

How do I convert timestamp to datetime.date in pandas dataframe?

How do I convert a text table to a pandas dataframe?

how do i convert from a webcomponent to pandas dataframe

How do I map a numpy array and an indices array to a pandas dataframe?

How do I efficiently parse a substring in a Pandas Dataframe based on a value in a column?

How do I efficiently apply pandas.Timestamp functions to a full dataframe/column?

How do I efficiently perform the same function across multiple groups of columns in a pandas dataframe?

How do I convert a Python DataFrame into a NumPy array

How do I convert a numpy array of floats into an image?

How do I convert a numpy array to (and display) an image?

How do I convert a pandas Series or index to a Numpy array?

How to convert array of dictionary of array to pandas dataframe?

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

How to efficiently iterate a pandas DataFrame and increment a NumPy array on these values?

How can I convert my Set to an array efficiently?

How to purify pandas dataframe efficiently?

How to efficiently filter pandas dataframe