GDAL 光栅输出

发布于 2024-11-24 08:20:45 字数 1988 浏览 2 评论 0原文

我正在尝试在 python 中使用 GDAL 创建一个 .tif 文件。它正在创建一个文件,但每当我浏览它时都会说“没有可用的预览”。现在,我只是想让它复制输入文件。这是我的代码:

gdal.AllRegister()

inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a2.tif")
if inDs is None:
  print 'Could not open image file'
  sys.exit(1)
else:
    print "successfully opened input file"

rows = inDs.RasterYSize
cols = inDs.RasterXSize
myband = inDs.GetRasterBand(1)
elev_data = myband.ReadAsArray(0,0,cols,rows)
driver = inDs.GetDriver()
outDs = driver.Create('C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\new.tif', cols, rows, 1, GDT_Int32)

if outDs is None:
    print "couldn't open output file"
    sys.exit(1)

outBand = outDs.GetRasterBand(1)
outData = numpy.zeros((rows,cols),numpy.int16)
outBand.WriteArray(elev_data)
outBand.FlushCache()
outBand.SetNoDataValue(-99)
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())
del outData

============================update========================================= made some discoveries... I've studied ways of converting from one number format to another using statistical normalization. I processed the input data and transformed it to uint8 by using the following algorithm:

std = elev_data.std() #standard dev
avg = elev_data.mean()
arr = numpy.zeros((rows,cols),numpy.uint8)
for _i_ in _range_(_rows_):
    for _j_ in _range_(_cols_):
        arr[i,j] = (((out_elev[i,j]-avg)/std)*127)+128 #normalization formula
        #this puts all vals in range 1 to 255 (uint8)
dr = gdal.GetDriverByName("GTiff")
outDs = dr.Create("name",cols,rows,3,GDT_Byte) 
#creates and RGB file, accepts uint8 for input
outDs.GetRasterBand(1).WriteArray(arr) #write the output as shades of red
#this writes out a format viewable by microsoft products

我想复制的主要原因是为了证明我可以读入,然后根据计算写出更新的数据。

我可以使用色带而不是仅使用一种颜色的阴影写出输出数据的方法是什么?

I'm trying to create a .tif file using GDAL in python. It's creating a file, but saying "no preview available" whenever I browse to it. Right now, I'm just trying to get it to make a copy of the input file. Here's my code:

gdal.AllRegister()

inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a2.tif")
if inDs is None:
  print 'Could not open image file'
  sys.exit(1)
else:
    print "successfully opened input file"

rows = inDs.RasterYSize
cols = inDs.RasterXSize
myband = inDs.GetRasterBand(1)
elev_data = myband.ReadAsArray(0,0,cols,rows)
driver = inDs.GetDriver()
outDs = driver.Create('C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\new.tif', cols, rows, 1, GDT_Int32)

if outDs is None:
    print "couldn't open output file"
    sys.exit(1)

outBand = outDs.GetRasterBand(1)
outData = numpy.zeros((rows,cols),numpy.int16)
outBand.WriteArray(elev_data)
outBand.FlushCache()
outBand.SetNoDataValue(-99)
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())
del outData


============================update=========================================
made some discoveries...
I've studied ways of converting from one number format to another using statistical normalization. I processed the input data and transformed it to uint8 by using the following algorithm:

std = elev_data.std() #standard dev
avg = elev_data.mean()
arr = numpy.zeros((rows,cols),numpy.uint8)
for _i_ in _range_(_rows_):
    for _j_ in _range_(_cols_):
        arr[i,j] = (((out_elev[i,j]-avg)/std)*127)+128 #normalization formula
        #this puts all vals in range 1 to 255 (uint8)
dr = gdal.GetDriverByName("GTiff")
outDs = dr.Create("name",cols,rows,3,GDT_Byte) 
#creates and RGB file, accepts uint8 for input
outDs.GetRasterBand(1).WriteArray(arr) #write the output as shades of red
#this writes out a format viewable by microsoft products

the main reason I wanted to copy was to prove that I could read in, then write out updated data based on calculations.

what might be a way I could write out the output data using a color ramp, instead of shades of just one color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2024-12-01 08:20:45

您的意思是当您尝试将 TIFF 文件预览为图像时,Windows 图片和传真查看器应用程序显示“无预览可用”? (请参见下面的屏幕截图。)

无预览可用屏幕截图

请记住,有 许多不同风格的 TIFF,但并非所有风格都是相同的。特别是,Windows 图片和传真查看器不支持所有类型的 TIFF。

有一篇 Microsoft 知识库文章 您无法使用 Windows 图片和传真查看器查看 TIFF 图像 部分内容如下:

Windows XP 中的 Windows 图片和传真查看器使用 Windows 图形设备接口 (GDI+)。 GDI+ 支持许多传真标准压缩算法。但是,它可能与一些不经常使用的编码方案不兼容。

如果您正在寻找用于查看栅格数据(包括 GeoTIFF 栅格)的工具,我建议您免费使用 OpenEV< /a>,您可以从 FWTools 包中获取它。

Do you mean you're getting "No preview available" from the Windows Picture and Fax Viewer application when trying to preview the TIFF file as an image? (See below screenshot.)

No preview available screenshot

Bear in mind that there are many different flavors of TIFF, and not all are the same. In particular, the Windows Picture and Fax Viewer does not support all kinds of TIFFs.

There is a Microsoft Knowledge Base article You Cannot View TIFF Images Using Windows Picture and Fax Viewer that says in part:

Windows Picture and Fax Viewer in Windows XP uses Windows Graphics Device Interface (GDI+). GDI+ supports many standard compression algorithms for faxes. However, it may be incompatible with some of the encoding schemes that are not used frequently.

If you're looking for a tool for viewing raster data (including GeoTIFF rasters), I'd recommend the freely available OpenEV, which you can get as part of the FWTools package.

£噩梦荏苒 2024-12-01 08:20:45

我注意到以下几点:

  1. 您只复制源数据集的一个波段。如果它是一种颜色
    图像,它可能有 3 个波段,或一个彩色图。如果有彩色图的话
    你也需要复制它。如果是 3 或 4 个频段,则有
    复制所有数据。
  2. CreateCopy() 是一种更简单的方法来完成您想要的操作。

Couple of things I notice:

  1. You only copy one band of the source dataset. If it is a color
    image, it may have 3 bands, or a color map. If it has a color map,
    you need to copy it over as well. If it is 3 or 4 bands, you have
    to copy all of the data.
  2. CreateCopy() is an easier way to do what you are looking for.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文