3 votos

La marca de agua TIFF con almohada en Python rompe la georreferenciación

Estoy intentando automatizar el marcado de agua de miles de archivos TIFF georreferenciados con un script de Python. Los archivos tienen archivos tfwx world relacionados y archivos aux.xml. Cuando ejecuto el código la referenciación espacial parece estar corrupta. El tamaño de la celda del TIFF de salida parece ser 200 veces mayor. Luego puedo ejecutar arcpy.Rescale_management para recuperar su tamaño adecuado, pero siguen estando mal colocadas. Soy un novato, así que me disculpo si esto es venir a través de vagos o ignorantes. He intentado varias soluciones sin suerte y sólo quiero asegurarme de que no me estoy perdiendo algo fácil.

Esta es una versión reducida del código que estoy utilizando:

from PIL import Image, ImageDraw, ImageFont
import os, sys, arcpy
from arcpy import env

env.workspace = r'D:\mbrush\scratch\tools\CalTrans\testing'
workspace = env.workspace
env.overwriteOutput = True

# copywrite logo and name
text = u'\u00a9' + ' Company Name'

# iterate through tiffs and watermark
for file in os.listdir(workspace):
    if file.endswith('.tif'):

        # open image
        im = Image.open(workspace + '\\' + file)

        # open image for drawing
        drawing = ImageDraw.Draw(im)

        # determine height and width of image
        width = im.size[0]
        height = im.size[1]

        # determine where to place the text
        posx = width - (500)
        posy = height - (80)
        pos = (posx, posy)

        # set font size
        fontsize = 33

        # define outfile to overwrite original
        img_out = workspace + '\\' + file

        # choose font
        font = ImageFont.truetype(r'C:/Windows/Fonts/Arial/arial.ttf', fontsize)

        # create text shadow
        shadow = (pos[0]-1, pos[1]-1)
        shadow2 = (pos[0]-2, pos[1]-2)
        shadow3 = (pos[0]+1, pos[1]+1)
        shadow4 = (pos[0]+2, pos[1]+2)
        drawing.text(shadow, text, fill=(200,200,200), font=font)
        drawing.text(shadow2, text, fill=(255,255,255), font=font)
        drawing.text(shadow3, text, fill=(200,200,200), font=font)
        drawing.text(shadow4, text, fill=(255,255,255), font=font)

        # draw watermark
        drawing.text(pos, text, fill=(3, 8, 12), font=font)

        # save image
        im.save(img_out)

1voto

user139549 Puntos 21

Puede utilizar gdal/ogr para georreferenciar sus rásteres de salida utilizando los metadatos de los rásteres de entrada.

from osgeo import gdal, ogr

src_tif = gdal.Open('input.tif')
new_tif = gdal.Open('output.tif')

ulx, xres, xskew, uly, yskew, yres  = src_tif.GetGeoTransform()
lrx = ulx + (src_tif.RasterXSize * xres)
lry = uly + (src_tif.RasterYSize * yres)

gdal.Translate('georeferenced_input.tif', new_tif, outputBounds=[ulx,uly,lrx,lry])

1voto

user3750444 Puntos 23

He podido recuperar la referenciación espacial del tif original georreferenciado utilizando gdal translate.

import gdal, osr

original = gdal.Open('path to tif')        # the original georeferenced tif
in_tif = 'path to watermarked tif'          # the one that lost spatial reference
out_tif = 'path to tif to be created'       # the georeferenced output tif

rf = original.GetSpatialRef()
gcps = original.GetGCPs()
gdal.Translate(str(out_tif), in_tif, format='GTiff', GCPs=gcps, outputSRS=rf)

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X