3

I am trying to automate the watermarking of thousands of georeferenced TIFF files with a Python script. The files have related tfwx world files and aux.xml files. When I run the code the spatial referencing seems to be corrupted. The cell size of the output TIFF seems to be 200x larger. I can then run arcpy.Rescale_management to recover their proper size, but they are still incorrectly placed. I am a noob, so I apologize if this is coming across as vague or ignorant. I have tried several workarounds without luck and I just want to make sure I'm not missing something easy.

Here is a stripped down version of the code I am using:

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)
1

You can use gdal/ogr to georeference your output rasters using the metadata from the input rasters.

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])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.