home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / gimp / 2.0 / plug-ins / py-slice.py < prev    next >
Encoding:
Python Source  |  2006-07-10  |  6.7 KB  |  234 lines

  1. #!/usr/bin/env python
  2.  
  3. #   Gimp-Python - allows the writing of Gimp plugins in Python.
  4. #   Copyright (C) 2003, 2005  Manish Singh <yosh@gimp.org>
  5. #
  6. #   This program is free software; you can redistribute it and/or modify
  7. #   it under the terms of the GNU General Public License as published by
  8. #   the Free Software Foundation; either version 2 of the License, or
  9. #   (at your option) any later version.
  10. #
  11. #   This program is distributed in the hope that it will be useful,
  12. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #   GNU General Public License for more details.
  15. #
  16. #   You should have received a copy of the GNU General Public License
  17. #   along with this program; if not, write to the Free Software
  18. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. import os
  21.  
  22. import gimp
  23. from gimpfu import *
  24.  
  25. def pyslice(image, drawable, save_path, html_filename,
  26.             image_basename, image_extension, separate,
  27.             image_path, capitalize, cellspacing):
  28.  
  29.     vert, horz = get_guides(image)
  30.  
  31.     if len(vert) == 0 and len(horz) == 0:
  32.         return
  33.  
  34.     gimp.progress_init("Py-Slice")
  35.     progress_increment = 1 / ((len(horz) + 1) * (len(vert) + 1))
  36.     progress = 0.0
  37.  
  38.     def check_path(path):
  39.         path = os.path.abspath(path)
  40.  
  41.         if not os.path.exists(path):
  42.             os.mkdir(path)
  43.  
  44.         return path
  45.     
  46.     save_path = check_path(save_path)
  47.  
  48.     if not os.path.isdir(save_path):
  49.         save_path = os.path.dirname(save_path)
  50.  
  51.     if separate:
  52.         image_relative_path = image_path
  53.         image_path = check_path(os.path.join(save_path, image_path))
  54.     else:
  55.         image_relative_path = ''
  56.         image_path = save_path
  57.  
  58.     tw = TableWriter(os.path.join(save_path, html_filename),
  59.                      cellspacing=cellspacing, capitalize=capitalize)
  60.  
  61.     top = 0
  62.  
  63.     for i in range(0, len(horz) + 1):
  64.         if i == len(horz):
  65.             bottom = image.height
  66.         else:
  67.             bottom = image.get_guide_position(horz[i])
  68.  
  69.         tw.row_start()
  70.  
  71.         left = 0
  72.  
  73.         for j in range(0, len(vert) + 1):
  74.             if j == len(vert):
  75.                 right = image.width
  76.             else:
  77.                 right = image.get_guide_position(vert[j])
  78.  
  79.             src = slice(image, image_path, image_basename, image_extension,
  80.                         left, right, top, bottom, i, j)
  81.  
  82.             if image_relative_path:
  83.                 if image_relative_path.endswith('/'):
  84.                     src_path = image_relative_path + src
  85.                 else:
  86.                     src_path = image_relative_path + '/' + src
  87.             else:
  88.                 src_path = src
  89.  
  90.             tw.cell(src_path, right - left, bottom - top)
  91.  
  92.             left = right + cellspacing
  93.  
  94.             progress += progress_increment
  95.             gimp.progress_update(progress)
  96.  
  97.         tw.row_end()
  98.  
  99.         top = bottom + cellspacing
  100.  
  101.     tw.close()
  102.  
  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)
  106.     filename = os.path.join(image_path, src)
  107.  
  108.     temp_image = image.duplicate()
  109.     temp_image.disable_undo()
  110.  
  111.     temp_image.crop(right - left, bottom - top, left, top)
  112.  
  113.     pdb.gimp_file_save(temp_image, temp_image.active_layer, filename, filename)
  114.  
  115.     gimp.delete(temp_image)
  116.  
  117.     return src
  118.  
  119. class GuideIter:
  120.     def __init__(self, image):
  121.         self.image = image
  122.         self.guide = 0
  123.  
  124.     def __iter__(self):
  125.         return iter(self.next_guide, 0)
  126.  
  127.     def next_guide(self):
  128.         self.guide = self.image.find_next_guide(self.guide)
  129.         return self.guide
  130.  
  131. def get_guides(image):
  132.     vguides = []
  133.     hguides = []
  134.  
  135.     for guide in GuideIter(image):
  136.         orientation = image.get_guide_orientation(guide)
  137.  
  138.         guide_position = image.get_guide_position(guide)
  139.  
  140.         if guide_position > 0:
  141.             if orientation == ORIENTATION_VERTICAL:
  142.                 if guide_position < image.width:
  143.                     vguides.append((guide_position, guide))
  144.             elif orientation == ORIENTATION_HORIZONTAL:
  145.                 if guide_position < image.height:
  146.                     hguides.append((guide_position, guide))
  147.  
  148.     def position_sort(x, y):
  149.         return cmp(x[0], y[0])
  150.  
  151.     vguides.sort(position_sort)
  152.     hguides.sort(position_sort)
  153.  
  154.     vguides = [g[1] for g in vguides]
  155.     hguides = [g[1] for g in hguides]
  156.  
  157.     return vguides, hguides
  158.  
  159. class TableWriter:
  160.     def __init__(self, filename, cellpadding=0, cellspacing=0, border=0,
  161.                  capitalize=FALSE):
  162.         self.table_attrs = {}
  163.  
  164.         self.table_attrs['cellpadding'] = cellpadding
  165.         self.table_attrs['cellspacing'] = cellspacing
  166.         self.table_attrs['border'] = border
  167.  
  168.         self.capitalize = capitalize
  169.  
  170.         self.html = file(filename, 'w')
  171.  
  172.         self.open()
  173.  
  174.     def write(self, s, vals=None):
  175.         if self.capitalize:
  176.             s = s.upper()
  177.             s = s.replace('%S', '%s')
  178.         else:
  179.             s = s.lower()
  180.  
  181.         if vals:
  182.             s = s % vals
  183.  
  184.         self.html.write(s + '\n')
  185.  
  186.     def open(self):
  187.         out = '<table'
  188.  
  189.         for attr, value in self.table_attrs.iteritems():
  190.             out += ' %s=%s' % (attr, value)
  191.  
  192.         out += '>'
  193.  
  194.         self.write(out)
  195.  
  196.     def close(self):
  197.         self.write('</table>')
  198.         self.html.close()
  199.  
  200.     def row_start(self):
  201.         self.write('\t<tr>')
  202.  
  203.     def row_end(self):
  204.         self.write('\t</tr>')
  205.  
  206.     def cell(self, src, width, height):
  207.         out = ('\t\t<td><img alt=" " src="%%s" width="%d" height="%d"></td>' %
  208.                (width, height))
  209.         self.write(out, src)
  210.  
  211. register(
  212.     "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.",
  215.     "Manish Singh",
  216.     "Manish Singh",
  217.     "2003",
  218.     "<Image>/Filters/Web/Py-Slice...",
  219.     "*",
  220.     [
  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))
  229.     ],
  230.     [],
  231.     pyslice)
  232.  
  233. main()
  234.