forked from TheHeadlessSourceMan/gimpFormats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcatXCF.py
77 lines (71 loc) · 1.85 KB
/
catXCF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""https://github.com/FHPythonUtils/GimpFormats/issues/4."""
from __future__ import annotations
from gimpformats.gimpXcfDocument import GimpDocument
#### XCF ####
def catXCF(file: str) -> None:
"""Open an .xcf file into a layered image."""
project = GimpDocument(file)
# Iterate the layers and create a list of layers for each group, then remove
# these from the project layers
layers = project.layers[::-1]
index = 0
groupIndex = 0
groupLayers = [[]]
while index < len(layers):
layerOrGroup = layers[index]
if layerOrGroup.isGroup:
index -= 1
while layers[index].itemPath is not None:
layer = layers[index]
groupLayers[groupIndex].append(
(
"LAYER:" + layer.name,
layer.image,
(layer.width, layer.height),
(
layer.xOffset - layerOrGroup.xOffset,
layer.yOffset - layerOrGroup.yOffset,
),
layer.opacity,
layer.visible,
layer.blendMode,
)
)
layers.pop(index)
index -= 1
index += 2
groupIndex += 1
groupLayers.append([])
else:
index += 1
# Iterate the clean project layers and add the group layers in
groupIndex = 0
layersAndGroups = []
for layerOrGroup in layers:
if layerOrGroup.isGroup:
layersAndGroups.append(
(
"GROUP:" + layerOrGroup.name,
groupLayers[groupIndex][::-1],
(layerOrGroup.width, layerOrGroup.height),
(layerOrGroup.xOffset, layerOrGroup.yOffset),
layerOrGroup.opacity,
layerOrGroup.visible,
layerOrGroup.blendMode,
)
)
groupIndex += 1
else:
layersAndGroups.append(
(
"LAYER:" + layerOrGroup.name,
layerOrGroup.image,
(layerOrGroup.width, layerOrGroup.height),
(layerOrGroup.xOffset, layerOrGroup.yOffset),
layerOrGroup.opacity,
layerOrGroup.visible,
layerOrGroup.blendMode,
)
)
catXCF("test_files/test.xcf")
catXCF("test_files/base24.xcf")