Skip to content

Instantly share code, notes, and snippets.

@neogeomat
Last active July 4, 2018 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neogeomat/67748803f7abfa85787ba6ef59f52373 to your computer and use it in GitHub Desktop.
Save neogeomat/67748803f7abfa85787ba6ef59f52373 to your computer and use it in GitHub Desktop.
Generates TFW or JGW file from Tif and jpg image
from Tkinter import *
from tkMessageBox import showerror
from tkFileDialog import askopenfilename
class Application(Frame):
"""A GUI to Generate Tfw file from Tiff File """
def __init__(self,master):
"""Initializes the frame"""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create buttons that do nothing"""
#create label for scale factor
self.sf=Label(self, text="Scale of map:",width=15)
self.sf.grid(row=0, column=0, sticky=E+W+N+S)
#create entry for scale factor
self.sfentry= Entry(self,width=15)
self.sfentry.grid(row=0, column =1, sticky=E+W+N+S)
#create label for scanning resolution
self.dpi=Label(self, text="Resolution of Image",width=15)
self.dpi.grid(row=1, column=0, sticky=E+W+N+S)
#create entry for scale factor
self.dpientry= Entry(self,width=15)
self.dpientry.grid(row=1, column =1, sticky=E+W+N+S)
#create browse label
self.browse= Label(self, text = "Choose File", width = 15)
self.browse.grid(row=2, column =0, sticky= E+W+N+S)
#create button for file selection
self.browsebutton=Button(self, text= "Browse file", command =self.filebrowser, width = 15)
self.browsebutton.grid(row = 2, column= 1, sticky = E+W+N+S)
#create Generate button
self.button3=Button(self, text="Generate", command=self.tfwmaker, width=30)
self.button3.grid(row=3, column=0, columnspan=2, sticky=E+W+N+S)
self.status= StringVar()
self.statusbar = Label(self, textvariable = self.status, width = 15)
self.statusbar.grid(row=4, column =0, columnspan=2, sticky= E+W+N+S)
def filebrowser(self):
import os
global filename, location, directory, extension
from tkFileDialog import askopenfilename
Tk().withdraw()
self.filename = askopenfilename()
print type(self.filename)
self.directory = os.path.dirname(os.path.realpath(self.filename))
self.name = os.path.splitext(os.path.basename(self.filename))[0]
self.extension = self.filename.rsplit(".",1)[-1]
print self.extension
os.chdir(self.directory)
print self.filename, self.directory, self.name, os.getcwd(), self.extension
self.location=Text(self, fg = 'green', height = 3)
self.location.insert(INSERT,self.filename)
self.location.grid( row = 2, column = 1, sticky = E+W+N+S)
#make tfw for image
def tfwmaker(self):
import os, time
import tkMessageBox
global sf,dpi,cellsize ,errormessage
if self.sfentry.get():
sf = float(self.sfentry.get())
else:
self.status.set("Scale Factor value empty")
# sf = 2500 #for development purpose only, change it in production
if self.dpientry.get():
dpi = float(self.dpientry.get())
else:
self.status.set("DPI value empty")
# dpi = 96 #for development purpose only, change it in production
cellsize = 0.0254*sf/dpi
# print cellsize
# print "\n"
# print self.extension
#create a new tfw file and write it
if self.extension == "tiff" or self.extension == "tif" or self.extension == "TIF" or self.extension == "TIFF" :
self.outfile = open(self.name+".tfw","w")
elif self.extension == "jpeg" or self.extension == "jpg" or self.extension == "JPEG" or self.extension == "JPG":
self.outfile = open(self.name+".jgw","w")
self.outfile.write(str(cellsize))
self.outfile.write("\n0\n")
self.outfile.write("0\n")
self.outfile.write(str(-cellsize))
self.outfile.write("\n500000\n")
self.outfile.write("3100000")
# Result Display
self.status.set("World File Generated in " + str(self.directory) + "\\" + str(self.outfile.name))
self.outfile.close()
root = Tk()
root.title("World file Maker")
app = Application(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment