-
Notifications
You must be signed in to change notification settings - Fork 1
/
layerstodivs.py
executable file
·152 lines (135 loc) · 5.02 KB
/
layerstodivs.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: <utf-8> -*-
#
# Quite flexible GIMP python-fu script to export layers.
# It supports all GIMP formats, depends on file save options
#
# Version 1.0
#
# Authors: Lars Pontoppidan <[email protected]>, headwar
# Copyright (C) 2012 Lars Pontoppidan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from gimpfu import *
import os, re, cgi
from os.path import expanduser
def dump(obj):
print "type: "+type(obj) +"\nattributes: "+ dir(obj)
def dbg(message):
print message
def export_layers(img, draw, path, visibility, filetype, css, csshover, dataattr, backlayer):
def traverse(layer,options):
vis = options['visibility']
include_layer = (vis == 0 or (vis == 1 and layer.visible) or (vis == 2 and not layer.visible))
include_group_layer = (vis == 0 or (vis == 1 and layer.visible) or (vis == 2 and not layer.visible))
if (type(layer) == gimp.GroupLayer):
if include_group_layer:
for l in layer.layers:
traverse(l,options)
else:
if include_layer:
options['keepers'].append(layer)
layers = []
options = {
'visibility' : visibility,
'keepers' : layers
}
for layer in img.layers:
traverse(layer,options)
# create the html file
f = open(path+"/myhtml.html", "w");
f.write("<!doctype html>\n<html>\n\t<head>\n\t\t<style>\n\t\t\t.container { position:relative; }\n\t\t\t.hoverimg { position: absolute; border: none; "+css+" }\n\t\t\t.hoverimg:hover{ "+csshover+" }\n\t\t</style>\n\t</head>\n\n\t<body>\n")
if not backlayer:
f.write("\t\t<div class=\"container\">\n");
filetypes = [".png",".jpg",".gif"]
filenames = []
for layer in layers[::-1]:
nicename = ""
name = pdb.gimp_item_get_name(layer)
# if an @ is in the layer name, the part after the @ char is the link
if "@" in name:
layernameurl = name.split("@")
nicename = layernameurl[0]
linkname = layernameurl[1]
else:
nicename = name
linkname = ""
# for the images filenames, remove all non alphanum + .-_ chars
filename= re.sub(r'[^.-_a-zA-Z0-9]', '', nicename)
# avoid duplicate filenames
while filename in filenames:
filename=filename+'0'
filenames.append(filename)
# add the file type
filename=filename+filetypes[filetype]
pathfilename = path+"/"+filename
# if the last layer (hence now the first, as the 'for' is in reverse order) is a background:
if backlayer:
myclass = "container"
f.write("\t\t")
else:
myclass = "hoverimg"
f.write("\t\t\t")
# if there is a link in the layer name:
if linkname:
f.write("<a href=\""+linkname+"\" alt=\""+cgi.escape(nicename).encode("ascii", "xmlcharrefreplace")+"\">")
# now, create the div with absolute positionning:
div_line = ""
div_line += "<div "
div_line += "class=\"" + myclass + "\" "
div_line += "style=\"left: "+str(layer.offsets[0]) + "px; top: "+str(layer.offsets[1])+"px; width: "+str(layer.width) + "px; height: "+str(layer.height)+"px; background-image: url('"+filename+"');\" "
if dataattr:
div_line += "data-layer-name=\"" + nicename + "\" "
div_line += ">"
f.write(div_line)
# f.write("<div class=\""+myclass+"\" style=\"left: "+str(layer.offsets[0])+
# "px; top: "+str(layer.offsets[1])+"px; width: "+str(layer.width)+
# "px; height: "+str(layer.height)+"px; background-image: url('"+filename+"');\">")
if backlayer:
backlayer = 0
else:
f.write("</div>")
# if there is a link in the layer name:
if linkname:
f.write("</a>")
f.write("\n")
pdb.gimp_file_save(img, layer, pathfilename, pathfilename)
f.write("\t\t</div>\n\t</body>\n</html>\n")
f.close()
register(
"export_layers_css",
"Export layers to CSS. Each file will be named after its layer. Name your layers filename@url to auto create a link to url.",
"Script to export separate layers to CSS blocks",
"Lars Pontoppidan, Headwar",
"Lars Pontoppidan, Headwar",
"2012, 2017",
"E_xport layers to CSS...",
"*",
[
(PF_IMAGE, "img", "Input image", None),
(PF_DRAWABLE, "draw", "Input drawable", None),
(PF_DIRNAME, "path", "Output directory", expanduser("~")),
(PF_OPTION, "visibility", "Visibility", 1, ["All", "Visible", "Invisible"]),
(PF_OPTION, "filetype", "File type", 0, [".png",".jpg",".gif"]),
(PF_STRING, "css", "Custom CSS", ""),
(PF_STRING, "csshover", "Custom CSS on hover", "z-index:100; box-shadow: 0 0 16px 10px yellow;"),
(PF_BOOL, "dataattr", "Include layer name as data attr", True),
(PF_TOGGLE, "backlayer", "The lowest layer is a background", 1)
],
[],
export_layers,
"<Image>/File/Save/"
)
main()