内网科室经常打电话咨询问题,想远程看看,让报下ip地址,很多人不知道怎么看,这里就写个程序,放到开机运行里,自动将ip地址写到桌面背景图上。AI找了几个函数,拼一拼实现效果如图:
python还真挺简单好用,看下菜鸟教程就可以上手了:Python3 简介 | 菜鸟教程
就是.py源码打包成exe后文件变大了许多,无所谓了,简单方便就行。代码如下:
from PIL import Image, ImageDraw, ImageFont
import subprocess
import io
import os
import socket
import win32com.client
import win32gui
import win32con
import win32api
import ctypes
'''
pip3 install pillow -i https://mirrors.aliyun.com/pypi/simple/
pip3 install pywin32 -i https://mirrors.aliyun.com/pypi/simple/
pip3 install pyinstaller -i https://mirrors.aliyun.com/pypi/simple/ #打包工具
pyinstaller --onefile --windowed your_script.py #打包命令
'''
#【获取IP函数】
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# 不需要真正连接到一个地址,只是为了得到我们的IP地址
s.connect(('8.8.8.8', 80))
IP = s.getsockname()[0]
finally:
s.close()
return IP
# ------------------------------------------------------
# 获取IP地址
ip_address = get_ip()
#print(get_ip())
#【图片上加文本函数】
def add_text_to_image(image_path, text, output_path, font_path='simhei.ttf', font_size=50):
# 打开图片
image = Image.open(image_path)
# 创建可以在给定图像上绘图的对象
draw = ImageDraw.Draw(image)
# 加载字体,如果不指定字体路径,则使用默认字体,可能导致乱码或不支持的字体错误
try:
font = ImageFont.truetype(font_path, font_size)
except IOError:
font = ImageFont.load_default() # 如果指定字体路径无效,则使用默认字体
#print("警告:默认字体不支持中文。")
# 获取图片的宽度和高度
image_width, image_height = image.size
# 计算文本位置(右上角)
text_width = 600
text_height = 30
x = image_width - text_width - 10 # 留出一些边距
y = text_height + 10 # 留出一些边距
# 在图片上添加文本
draw.text((x+1, y+1), text, fill=(0, 0, 0), font=font) # fill颜色为白色
draw.text((x, y), text, fill='red', font=font) # fill颜色为白色
# 保存或显示图片
image.save(output_path)
#print(f"图片成功保存到 {output_path}")
# image.show() # 如果想直接显示图片,可以使用这个方法代替保存
# ------------------------------------------------------
# add_text_to_image('bzyy.jpg', 'ip地址:' + ip_address , 'bzyy2.jpg')
# 【获得当前桌面背景图片地址函数】
def get_desktop_wallpaper():
# 创建Shell对象
shell = win32com.client.Dispatch("WScript.Shell")
# 获取桌面壁纸路径
path = shell.RegRead("HKCU\\Control Panel\\Desktop\\WallPaper")
return path
# ----------------------------------------------------------------------
#print("原桌面图片:"+get_desktop_wallpaper())
deskPic = get_desktop_wallpaper()
add_text_to_image(deskPic , 'ip地址:' + ip_address , 'd:\\desktop1.jpg' )
# 【设置桌面背景图片函数】
def set_wallpaper(path):
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0)
# ----------------------------
# set_wallpaper("C:\\path\\to\\your\\image.jpg")
set_wallpaper("d:\\desktop1.jpg")
def set_wallpaper(image_path, style=2):
"""
设置桌面壁纸
:param image_path: 图片绝对路径(支持JPG/PNG/BMP)
:param style: 壁纸样式(2拉伸/0居中/6适应/10填充)
"""
# 修改注册表设置壁纸样式
reg_key = win32api.RegOpenKeyEx(
win32con.HKEY_CURRENT_USER,
"Control Panel\\Desktop",
0,
win32con.KEY_SET_VALUE
)
win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, str(style))
win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
# 应用壁纸设置
win32gui.SystemParametersInfo(
win32con.SPI_SETDESKWALLPAPER,
image_path,
win32con.SPIF_SENDCHANGE
)
#--------------------------------------------------
set_wallpaper(r"d:\\desktop1.jpg", style=6)