1. 灵感来源
开发中因为一些极端的原因需要用到修改精灵图的方法,可是毕竟是图片,p图不能保证修改后的正确性,python具备图片处理等功能,另外精灵图附带一个json文件,可以获取到某个图片位置,所以有了此想法
2. 素材准备
- 精灵图图片
- 精灵图json文件
- 要替换的图片文件
- 安装python环境的电脑
3. 基本操作
- 新建文件夹,作为项目路径
- 安装虚拟环境
- 进入虚拟环境
- 新建py文件夹(如spr.py)
- 安装必要的模块
- 书写代码
- 运行测试
- 打包发布
4. 安装的模块
pip install virtualenv
虚拟环境pip install pillow
Python图像处理库pip install pyqt5
python 的GUI库pip install pyinstaller
打包exe的库
5. 具体代码
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFileDialog, QLineEdit, QLabel, QComboBox, QHBoxLayout
from PyQt5.QtGui import QIcon
from PIL import Image
import json
import sys
class SpriteReplacer(QWidget):
def __init__(self):
super().__init__()
self.sprite_png = None
self.sprite_json = None
self.replace_png = None
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.combo_box = QComboBox()
self.combo_box.setMinimumWidth(200)
self.combo_box_label = QLabel()
btn1 = QPushButton('选择精灵图', self)
btn1.setMinimumWidth(200)
btn1.clicked.connect(self.select_sprite_png)
self.btn1_label = QLabel()
btn2 = QPushButton('选择json文件', self)
btn2.setMinimumWidth(200)
btn2.clicked.connect(self.select_sprite_json)
self.btn2_label = QLabel()
btn3 = QPushButton('选择要替换的图片', self)
btn3.setMinimumWidth(200)
btn3.clicked.connect(self.select_replace_png)
self.btn3_label = QLabel()
btn4 = QPushButton('确认', self)
btn4.setMinimumWidth(200)
btn4.clicked.connect(self.replace_sprite)
layout.addLayout(self.create_hbox(btn1, self.btn1_label))
layout.addLayout(self.create_hbox(btn2, self.btn2_label))
layout.addLayout(self.create_hbox(self.combo_box, self.combo_box_label))
layout.addLayout(self.create_hbox(btn3, self.btn3_label))
layout.addWidget(btn4)
self.setLayout(layout)
def create_hbox(self, widget1, widget2):
hbox = QHBoxLayout()
hbox.addWidget(widget1)
hbox.addWidget(widget2)
return hbox
def select_sprite_png(self):
self.sprite_png, _ = QFileDialog.getOpenFileName(self, '选择精灵图')
if self.sprite_png:
self.btn1_label.setText('✔️')
def select_sprite_json(self):
self.sprite_json, _ = QFileDialog.getOpenFileName(self, '选择json文件')
if self.sprite_json:
self.btn2_label.setText('✔️')
# 加载json文件并更新下拉选择框的选项
with open(self.sprite_json, 'r') as f:
sprite_info = json.load(f)
self.combo_box.clear()
self.combo_box.addItems(sprite_info.keys())
def select_replace_png(self):
self.replace_png, _ = QFileDialog.getOpenFileName(self, '选择要替换的图片')
if self.replace_png:
self.btn3_label.setText('✔️')
def replace_sprite(self):
replace_name = self.combo_box.currentText()
# 这里是你的replace_sprite函数
replace_sprite(self.sprite_png, self.sprite_json, self.replace_png, replace_name)
def replace_sprite(sprite_png, sprite_json, replace_png, replace_name):
# 打开精灵图和要替换的图片
sprite_img = Image.open(sprite_png)
replace_img = Image.open(replace_png)
# 从json文件中获取精灵图信息
with open(sprite_json, 'r') as f:
sprite_info = json.load(f)
# 找到要替换的精灵图信息
if replace_name in sprite_info:
# 获取精灵图的位置和大小
x = sprite_info[replace_name]['x']
y = sprite_info[replace_name]['y']
w = sprite_info[replace_name]['width']
h = sprite_info[replace_name]['height']
# 确保替换的图片大小和精灵图中的图片大小一致
if replace_img.size != (w, h):
replace_img = replace_img.resize((w, h))
# 在精灵图中替换图片
sprite_img.paste(replace_img, (x, y))
# 保存新的精灵图
sprite_img.save('new_sprite.png')
return 'new_sprite.png'
def main():
app = QApplication(sys.argv)
ex = SpriteReplacer()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()