【问题标题】:Exporting image intensities from multiple files in multiple folders to excel in Python将图像强度从多个文件夹中的多个文件导出到 Python 中的 excel
【发布时间】:2018-05-12 15:25:21
【问题描述】:

我需要将来自多个文件夹中的多个图像的像素强度列表导出到 Excel 电子表格。每个文件夹都包含一个 tiff 文件列表,每个文件都代表时间流逝中的某个时间点。我已经设法获得了每个文件夹子集的像素强度,但是我正在努力使用 DataFrames 和 pandas 来输出 excel。数据框只显示最后一个文件夹中的值列表,我需要电子表格在单独的行中显示每个列表。这是我所拥有的:

import os
import matplotlib.pyplot as plt
import skimage.external.tifffile as tiff
import pandas as pd
from pandas import DataFrame

#to read images in each folder
def load_images_from_folder(folder):
images=[]
for filename in os.listdir(folder):
    if any([filename.endswith(x) for x in ['.tif']]):
        img=tiff.imread(os.path.join(folder, filename))
        if img is not None:
            images.append(img)
return images

folders = [
    'path to folder1',
    'path to folder2',
    'path to folder3',
]

for folder in folders:
    images=load_images_from_folder(folder)

#ratio the mean green to red signal in each image 
    ratios = [image[..., 1].mean() / image[..., 0].mean() for image in 
    images]
    plt.plot(range(len(images)), ratios)
    plt.show()


df=DataFrame({'Ratios':ratios})
df.to_excel('Ratios.xlsx', sheet_name='sheet1', index=0)

打印出比率给出: 文件夹1: [值列表] 文件夹2: [值列表] 文件夹 3: [值列表 等等 但是df(DataFrame)显示的数据只来自Folder3中的列表。那么,我需要做些什么不同的事情才能将来自多个文件夹的值导出到 Excel 中?我还确保每个图像都被读取为 ndarray 和 type=uint8。

【问题讨论】:

  • 当我尝试创建数据框时,扩展列表方法效果不佳。对不起,我想通了之后忘记添加答案了。

标签: python pandas image-processing scikit-image numpy-ndarray


【解决方案1】:

通过调用 extend 列表方法替换 for 循环中的赋值语句应该可以解决问题:

images = []
for folder in folders:
    images.extend(load_images_from_folder(folder))

【讨论】:

  • 没有这样的运气。另外,不应该在 for 循环之后首先定义名称“图像”吗?
  • 确实,在调用extend 之前,您需要创建images。顺便说一句,你应该修复缩进。
  • 数据框现在生成所有数据的列表。我将指定在哪里剪切它以在电子表格中生成多个列。
【解决方案2】:
import tkinter
from tkinter import filedialog
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askdirectory
import os
import matplotlib.pyplot as plt
import skimage.external.tifffile as tiff
import pandas as pd
from pandas import DataFrame
import numpy as np

def load_images_from_folder(folder):
    images=[]
    for filename in os.listdir(folder):
        if any([filename.endswith(x) for x in ['.tif']]):
            img=tiff.imread(os.path.join(folder, filename))
            if img is not None:
                images.append(img)
    return images

tkinter.Tk().withdraw()   
dirname = askdirectory(initialdir="/", title='Please select a directory')
os.chdir(dirname)

data = pd.DataFrame([])
ratiodata = []
foldernames = []
for folder in os.listdir(dirname):  
    if not folder.endswith('.xlsx'):
       images=load_images_from_folder(folder)
       green=[image[..., 1].mean() for image in images]
       red=[image[..., 0].mean() for image in images]
       ratios= [img[..., 1].mean() / img[..., 0].mean() for img in images]
       #PLOTS
       temp = pd.DataFrame({folder + " ratios" : ratios})
       data = pd.concat([data,temp],axis=1)
data.to_excel('test.xlsx', sheet_name='sheet1')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-26
    • 2018-09-07
    • 1970-01-01
    • 2017-02-22
    • 2020-08-24
    • 2015-07-30
    • 2016-06-14
    • 1970-01-01
    相关资源
    最近更新 更多