コード例 #1
0
def convert_image_to_gray_auto(output_image, input_image):
    """
    convert inputed image to 8bit
    Args:
        output_image:output image file path
        input_image: input imag file path

    Returns:output_image if successful, False otherwise

    """
    if os.path.isfile(output_image) is True:
        basic.outputlogMessage('%s already exist,skip' % output_image)
        return output_image

    input_image_obj = RSImageclass()
    if input_image_obj.open(input_image) is False:
        return False

    # GDT_Unknown = 0, GDT_Byte = 1, GDT_UInt16 = 2, GDT_Int16 = 3,
    # GDT_UInt32 = 4, GDT_Int32 = 5, GDT_Float32 = 6, GDT_Float64 = 7,
    # GDT_CInt16 = 8, GDT_CInt32 = 9, GDT_CFloat32 = 10, GDT_CFloat64 = 11,
    #GDT_Byte
    if input_image_obj.GetGDALDataType() == 1:
        # io_function.copy_file_to_dst(input_image,output_image)
        output_image = input_image
        return output_image

    (max_value_list,
     min_value_list) = RSImage.get_image_max_min_value(input_image)
    if max_value_list is False or min_value_list is False:
        return False
    input_image_obj = None

    # CommandString = 'gdal_translate  -r bilinear -ot Byte -scale  ' + input_image + ' '+output_image
    # return basic.exec_command_string_one_file(CommandString,output_image)
    args_list = ['gdal_translate', '-r', 'bilinear', '-ot', 'Byte']
    for band_index in range(0, len(max_value_list)):
        args_list.append('-b')
        args_list.append(str(band_index + 1))
        args_list.append('-scale')
        args_list.append(str(min_value_list[band_index]))
        args_list.append(str(max_value_list[band_index]))
        args_list.append(str(1))
        args_list.append(str(254))
    args_list.append(input_image)
    args_list.append(output_image)

    return basic.exec_command_args_list_one_file(args_list, output_image)
コード例 #2
0
    def compose_two_image(self, main_image, second_image, nodata):

        if io_function.is_file_exist(main_image) is False:
            return False
        if io_function.is_file_exist(second_image) is False:
            return False
        main_img = RSImageclass()
        if main_img.open(main_image) is False:
            return False
        width_main = main_img.GetWidth()
        height_main = main_img.GetHeight()
        bandcount_main = main_img.GetBandCount()

        sec_img = RSImageclass()
        if sec_img.open(second_image) is False:
            return False
        width_sec = sec_img.GetWidth()
        height_sec = sec_img.GetHeight()
        bandcount_sec = sec_img.GetBandCount()

        if width_main != width_sec or height_main != height_sec or bandcount_main != bandcount_sec:
            basic.outputlogMessage(
                'Error: The dimension of two composed images is different')
            return False
        if main_img.GetGDALDataType() != sec_img.GetGDALDataType(
        ) or main_img.GetGDALDataType() != 6:
            basic.outputlogMessage(
                'Error: The Data type of two composed imagaes is different or is not float'
            )
            return False

        outputfile = io_function.get_name_by_adding_tail(main_image, 'comp')
        imagenew = RSImageclass()
        width = width_main
        height = height_main
        if not imagenew.New(outputfile, width, height, bandcount_main,
                            main_img.GetGDALDataType()):
            return False
        for i in range(0, bandcount_main):
            bandindex = i + 1
            band_main_str = main_img.ReadbandData(bandindex, 0, 0, width,
                                                  height,
                                                  main_img.GetGDALDataType())
            band_sec_str = sec_img.ReadbandData(bandindex, 0, 0, width, height,
                                                sec_img.GetGDALDataType())

            band_main_data = struct.unpack('f' * width * height, band_main_str)
            band_main_numpy = numpy.asarray(band_main_data)

            band_sec_data = struct.unpack('f' * width * height, band_sec_str)
            band_sec_numpy = numpy.asarray(band_sec_data)

            compose_loc = numpy.where(
                (numpy.fabs(band_main_numpy - nodata) < 0.0001)
                & (numpy.fabs(band_sec_numpy - nodata) > 0.0001))
            band_main_numpy[compose_loc] = band_sec_numpy[compose_loc]
            basic.outputlogMessage('outputfortest2: compose_loc_num = %d' %
                                   numpy.array(compose_loc).size)

            templist = band_main_numpy.tolist()
            band_composed_str = struct.pack('%sf' % width * height, *templist)
            if imagenew.WritebandData(bandindex, 0, 0, width, height,
                                      band_composed_str,
                                      imagenew.GetGDALDataType()) is False:
                return False
            imagenew.SetBandNoDataValue(bandindex, nodata)

        imagenew.SetGeoTransform(main_img.GetGeoTransform())
        imagenew.SetProjection(main_img.GetProjection())

        main_img = None
        sec_img = None
        imagenew = None

        return outputfile
コード例 #3
0
ファイル: geometryProcess.py プロジェクト: yghlc/EDSR-PyTorch
def setGCPsfromptsFile(imagefile, projection, GeoTransform, ptsfile):
    if not is_file_exist(imagefile):
        return False
    image = RSImageclass()
    if not image.open(imagefile):
        return False
    ngcpcount = image.ds.GetGCPCount()
    if ngcpcount > 0:
        basic.outputlogMessage(
            'warning: The file already have GCP,GCP count is ' +
            str(ngcpcount))

    allgcps = []
    inputfile_object = open(ptsfile, 'r')
    all_points = inputfile_object.readlines()
    for linestr in all_points:
        if linestr[0:1] == '#' or linestr[0:1] == ';' or len(linestr) < 2:
            continue
        if len(allgcps) >= 10000:
            basic.outputlogMessage(
                'warning: the count of gcps already greater than 10000, and ignore the others to make geotiff work correctly'
            )
            continue
        tiepointXY = linestr.split()
        base_x = float(tiepointXY[0])
        base_y = float(tiepointXY[1])
        Xp = GeoTransform[
            0] + base_x * GeoTransform[1] + base_y * GeoTransform[2]
        Yp = GeoTransform[
            3] + base_x * GeoTransform[4] + base_y * GeoTransform[5]

        warp_x = float(tiepointXY[2])
        warp_y = float(tiepointXY[3])
        info = 'GCPbysiftgpu_%d' % len(allgcps)
        id = str(len(allgcps))
        gcp1 = gdal.GCP(Xp, Yp, 0, warp_x, warp_y, info, id)
        allgcps.append(gcp1)
    inputfile_object.close()

    Outputtiff = imagefile.split('.')[0] + '_new.tif'
    format = "GTiff"
    driver = gdal.GetDriverByName(format)
    metadata = driver.GetMetadata()
    if metadata.has_key(
            gdal.DCAP_CREATE) and metadata[gdal.DCAP_CREATE] == 'YES':
        basic.outputlogMessage('Driver %s supports Create() method.' % format)
    else:
        basic.outputlogMessage('Driver %s not supports Create() method.' %
                               format)
        return False
    # if metadata.has_key(gdal.DCAP_CREATECOPY) and metadata[gdal.DCAP_CREATECOPY] == 'YES':
    #     syslog.outputlogMessage('Driver %s supports CreateCopy() method.' % format)

    # dst_ds = driver.CreateCopy(Outputtiff, dataset, 0)
    datatype = image.GetGDALDataType()
    dst_ds = driver.Create(Outputtiff, image.GetWidth(), image.GetHeight(),
                           image.GetBandCount(), datatype)
    for bandindex in range(0, image.GetBandCount()):
        bandobject = image.Getband(bandindex + 1)
        banddata = bandobject.ReadRaster(0, 0, image.GetWidth(),
                                         image.GetHeight(), image.GetWidth(),
                                         image.GetHeight(), datatype)
        #byte
        # if banddata is 1:
        #     bandarray = struct.unpack('B'*image.GetWidth()*image.GetHeight(), banddata)
        dst_ds.GetRasterBand(bandindex + 1).WriteRaster(
            0, 0, image.GetWidth(), image.GetHeight(), banddata,
            image.GetWidth(), image.GetHeight(), datatype)

    dst_ds.SetGCPs(allgcps, projection)

    # if I have set the GCPs, should not do this again, or SetGCPs will be undo
    # dst_ds.SetGeoTransform(image.GetGeoTransform())
    # dst_ds.SetProjection(image.GetProjection())

    if not os.path.isfile(Outputtiff):
        basic.outputlogMessage(
            'result file not exist, the operation of create set gcp failed')
        return False
    dst_ds = None
    image = None

    return Outputtiff