最近看到mcmod百科上有个叫成就生成器的东西,于是也想用Python做一个。
大致思路:
一张成就图像,需要有底图,图标和和说明文字三个部分。
成就的图标比较难搞,我使用的是Minecraft Wiki上的物品栏图标,大致思路是先把Sprite全图下载下来,然后再从Wiki的Lua脚本里获取物品名和图标位置的映射关系,然后保存在一个字典里备用。
因为成就的底图有透明度,所以不能上传到博客园的相册里,于是我把它放在了其他图床(SM.MS)上,地址见代码(base_url)。
Minecraft的英文字体(minecraft.otf)在网上是比较好找的,下载后和脚本文件放在同一目录下即可。不过,现在网上还是找不到Minecraft原生的中文字体(即像素化的仿宋字体),所以要显示中文,就只能用系统自带的simsun.ttc将就一下了。
注意图像的绘制需要用到Pillow模块,html的处理和提取需要BeautifulSoup模块,需使用pip命令安装。
代码如下:
from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup
from PIL import Image, ImageFont, ImageDraw
import re, string, os, requests
size = 32
listsize = 32
page_url = r'https://minecraft.gamepedia.com/Module:InvSprite'
sprite_url = r'https://static.wikia.nocookie.net/minecraft_gamepedia/images/4/44/InvSprite.png/revision/latest'
base_url = r'https://i.loli.net/2020/09/27/zwIY6nVJRmqbHaL.png' #使用SM.MS储存成就底图
catdict = {}
itemdict = {}
def init():
#创建资源文件夹...
if (not os.path.exists(r'./adv-maker/')):
os.mkdir(r'./adv-maker/')
#下载图标文件...
if (not os.path.exists(r'./adv-maker/inv-sprite.png')):
print('Downloading Sprites file...')
urlretrieve(sprite_url, './adv-maker/inv-sprite.png')
#下载成就底图...
if (not os.path.exists(r'./adv-maker/advancement-base.png')):
print('Downloading Advancement Background Image...')
r = requests.get(base_url)
with open(r'./adv-maker/advancement-base.png','wb') as bim:
bim.write(r.content)
#把Wiki上的Lua脚本先搞出来...
if (not os.path.exists(r'./adv-maker/pos-info.txt')):
resp = urlopen(page_url)
print('Downloading HTML file...')
cont = resp.read()
soup = BeautifulSoup(cont,'html.parser')
print('Extracting Lua script...')
src = soup.select(".mw-code")[0].prettify().replace('&','&')
print('Saving the script...')
with open(r'./adv-maker/pos-info.txt','w+') as f:
f.write(src)
print('Reading Lua script...')
f = open(r'./adv-maker/pos-info.txt','r')
src = f.read()
f.close()
print('Getting Position Info...')
#...然后提取物品名称、物品分类、在图中的位置等信息,存起来
pat1 = re.compile(r'^\s*{ name = \'(.*)\', id = (.*) }',re.M)
pat2 = re.compile(r'^\s*(.*) = { pos = (.*), section = (.*) }',re.M)
res1 = pat1.findall(src)
res2 = pat2.findall(src)
for cat in res1:
catdict[str(cat[1])] = cat[0]
for itm in res2:
#print('%-45s%-35s%-20s' %(itm[0][2:-2] if(itm[0][0]=='[') else itm[0], catdict[itm[2]], itm[1])) 如果要显示物品列表,把这行的注释符号去掉
itemdict[(itm[0][2:-2] if(itm[0][0]=='[') else itm[0])] = int(itm[1])
def trans_paste(bg,fg,box=(0,0)):
trans = Image.new("RGBA",bg.size)
trans.paste(fg,box,mask=fg)
nim = Image.alpha_composite(bg,trans)
return nim
def show(tar="???",text1="Advancement Made!",color1=(255,255,0),text2="Minecraft Advancements!",color2=(255,255,255)):
idx = 1
if (tar in itemdict):
idx = itemdict[tar]
posx = int((idx - 1) // listsize)
posy = (idx - 1 + listsize) % listsize
print(idx,posx,posy)
sprite = srci.crop((posy*size,posx*size,(posy+1)*size,(posx+1)*size))
box = (17, 16, 17 + size, 16 + size)
resi = trans_paste(advi,sprite,box)
drw = ImageDraw.Draw(resi)
drw.text((60, 12), text1, font=fnt, fill=color1)
drw.text((60, 34), text2, font=fnt, fill=color2)
resi.show()
def main():
init()
global srci, advi, fnt
srci = Image.open('./adv-maker/inv-sprite.png').convert('RGBA')
advi = Image.open('./adv-maker/advancement-base.png').convert('RGBA')
fnt = ImageFont.truetype(r'minecraft.otf', 20)
#fnt = ImageFont.truetype("simsun.ttc", 19) Use this to display Chinese characters
#show('Wooden Pickaxe',text2='Stone Age')
show(text1='Advancement Made!',text2='What the hell is this?')
if __name__ == '__main__':
main()
# print(__name__)
输出:
至此,一个自制的Minecraft成就生成器就算完成了,撒花!
SPECIAL THANKS TO: