python实现替换精灵图某张图片

1. 灵感来源

开发中因为一些极端的原因需要用到修改精灵图的方法,可是毕竟是图片,p图不能保证修改后的正确性,python具备图片处理等功能,另外精灵图附带一个json文件,可以获取到某个图片位置,所以有了此想法

2. 素材准备

  • 精灵图图片
  • 精灵图json文件
  • 要替换的图片文件
  • 安装python环境的电脑

3. 基本操作

  1. 新建文件夹,作为项目路径
  2. 安装虚拟环境
  3. 进入虚拟环境
  4. 新建py文件夹(如spr.py)
  5. 安装必要的模块
  6. 书写代码
  7. 运行测试
  8. 打包发布

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值