Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # openscad_surface_export.py
- # by Rob Antonishen
- # http://ffaat.pointclark.net
- #
- # Version 1.0 (20120907)
- #
- # Description
- # File save handler for openscad dat format
- # Borrows heavily from colorxhtml.py
- #
- # Changes
- #
- # License:
- #
- # 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 2 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.
- #
- # The GNU Public License is available at
- # http://www.gnu.org/copyleft/gpl.datfile
- import struct
- import datetime
- import gimp
- from gimpfu import *
- gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
- def save_openscad_dat(img, drawable, filename, raw_filename):
- width = drawable.width
- height = drawable.height
- bpp = drawable.bpp
- gimp.tile_cache_ntiles(width / gimp.tile_width() + 1)
- fileOut = file(filename,"w")
- pr = drawable.get_pixel_rgn(0, 0, width, height, False, False)
- gimp.progress_init(_("Saving as OpenSCAD .dat file"))
- fileOut.write("# Created by gimp plugin openscad_surface_export.py, %s\n" % datetime.datetime.strftime(datetime.datetime.now(), "%c"))
- fileOut.write("# Name: %s\n" % img.name)
- fileOut.write("# type: matrix\n")
- fileOut.write("# Rows: %s\n" % height)
- fileOut.write("# Columns: %s\n" % width)
- for y in range(height-1, -1, -1):
- row = pr[0:width, y]
- for pixel in RowIterator(row, bpp):
- if drawable.is_gray:
- pixval = pixel[0]
- else:
- pixval = (pixel[0] + pixel[1] + pixel[2]) / 3
- fileOut.write(" %s" % str((pixval-128.0)/256.0))
- fileOut.write("\n")
- gimp.progress_update((height-y) / float(height))
- fileOut.close()
- class RowIterator:
- def __init__(self, row, bpp):
- self.row = row
- self.bpp = bpp
- self.start = 0
- self.stop = bpp
- self.length = len(row)
- self.fmt = 'B' * bpp
- def __iter__(self):
- return iter(self.get_pixel, None)
- def get_pixel(self):
- if self.stop > self.length:
- return None
- pixel = struct.unpack(self.fmt, self.row[self.start:self.stop])
- self.start += self.bpp
- self.stop += self.bpp
- return pixel
- def register_save_dat():
- gimp.register_save_handler("file-openscad-save", "dat", "")
- register(
- "file-openscad-save",
- N_("Saves an OpenSCAD surface dat file."),
- "Saves an OpenSCAD surface dat file.",
- "Rob Antonishen",
- "Rob Antonishen",
- "2012",
- N_("OpenSCAD Surface"),
- "RGB, GRAY",
- [
- (PF_IMAGE, "image", "Input image", None),
- (PF_DRAWABLE, "drawable", "Input drawable", None),
- (PF_STRING, "filename", "The name of the file", None),
- (PF_STRING, "raw-filename", "The name of the file", None),
- ],
- [],
- save_openscad_dat, on_query=register_save_dat,
- menu="<Save>", domain=("gimp20-python", gimp.locale_directory))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement