Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - Iterate and transfer dataframe information to an image

I'm trying to "convert" the information of several blocks of dataframe rows 0 to 15 and columns col1 to col 16 into an image (16x16). Between each 16x16 block, I have an empty line as you can see.

I'm reading the dataframe from a .txt file:

df = pd.read_csv('User1/Video1.txt', sep="s+|	+|s+	+|	+s+", header=None, names=headers, engine='python', parse_dates=parse_dates)
                        date arrow  col1  col2  ...  col13  col14  col15  col16
0    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    0.0    0.0
1    2020-11-09 09:53:39.552    ->   0.0   2.0  ...    0.0    0.0    0.0    0.0
2    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    6.0    6.0
3    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    0.0    0.0
4    2020-11-09 09:53:39.586    ->   0.0   9.0  ...    0.0    7.0    0.0    0.0
...
15   2020-11-09 09:53:39.586    ->   0.0   9.0  ...    0.0    7.0    0.0    0.0
16   2020-11-09 09:53:39.586    ->
...                 
1648 2020-11-09 09:54:06.920    ->   4.0   0.0  ...    4.0    4.0    0.0    0.0

I'm capable of reshaping the first 16x16 block: img=np.resize(df.iloc[:16, -16:].to_numpy(), (16, 16, 3)) but I want to iterate over all dataframe and sum all the pixel values of each 16x16 block.

Can you provide any advice? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you can provide input of the first 32 rows and columns I can test, but try the following code (untested). Essentially, you can just loop through each 16x16 block, get the sum and append to a list:

i = 16
img_list = []
for _ in range(df.shape[0] / 17):
    img = np.resize(df.iloc[i-16:i, -16:].to_numpy(), (16, 16, 3))
    img_sum = img.sum()
    img_list.append(img_sum)
    i += 17 

Edit: I just saw you had one empty line separating blocks. I have update the necessary values to 17.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...