Advertisement
Guest User

Untitled

a guest
Sep 9th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # openscad_surface_export.py
  4. # by Rob Antonishen
  5. # http://ffaat.pointclark.net
  6. #
  7. # Version 1.0 (20120907)
  8. #
  9. # Description
  10. # File save handler for openscad dat format
  11. # Borrows heavily from colorxhtml.py
  12. #
  13. # Changes
  14. #
  15. # License:
  16. #
  17. # This program is free software; you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation; either version 2 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. # GNU General Public License for more details.
  26. #
  27. # The GNU Public License is available at
  28. # http://www.gnu.org/copyleft/gpl.datfile
  29.  
  30. import struct
  31. import datetime
  32. import os
  33. import gimp
  34. from gimpfu import *
  35.  
  36. gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
  37.  
  38. def save_openscad_dat(img, drawable, path, minval=0.0, maxval=100.0):
  39.     filename = path + os.sep + img.name.split(".")[0] + ".dat"
  40.     drawable = pdb.gimp_layer_new_from_visible(img, img, "merged")
  41.     width = drawable.width
  42.     height = drawable.height
  43.     bpp = drawable.bpp
  44.     span = maxval-minval
  45.    
  46.     if span == 0:
  47.         return
  48.  
  49.     gimp.tile_cache_ntiles(width / gimp.tile_width() + 1)
  50.    
  51.     fileOut = file(filename,"w")
  52.    
  53.     pr = drawable.get_pixel_rgn(0, 0, width, height, False, False)
  54.    
  55.     gimp.progress_init(_("Saving as OpenSCAD .dat file"))
  56.    
  57.     fileOut.write("# Created by gimp plugin openscad_surface_export.py, %s\n" % datetime.datetime.strftime(datetime.datetime.now(), "%c"))
  58.     fileOut.write("# Name: %s\n" % img.name)
  59.     fileOut.write("# type: matrix\n")
  60.     fileOut.write("# Rows: %s\n" % height)
  61.     fileOut.write("# Columns: %s\n" % width)
  62.  
  63.     for y in range(height-1, -1, -1):
  64.         row = pr[0:width, y]
  65.         for pixel in RowIterator(row, bpp):
  66.             pixval = pixel[0]
  67.             fileOut.write(" %s" % str((minval+span*(pixval/255.0))))
  68.         fileOut.write("\n")
  69.         gimp.progress_update((height-y) / float(height))
  70.  
  71.     fileOut.close()
  72.     pdb.gimp_drawable_delete(drawable)
  73.    
  74. class RowIterator:
  75.     def __init__(self, row, bpp):
  76.         self.row = row
  77.         self.bpp = bpp
  78.  
  79.         self.start = 0
  80.         self.stop = bpp
  81.  
  82.         self.length = len(row)
  83.         self.fmt = 'B' * bpp
  84.  
  85.     def __iter__(self):
  86.         return iter(self.get_pixel, None)
  87.  
  88.     def get_pixel(self):
  89.         if self.stop > self.length:
  90.             return None
  91.  
  92.         pixel = struct.unpack(self.fmt, self.row[self.start:self.stop])
  93.  
  94.         self.start += self.bpp
  95.         self.stop += self.bpp
  96.  
  97.         return pixel
  98.        
  99. def register_save_dat():
  100.     gimp.register_save_handler("file-openscad-save", "dat", "")
  101.  
  102. register(
  103.         proc_name=("file-openscad-save"),
  104.         blurb=N_("Saves an OpenSCAD surface dat file."),
  105.         help=("Saves an OpenSCAD surface dat file."),
  106.         author=("Rob Antonishen"),
  107.         copyright=("Rob Antonishen"),
  108.         date=("2012"),
  109.         label=N_("Export OpenSCA_D Surface"),
  110.         imagetypes=("RGB, GRAY"),
  111.         params=[
  112.             (PF_IMAGE, "img", "Input image", None),
  113.             (PF_DRAWABLE, "drawable", "Input drawable", None),
  114.             (PF_DIRNAME, "path", "Export file here", None),
  115.             (PF_FLOAT, "minval", "Black (1) Value", 0.0),
  116.             (PF_FLOAT, "maxval", "White (255) Value", 100.0),
  117.         ],
  118.         results=[],
  119.         function=(save_openscad_dat),
  120.         menu=("<Image>/File"),
  121.         domain=("gimp20-python", gimp.locale_directory)
  122.         )
  123.  
  124. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement