python - 将 Excel 转换为 TIFF 文件

标签 python python-imaging-library tiff

我有一个包含像素值的 Excel 文件,我正在尝试将其转换为 TIFF 或栅格数据集以使用 Arcgis 打开。我在这里寻找类似的问题,但找不到任何问题。我尝试了一些东西,但它给出了一个错误。从 DEM 获取的 Excel 文件包含 2098 行 x 2851 列,无标题。

这是我的代码:

import pandas as pd
import Image as im    

file = r'C:/Users/owrasa/PycharmProjects/den/demrep2.xlsx'
size = 2098, 2851
df = pd.read_excel(file, header=0)
df2 = pd.np.array(df)
imarray = im.fromarray(df2)
imsave = im.SAVE(imarray, "TIFF")

这是错误消息:

TypeError: Cannot handle this data type

Excel 文件如下所示:

-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60      60      60      60      60
-32767  -32767  -32767  -32767  -32767  -32767      60      60      60      60      60      60      60      60      60
-32767  -32767  -32767  -32767      60      60      60      60      60      60      60      60      60      60      60
-32767  -32767  -32767      60      60      60      60      60      60      60      60      60      60      60      60
-32767  -32767      59      60      60      60      60      60      60      60      60      60      60      60      60
-32767      59      59      59      59      60      60      60      60      60      60      60      60      60      60
    59      59      59      59      59      59      59      59      59      60      60      60      59      60      60
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59
    59      59      59      59      59      59      59      59      59      59      59      59      59      59      59

最佳答案

您的代码有几个问题:

  1. Image 模块未从 PIL 导入。
  2. df2 的数据类型是 int64 而不是 int32
  3. Image.save() 的调用不正确(函数名称必须小写)。

以下代码片段修复了所有这些问题,应该可以完成工作:

import pandas as pd
import numpy as np
import PIL.Image as im

file = r'C:/Users/owrasa/PycharmProjects/den/demrep2.xlsx'
df = pd.read_excel(file, header=0)
df2 = pd.np.array(df).astype(np.int32)
imarray = im.fromarray(df2)
imarray.save(r'C:/Users/owrasa/PycharmProjects/den/demrep2.tif')

关于python - 将 Excel 转换为 TIFF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076811/

相关文章:

python - 在 App Engine 上用 Python 打开图像

python - Django/PIL 错误 - 呈现 : The _imagingft C module is not installed 时捕获异常

python-3.x - tifffile 无法解压缩 JPEG,因为 JPEG 不在 TIFF.DECOMPRESSORS 中

python - 零值的 Pandas groupby

python - 在opencv安装后在Python中导入cv2时出错

python:在cherrypy中使用json.load()时出现500错误

python - 如何使用PIL(pillow)绘制任何语言的文字?

python - 如何将自定义选项传递给 fab 命令

silverlight - 如何使用控件或类/函数将 TIFF 转换为 JPG Inside Silverlight,客户端?

c - 加快写入多个图像 TIFF?