#!/usr/bin/env python ''' mm extern webresize.py Use extern imagemagick binary to downscale the image. Author: Michael Munzert (mail mm-log com) Version: 2010.02.03 Added different filters. modelled after the trace plugin (lloyd konneker, lkk, bootch at nc.rr.com) 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.html ''' from gimpfu import * import subprocess import os def plugin_main(image, drawable, size, method, visible): # Get image file name name = image.filename # Get exif data exifdata = image.parasite_find("exif-data") # Copy so the save operations doesn't affect the original tempimage = pdb.gimp_image_duplicate(image) if not tempimage: raise RuntimeError # Use temp file names from gimp, it reflects the user's choices in gimp.rc tempfilename = pdb.gimp_temp_name("tif") if visible == 0: # Save in temporary. Note: empty user entered file name tempdrawable = pdb.gimp_image_get_active_drawable(tempimage) else: # Get the current visible tempdrawable = pdb.gimp_layer_new_from_visible(image, tempimage, "visible") # !!! Note no run-mode first parameter, and user entered filename is empty string pdb.gimp_progress_set_text ("Saving a copy") pdb.gimp_file_save(tempimage, tempdrawable, tempfilename, "") width = tempdrawable.width height = tempdrawable.height # Command string for mogrify. if method == 1: arg = "-filter Catrom " elif method == 2: arg = "-filter Mitchell " elif method == 3: arg = "-filter Cubic " else: arg = "-filter Lanczos " if height > width: arg = arg + "-resize x" + str(size) + " " else : arg = arg + "-resize " + str(size) + " " # Command line for linux command = "/usr/bin/mogrify " + arg + "\"" + tempfilename + "\"" # Command line for windows, alter path if needed # command = "c:\\programs\\ImageMagick\\mogrify.exe " + arg + "\"" + tempfilename + "\"" # Invoke mogrify. pdb.gimp_progress_set_text ("Resizing") pdb.gimp_progress_pulse() child = subprocess.Popen(command, shell=True) child.communicate() # Reload the transformed image. try: newimage = pdb.file_tiff_load(tempfilename, "") except: RuntimeError # Write exif data if exifdata != None: newimage.parasite_attach(exifdata) # Write name if name != None: newimage.filename = name ''' # just for testing, put it as a new layer in the opened image try: newlayer = pdb.gimp_file_load_layer(image, tempfilename) except: RuntimeError image.add_layer(newlayer,0) ''' # cleanup os.remove(tempfilename) # delete the temporary file gimp.delete(tempimage) # delete the temporary image # Note the new image is dirty in Gimp and the user will be asked to save before closing. gimp.Display(newimage) gimp.displays_flush() register( "python_fu_mm_extern_webresize", "Create a new resized image by using the Lanczos algorithm of ImageMagick. Requires separate ImageMagick (mogrify) program to be installed.", "Create a new resized image by using the Lanczos algorithm of ImageMagick. Requires separate ImageMagick (mogrify) program to be installed.", "Michael Munzert (mail mm-log com)", "Copyright 2010 Michael Munzert", "2010", "/Filters/MM-Filters/_Web Resize ...", # menuitem with accelerator key "*", # image types [ (PF_SPINNER, "size", "Longer edge:", 800, (400, 2000, 100)), (PF_OPTION, "method", "Filter:", 0, ["Lanczos","Catrom","Mitchell","Cubic"]), (PF_RADIO, "visible", "Layer:", 1, (("new from visible", 1),("current layer",0))) ], [], plugin_main, # menu="/Filters", # really the menupath less menuitem. # Enables plugin regardless of image open, passes less params # domain=("gimp20-python", gimp.locale_directory)) ) main()