75
100
right = image.width
77
102
right = image.get_guide_position(vert[j])
79
src = slice(image, image_path, image_basename, image_extension,
80
left, right, top, bottom, i, j)
82
if image_relative_path:
83
if image_relative_path.endswith('/'):
84
src_path = image_relative_path + src
86
src_path = image_relative_path + '/' + src
90
tw.cell(src_path, right - left, bottom - top)
105
(len(horz) >= 2 and (i == 0 or i == len(horz) )) or
106
(len(vert) >= 2 and (j == 0 or j == len(vert) ))
113
if (not animate or skip_stub):
114
src = (image_relative_path +
115
slice (image, None, image_path,
116
image_basename, image_extension,
117
left, right, top, bottom, i, j, ""))
120
for layer, postfix in zip (drw, ("", "hover", "clicked")):
121
src.append (image_relative_path +
122
slice(image, layer, image_path,
123
image_basename, image_extension,
124
left, right, top, bottom, i, j, postfix))
126
tw.cell(src, right - left, bottom - top, i, j, skip_stub)
92
128
left = right + cellspacing
103
def slice(image, image_path, image_basename, image_extension,
104
left, right, top, bottom, i, j):
105
src = "%s-%d-%d.%s" % (image_basename, i, j, image_extension)
139
def slice(image, drawable, image_path, image_basename, image_extension,
140
left, right, top, bottom, i, j, postfix):
142
postfix = "_" + postfix
143
src = "%s_%d_%d%s.%s" % (image_basename, i, j, postfix, image_extension)
106
144
filename = os.path.join(image_path, src)
108
temp_image = image.duplicate()
147
temp_image = image.duplicate()
148
temp_drawable = temp_image.active_layer
150
if image.base_type == INDEXED:
151
#gimp_layer_new_from_drawable doesn't work for indexed images.
152
#(no colormap on new images)
153
original_active = image.active_layer
154
image.active_layer = drawable
155
temp_image = image.duplicate()
156
temp_drawable = temp_image.active_layer
157
image.active_layer = original_active
158
temp_image.disable_undo()
159
#remove all layers but the intended one
160
while len (temp_image.layers) > 1:
161
if temp_image.layers[0] != temp_drawable:
162
pdb.gimp_image_remove_layer (temp_image, temp_image.layers[0])
164
pdb.gimp_image_remove_layer (temp_image, temp_image.layers[1])
166
temp_image = pdb.gimp_image_new (drawable.width, drawable.height,
168
temp_drawable = pdb.gimp_layer_new_from_drawable (drawable, temp_image)
169
temp_image.add_layer (temp_drawable, -1)
109
171
temp_image.disable_undo()
111
172
temp_image.crop(right - left, bottom - top, left, top)
113
pdb.gimp_file_save(temp_image, temp_image.active_layer, filename, filename)
173
if image_extension == "gif" and image.base_type == RGB:
174
pdb.gimp_image_convert_indexed (temp_image,NO_DITHER, MAKE_PALETTE, 255,
176
if image_extension == "jpg" and image.base_type == INDEXED:
177
pdb.gimp_image_convert_rgb (temp_image)
179
pdb.gimp_file_save(temp_image, temp_drawable, filename, filename)
115
181
gimp.delete(temp_image)
159
224
class TableWriter:
160
225
def __init__(self, filename, cellpadding=0, cellspacing=0, border=0,
228
self.filename = filename
162
229
self.table_attrs = {}
231
#Hellraisen IE 6 doesn't support CSS for table control.
164
232
self.table_attrs['cellpadding'] = cellpadding
165
233
self.table_attrs['cellspacing'] = cellspacing
166
234
self.table_attrs['border'] = border
168
self.capitalize = capitalize
170
self.html = file(filename, 'w')
236
self.image_prefix = os.path.basename (filename)
237
self.image_prefix = self.image_prefix.split(".")[0]
238
self.image_prefix = self.image_prefix.replace ("-", "_")
239
self.image_prefix = self.image_prefix.replace (" ", "_")
248
if os.path.exists (filename):
249
#The plug-in is running to overwrite a previous
250
#version of the file. This will parse the href targets already
251
#in the file to preserve them.
252
self.urls = self.parse_urls ()
258
self.html = open(filename, 'wt')
262
if self.url_index < len (self.urls):
264
return self.urls [self.url_index - 1]
266
#Default url to use in the anchor tags:
174
269
def write(self, s, vals=None):
177
s = s.replace('%S', '%s')
184
273
self.html.write(s + '\n')
276
out = '''<!--HTML SNIPPET GENERATED BY THE GIMP
278
WARNING!! This is NOT a fully valid HTML document, it is rather a piece of
279
HTML generated by the GIMP's py-slice plugin that should be embedded in an HTML
280
or XHTML document to be valid.
282
Replace the href targets in the anchor (<a >) for your URLS to have it working
189
287
for attr, value in self.table_attrs.iteritems():
190
out += ' %s=%s' % (attr, value)
288
out += ' %s="%s"' % (attr, value)
197
self.write('</table>')
295
self.write('</table>\n')
296
prefix = self.image_prefix
299
<script language="javascript" type="text/javascript">
300
/* Made with The GIMP */
302
/* Preload images: */
303
images_%s = new Array();
305
for image in self.images:
306
for type_ in ("plain", "hover", "clicked"):
307
if image.has_key(type_):
308
image_index = ("%d_%d_%s" %
310
image["index"][1], type_))
311
out += (" images_%s[\"%s\"] = new Image();\n" %
312
(prefix, image_index))
313
out += (" images_%s[\"%s\"].src = \"%s\";\n" %
314
(prefix, image_index, image[type_]))
317
function exchange (image, images_array_name, event)
320
images = eval (images_array_name);
325
image.src = images[name + "_plain"].src;
328
image.src = images[name + "_hover"].src;
331
image.src = images[name + "_clicked"].src;
334
image.src = images[name + "_hover"].src;
341
End of the part generated by the GIMP
200
347
def row_start(self):
203
350
def row_end(self):
204
self.write('\t</tr>')
206
def cell(self, src, width, height):
207
out = ('\t\t<td><img alt=" " src="%%s" width="%d" height="%d"></td>' %
351
self.write('</tr>\n')
353
def cell(self, src, width, height, row=0, col=0, skip_stub = False):
354
if isinstance (src, list):
355
prefix = "images_%s" % self.image_prefix
356
self.images.append ({"index" : (row, col), "plain" : src[0]})
358
out = (' <td><a href="%s"><img alt="" src="%s" ' +
359
'style="width: %dpx; height: %dpx; border-width: 0px" \n') %\
360
(self.next_url(), src[0], width, height)
361
out += 'name="%d_%d" \n' % (row, col)
363
self.images[-1]["hover"] = src [1]
364
out += """ onmouseout="exchange(this, '%s', 0);"\n""" % \
366
out += """ onmouseover="exchange(this, '%s', 1);"\n""" % \
369
self.images[-1]["clicked"] = src [2]
370
out += """ onmousedown="exchange(this, '%s', 2);"\n""" % \
372
out += """ onmouseup="exchange(this, '%s', 3);"\n""" % \
377
out += "/></a></td>\n"
381
out = (' <td><img alt=" " src="%s" style="width: %dpx; ' +
382
' height: %dpx; border-width: 0px;"></td>') % \
385
out = (' <td><a href="#"><img alt=" " src="%s" ' +
386
' style="width: %dpx; height: %dpx; border-width: 0px;">' +
387
'</a></td>') % (src, width, height)
389
def parse_urls (self):
391
This will parse any url targets in the href="XX" fields
392
of the given file and return then as a list
397
html_file = open (self.filename)
399
#Regular expression to pick everything up to the next doublequote
400
#character after finding the sequence 'href="'. The found sequences
401
#will be returned as a list by the "findall" method.
402
expr = re.compile (r"""href\=\"([^\"]*?)\"""")
403
url_list = expr.findall (html_file.read (2 ** 18))
407
#silently ignore any errors parsing this. The file being
408
#ovewriten may not be a file genrated by py-slice.
213
"Guillotine implemented ala python, with html output (based on perlotine by Seth Burgess)",
214
"Add guides to an image. Then run this. It will cut along the guides, and give you the html to reassemble the resulting images.",
416
N_("Cuts an image along its guides, creates images and a HTML table snippet"),
417
"""Add guides to an image. Then run this. It will cut along the guides,
418
and give you the html to reassemble the resulting images. If you
419
choose to generate javascript for onmouseover and clicked events, it
420
will use the lower three visible layers on the image for normal,
421
onmouseover and clicked states, in that order. If skip caps is
422
enabled, table cells on the edge of the table won't become animated,
423
and its images will be taken from the active layer.""",
218
"<Image>/Filters/Web/Py-Slice...",
221
(PF_STRING, "save_path", "The path to export the HTML to", os.getcwd()),
222
(PF_STRING, "html_filename", "Filename to export", "py-slice.html"),
223
(PF_STRING, "image_basename", "What to call the images", "pyslice"),
224
(PF_RADIO, "image_extension", "The format of the images: {gif, jpg, png}", "gif", (("gif", "gif"), ("jpg", "jpg"), ("png", "png"))),
225
(PF_TOGGLE, "separate_image_dir", "Use a separate directory for images?", FALSE),
226
(PF_STRING, "relative_image_path", "The path to export the images to, relative to the Save Path", "images/"),
227
(PF_TOGGLE, "capitalize_tags", "Capitalize HTML tags?", FALSE),
228
(PF_SPINNER, "cellspacing", "Add space between the table elements", 0, (0,15,1))
430
(PF_DIRNAME, "save-path", _("Path for HTML export"), os.getcwd()),
431
(PF_STRING, "html-filename", _("Filename for export"), "slice.html"),
432
(PF_STRING, "image-basename", _("Image name prefix"), "slice"),
433
(PF_RADIO, "image-extension", _("Image format"), "gif", (("gif", "gif"), ("jpg", "jpg"), ("png", "png"))),
434
(PF_TOGGLE, "separate-image-dir", _("Separate image folder"),
436
(PF_STRING, "relative-image-path", _("Folder for image export"), "images"),
437
(PF_SPINNER, "cellspacing", _("Space between table elements"), 0,
439
(PF_TOGGLE, "animate", _("Javascript for onmouseover and clicked"),
441
(PF_TOGGLE, "skip-caps", _("Skip animation for table caps"), True)
445
menu="<Image>/Filters/Web",
446
domain=("gimp20-python", gimp.locale_directory)