You might think it’s a dumb idea, because it probably is. Basically the transparent image outlined below is 4096x4096 PNG file, which gives a total of 16777216 pixels to work with. Our goal here is to make an IPv4 geolocation database in a PNG file, because it’s lightweight I guess. Each pixel (naively) represent a /24 subnet and the RGBA pixel value is essentially the latitude and longitude. Red and Green channels together give a signed 16bits integer representing latitude, and the Blue and Alpha channels together give another signed 16bits integer, but this time it gives the longitude.
Below is a Python script to get the GPS location of any IP address you want, as long as it’s covered by the database, which as of right now is very incomplete. So give 8.8.8.8 a try
from PIL import Image
import os, sys
#Relative path of the Python script
path = ''
for i in os.path.dirname(os.path.realpath(sys.argv[0])):
if i == chr(92):
path += '/'
else:
path += i
def ip_to_integers(ip):
parts = ip.split('.')
return [int(part) for part in parts]
def findResult(ip):
ipb = ip_to_integers(ip)
preipencode = (ipb[0] * 65536) + (ipb[1] * 256) + ipb[2]
pxH = preipencode%4096
pxV = int(preipencode/4096)
quad = img.getpixel((pxH, pxV))
lat = (quad[0]*256) + quad[1]
lon = (quad[2]*256) + quad[3]
if lat > 32767:
lat = lat - (lat >> 15 << 16)
if lon > 32767:
lon = lon - (lon >> 15 << 16)
decodedlat = (lat/32768) * 90
decodedlon = (lon/32768) * 180
return (decodedlat, decodedlon)
theip = input("IP Address: ")
img = Image.open(path + '/db.png')
result = findResult(theip)
print(result)
input() #Prevents window from abruptly closing if on Windows
So yeah.