diff options
author | Martin Poirier <theeth@yahoo.com> | 2011-03-22 04:42:06 +0300 |
---|---|---|
committer | Martin Poirier <theeth@yahoo.com> | 2011-03-22 04:42:06 +0300 |
commit | 2bb986132e429b57104dc9cf4949d6e38f8fc053 (patch) | |
tree | ee170f026a7fb47d303f78ed4fafb22ddd9ebc69 /netrender/thumbnail.py | |
parent | 82cc62d31778ebd71d666ce1a6460c211fa5c196 (diff) |
Moving netrender to addons
Diffstat (limited to 'netrender/thumbnail.py')
-rw-r--r-- | netrender/thumbnail.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/netrender/thumbnail.py b/netrender/thumbnail.py new file mode 100644 index 00000000..2ead6e82 --- /dev/null +++ b/netrender/thumbnail.py @@ -0,0 +1,81 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +import sys, os +import subprocess + +import bpy + +def generate(filename, external=True): + if external: + process = subprocess.Popen([sys.argv[0], "-b", "-noaudio", "-P", __file__, "--", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + while process.poll() is None: + process.stdout.read(1024) # empty buffer to be sure + process.stdout.read() + + return _thumbname(filename) + else: + return _internal(filename) + +def _thumbname(filename): + root = os.path.splitext(filename)[0] + return root + ".jpg" + +def _internal(filename): + imagename = os.path.split(filename)[1] + thumbname = _thumbname(filename) + + if os.path.exists(thumbname): + return thumbname + + if bpy: + scene = bpy.data.scenes[0] # FIXME, this is dodgy! + scene.render.file_format = "JPEG" + scene.render.file_quality = 90 + + # remove existing image, if there's a leftover (otherwise open changes the name) + if imagename in bpy.data.images: + img = bpy.data.images[imagename] + bpy.data.images.remove(img) + + bpy.ops.image.open(filepath=filename) + img = bpy.data.images[imagename] + + img.save_render(thumbname, scene=scene) + + img.user_clear() + bpy.data.images.remove(img) + + try: + process = subprocess.Popen(["convert", thumbname, "-resize", "300x300", thumbname]) + process.wait() + return thumbname + except Exception as exp: + print("Error while generating thumbnail") + print(exp) + + return None + +if __name__ == "__main__": + import bpy + try: + start = sys.argv.index("--") + 1 + except ValueError: + start = 0 + for filename in sys.argv[start:]: + generate(filename, external=False) |