日常里很多"小事"单独做不值当、攒多了又烦:一堆文本编码不对、想看日历还得开应用、配色拿不准、图片想换个风格、要做个图标、想要张壁纸。下面 6 个脚本代码完整、依赖常见,复制回去改改就能跑。
1. 批量把文本编码转成 UTF-8(标准库,零安装)
从别人机器拷来的 .txt 常常是 GBK,你的编辑器打开全是乱码。这个脚本把一个文件夹里的文本统一转成 UTF-8,一劳永逸。
(零依赖,标准库 codecs 即可,无需 pip install)
import os, codecs
defconvert_encoding(folder, src="gbk", dst="utf-8"):
for f in os.listdir(folder):
ifnot f.endswith(".txt"):
continue
p = os.path.join(folder, f)
text = codecs.open(p, "r", src).read()
codecs.open(p, "w", dst).write(text)
print(f"{f}: {src} -> {dst}")
convert_encoding("./texts")
小提示:src / dst 换成你实际遇到的编码(常见 gbk、utf-8、utf-8-sig);转码前先备份原文件,编错方向会乱码且不可逆。
2. 把当月日历画成一张图片(标准库 + Pillow)
不想开日历应用?用标准库 calendar 取出当月排版,再用 Pillow 渲染成图片,钉在桌面或做素材都行。
(需 pip install Pillow,若已装可跳过)
import calendar
from PIL import Image, ImageDraw, ImageFont
defmake_calendar(year, month, out="calendar.png"):
text = calendar.month(year, month)
img = Image.new("RGB", (420, 360), "white")
d = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("arial.ttf", 18)
except Exception:
font = ImageFont.load_default()
d.text((20, 20), text, fill="black", font=font, spacing=6)
img.save(out)
print(f"已生成 {out}")
make_calendar(2026, 8)
小提示:calendar.month 输出自带星期表头;spacing 控制行距;想显示中文星期,把 arial.ttf 换成系统里的中文字体 .ttf 路径。
3. 从一张图里提取主色调(colorthief)
做配色、找灵感时,让脚本告诉你图里占主导的几个颜色,比肉眼估准得多。
(需 pip install colorthief,它会自动带上 Pillow)
from colorthief import ColorThief
defpalette(path, count=5):
ct = ColorThief(path)
for rgb in ct.get_palette(color_count=count):
print(rgb, "#%02x%02x%02x" % rgb)
palette("photo.jpg")
小提示:返回的是 RGB 元组,上面顺手转成了十六进制,方便直接粘进设计工具;count 控制取几个主色,默认 5 个。
4. 把照片一键变成素描风(Pillow)
灰度加边缘检测,几行就出"手绘线条"效果,比滤镜 APP 还快。
(需 pip install Pillow)
from PIL import Image, ImageOps, ImageFilter
defsketch(path, out="sketch.png"):
img = Image.open(path).convert("L") # 先转灰度
edge = img.filter(ImageFilter.FIND_EDGES) # 再提边缘
edge.save(out)
print(f"已生成 {out}")
sketch("photo.jpg")
小提示:convert("L") 负责灰度;FIND_EDGES 出素描感线条;想更柔和换成 ImageFilter.CONTOUR,或先 GaussianBlur 再提边缘减少噪点。
5. 批量把图片转成 ICO 图标(Pillow)
做网站、做小工具常要 favicon 或应用图标,一张 PNG 进去,几张不同尺寸的 .ico 出来。
(需 pip install Pillow)
import os
from PIL import Image
defto_ico(folder, size=256):
for f in os.listdir(folder):
ifnot f.lower().endswith((".png", ".jpg", ".jpeg")):
continue
p = os.path.join(folder, f)
img = Image.open(p).convert("RGBA")
img.save(os.path.splitext(p)[0] + ".ico", sizes=[(size, size)])
print(f"{f} -> .ico")
to_ico("./icons")
小提示:sizes 决定生成的图标分辨率,网站 favicon 常用 32 / 64 / 256;convert("RGBA") 保留透明区域,白底图也能透。
6. 生成一张随机渐变壁纸(标准库 + Pillow)
想要张不撞脸的纯色渐变壁纸?随机两组颜色,让脚本按像素插值铺满,每次都不同。
(需 pip install Pillow;random 为标准库)
import random
from PIL import Image
defgradient(out="wallpaper.png", w=1920, h=1080):
img = Image.new("RGB", (w, h))
top = [random.randint(0, 255) for _ in range(3)]
bot = [random.randint(0, 255) for _ in range(3)]
px = img.load()
for y in range(h):
t = y / h
for x in range(w):
px[x, y] = (
int(top[0] + (bot[0] - top[0]) * t),
int(top[1] + (bot[1] - top[1]) * t),
int(top[2] + (bot[2] - top[2]) * t),
)
img.save(out)
print(f"已生成 {out}")
gradient()
小提示:上面是上下渐变,t = y / h;想要斜向把 t 换成 (x + y) / (w + h);像素级循环在大分辨率下较慢,先拿小尺寸预览。
这 6 个脚本覆盖文件编码、桌面日历、配色提取、图像风格化、图标生成、壁纸制作,都是平时顺手就能用上的需求。挑一个最戳你的先跑,改改路径和参数,它就是你的专属小工具。