import os
from PIL import Image
import json
# 读取图片
image_path = "14085874e.png" # 替换为你的图片路径
image = Image.open(image_path)
print(f"Original image size: {image.size}") # 打印原始图像尺寸
# 创建保存文件夹
output_folder = "images"
os.makedirs(output_folder, exist_ok=True)
# 转换为json格式
data = [
1,
[
"14085874e@6c48a"
],
[
"_textureSource"
],
[
"cc.SpriteFrame"
],
0,
[
[
[
{
"name": "img_guang01",
"rect": {
"x": 69,
"y": 3,
"width": 452,
"height": 34
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 452,
"height": 34
},
"rotated": 'true',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "notice_BG2",
"rect": {
"x": 3,
"y": 114,
"width": 107,
"height": 46
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 107,
"height": 46
},
"rotated": 'true',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "Loading_Quan 1",
"rect": {
"x": 3,
"y": 48,
"width": 60,
"height": 60
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 60,
"height": 60
},
"rotated": 'false',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "lightframe_bg",
"rect": {
"x": 109,
"y": 265,
"width": 997,
"height": 87
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 1001,
"height": 91
},
"rotated": 'true',
"capInsets": [
50,
0,
50,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "btn_confirm",
"rect": {
"x": 3,
"y": 461,
"width": 256,
"height": 84
},
"offset": {
"x": 0.5,
"y": -0.5
},
"originalSize": {
"width": 259,
"height": 87
},
"rotated": 'true',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "lightframe_b",
"rect": {
"x": 202,
"y": 3,
"width": 1001,
"height": 91
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 1001,
"height": 91
},
"rotated": 'true',
"capInsets": [
50,
0,
50,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "black",
"rect": {
"x": 3,
"y": 3,
"width": 1,
"height": 1
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 1,
"height": 1
},
"rotated": 'false',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "btn_qx",
"rect": {
"x": 3,
"y": 10,
"width": 31,
"height": 32
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 31,
"height": 32
},
"rotated": 'false',
"capInsets": [
15,
11,
15,
11
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "fra_diban09",
"rect": {
"x": 3,
"y": 1268,
"width": 696,
"height": 418
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 696,
"height": 418
},
"rotated": 'true',
"capInsets": [
300,
78,
300,
35
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "btn_grey",
"rect": {
"x": 109,
"y": 3,
"width": 256,
"height": 85
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 256,
"height": 85
},
"rotated": 'true',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
],
[
[
{
"name": "btn_category_unselect",
"rect": {
"x": 3,
"y": 227,
"width": 160,
"height": 56
},
"offset": {
"x": 0,
"y": 0
},
"originalSize": {
"width": 160,
"height": 56
},
"rotated": 'true',
"capInsets": [
0,
0,
0,
0
],
"packable": 'true',
"pixelsToUnit": 100,
"pivot": {
"x": 0.5,
"y": 0.5
},
"meshType": 0
}
],
[
0
],
0,
[
0
],
[
0
],
[
0
]
]
]
]
# 遍历裁剪信息
for item in data[5]:
for sub_item in item:
if isinstance(sub_item, list) and len(sub_item) > 0 and isinstance(sub_item[0], dict):
sprite_info = sub_item[0]
name = sprite_info.get("name")
rect = sprite_info.get("rect")
offset = sprite_info.get("offset", {"x": 0, "y": 0}) # 获取偏移量,默认0
rotated_str = sprite_info.get("rotated", "false")
rotated = rotated_str.lower() == 'true'
print(name, rotated)
if name and rect:
x = rect["x"]
y = rect["y"]
width = rect["width"]
height = rect["height"]
# 验证坐标和尺寸是否合理
if x < 0 or y < 0 or x + width > image.width or y + height > image.height:
# 尝试交换长和宽
new_width = height
new_height = width
if x < 0 or y < 0 or x + new_width > image.width or y + new_height > image.height:
print(f"裁剪区域 x={x}, y={y}, width={width}, height={height} 超出图片范围,交换尺寸后仍超出,跳过 {name} 的裁剪!")
continue
else:
print(f"裁剪区域 x={x}, y={y}, width={width}, height={height} 超出图片范围,尝试交换长和宽后继续裁剪。")
width = new_width
height = new_height
print(f"Cropping rectangle: x={x}, y={y}, width={width}, height={height}")
# 裁剪区域
box = (x, y, x + width, y + height)
# 裁剪图片
try:
cropped_image = image.crop(box)
except ValueError as e:
print(f"裁剪 {name} 时出错: {e}")
continue
# 如果需要旋转
if rotated:
try:
cropped_image = cropped_image.rotate(90, expand='true',)
new_width, new_height = cropped_image.size
cropped_image = cropped_image.crop((0, 0, new_width, new_height))
except Exception as e:
print(f"旋转 {name} 时出错: {e}")
continue
# 保存图片
output_path = os.path.join(output_folder, f"{name}.png")
try:
cropped_image.save(output_path)
print(f"Saved: {output_path}")
except OSError as e:
print(f"保存 {name} 到 {output_path} 时出错: {e}")
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END








暂无评论内容