【发布时间】: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