Pi IP Address on Desktop

Most of this was lifted from https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=259108&sid=52d7c8cd098871bd1e825a4b05f4ea94&start=25 so blame them not me when it all breaks...

Anyway, the script three quarters down is pretty good (missing a closing bracket but otherwise fine)

The version I used is here

#!/usr/bin/env python3

# this script provides a desktop wall paper with useful info, host name, lan and wan addresses
# please provide a backgrourd image having the size of your desktop as Wallpaper1.jpg in your home directory
# you may provide a logo having less than 100 Px hight as Logo2.jpg in your home directory (uncomment the add logo part).

import socket, pygame, sys, os
from requests import get
from pygame.locals import *
pygame.init()
import time

t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print(current_time)

# get host name
host_name = socket.gethostname()

# get wan address
try:
 ip = get('https://api.ipify.org').text
 wan_address = format(ip)
except:
 wan_address = "not connected"

# get lan address
try:
 from subprocess import check_output
 lan_address = check_output(['hostname', '-I']).decode().strip()
except:
 lan_address = "not connected"

# get wallpaper
image = pygame.image.load("Wallpaper1.jpg")
width,height = (image.get_rect().size)
windowSurfaceObj = pygame.display.set_mode((width, height), pygame.NOFRAME, 24)
windowSurfaceObj.blit(image, (0, 0))

# add logo
# logo = pygame.image.load("Logo2.jpg")
# logo_width,logo_height = (logo.get_rect().size)
# mask out any pixels of 0 value
# logo.set_colorkey(0, pygame.RLEACCEL)
# set transparency
# logo.set_alpha(200)
# windowSurfaceObj.blit(logo, (100, height-300))

# add host name
msg = host_name
ink =   pygame.Color(255,255,255)
fontObj = pygame.font.Font('freesansbold.ttf',30)
msgSurfaceObj = fontObj.render(msg, False,ink)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(100,height - 200 )
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)

# add lan ip
msg = lan_address
ink =   pygame.Color(255,255,255)
fontObj = pygame.font.Font('freesansbold.ttf',30)
msgSurfaceObj = fontObj.render(msg, False,ink)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(100,height - 160)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)

# add wan ip
msg = wan_address
ink =   pygame.Color(255,255,255)
fontObj = pygame.font.Font('freesansbold.ttf',30)
msgSurfaceObj = fontObj.render(msg, False,ink)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(100,height - 120)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)

# add date
msg = current_time
ink =   pygame.Color(255,255,255)
fontObj = pygame.font.Font('freesansbold.ttf',30)
msgSurfaceObj = fontObj.render(msg, False,ink)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(100,height - 80)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
# save wallpaper
pygame.image.save(windowSurfaceObj, 'wallpaper.jpg')

os.system("pcmanfm --set-wallpaper /home/pi/wallpaper.jpg")

# quit
pygame.display.quit()


I created a plain black wallpaper in paint.net and ssh'd it over (as this on a pi with a tiny 3.5" screen) then reran the command

python ~/Watermark.py

As it's in my home directory. It's pretty fun, I'll add a photo at some point.

Added a crontab too but I'm not that au fait with crontab but here it is

# Need this to tell it what to change
pi@raspberrypi:~ $ export DISPLAY=:0.0
pi@raspberrypi:~ $ sudo crontab -e

#append this line to the end
@reboot (echo $(date) >> /tmp/date.txt && sleep 90 && export DISPLAY=:0.0 && python3 /home/pi/Watermark.py >/tmp/watermarklog.log 2>&1
 && echo $(date) >> /tmp/date.txt)

And voila, success (I think - IP doesn't change often so not sure it works...)

Lots of French in this post for some reason.