简体   繁体   English

python和gdal用于图像处理

[英]python and gdal for image processing

i have a database catalogue that reads images path as well as other attributes and there is a part that tries to open the dataset in the code so that other processe can go on if the opening was sucessfull but i have hit a stumblimg block as to how to tell the process to go on after the following code, the code runs smoothly but when it encounters an image it can not open it stops instead of going to the begining to read the database again and open a new image. 我有一个数据库目录,它读取图像路径以及其他属性,并且有一部分试图在代码中打开数据集,以便在打开成功后其他进程可以继续进行,但是我遇到了一个关于如何执行的stumblimg块为了告诉过程在以下代码之后继续进行,代码运行流畅,但是当遇到图像无法打开时,它将停止,而不是开始重新读取数据库并打开新图像。

try:  
    hDataset = gdal.Open( pszFilename, gdal.GA_ReadOnly )  
    except IOError:  
    print("gdalinfo failed - unable to open '%s'." % pszFilename )  
    status = "UPDATE %s SET job = 11  WHERE id = %s" % (table,row[2])  
    setstatus = conn.cursor()  
    setstatus.execute(status)  
    conn.commit()  
    setstatus.close()  
else:  
    print "file opened sucessfully"  
    hDataset.close()

GDAL does not normally throw exceptions, which is a shame. GDAL通常不会引发异常,这是可耻的。 With gdal.UseExceptions() turned on, it sometimes throws RuntimeError (only!), but I haven't found this feature very reliable. gdal.UseExceptions() ,有时会抛出RuntimeError (仅!),但是我还没有发现此功能非常可靠。

Some functions with GDAL return None if unsuccessful, and others return a status integer, where 0 is good and non-zero is an error code. 如果不成功,则使用GDAL的某些函数将返回None ,而另一些函数将返回状态整数,其中0为良好,非零为错误代码。

A typical form I use is something like this: 我使用的一种典型形式是这样的:

hDataset = gdal.Open(pszFilename, gdal.GA_ReadOnly)
if hDataset is None:
    raise IOError("Could not open '%s'" % (pszFilename,))

band_num = 1
band = hDataset.GetRasterBand(band_num)
if band is None:
    raise AttributeError("Raster band %s cannot be fetched" % (band_num,))
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM