Advertisement
Guest User

shellout.py

a guest
Jul 30th, 2012
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. ShellOut.py
  5. call an external program passing the active layer as a temp file. Windows Only(?)
  6.  
  7. Author:
  8. Rob Antonishen
  9.  
  10. Version:
  11. 0.7 fixed file save bug where all files were png regardless of extension
  12. 0.6 modified to allow for a returned layer that is a different size
  13. than the saved layer for
  14. 0.5 file extension parameter in program list.
  15. 0.4 modified to support many optional programs.
  16.  
  17. this script is modelled after the mm extern LabCurves trace plugin
  18. by Michael Munzert http://www.mm-log.com/lab-curves-gimp
  19.  
  20. and thanks to the folds at gimp-chat has grown a bit ;)
  21.  
  22. License:
  23.  
  24. This program is free software; you can redistribute it and/or modify
  25. it under the terms of the GNU General Public License as published by
  26. the Free Software Foundation; version 3 of the License.
  27.  
  28. This program is distributed in the hope that it will be useful,
  29. but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. GNU General Public License for more details.
  32.  
  33. The GNU Public License is available at
  34. http://www.gnu.org/copyleft/gpl.html
  35.  
  36. '''
  37.  
  38. from gimpfu import *
  39. import shlex
  40. import subprocess
  41. import os, sys
  42. import tempfile
  43.  
  44. #program list function (globals are evil)
  45. def listcommands(option=None):
  46. #
  47. # Insert additonal shell command into this list. They will show up in the drop menu in this order.
  48. # Use the syntax:
  49. # ["Menu Label", "command", "ext"]
  50. #
  51. # Where what gets executed is command fileame so include and flags needed in the command.
  52. programlist = [
  53. ["XNView", "\"C:\\Program Files (x86)\\XnView\\xnview.exe\""],
  54. ["MS Paint", "\"..\\..\\..\\..\\WINDOWS\\system32\\mspaint.exe\"", "bmp"],
  55. ["InPaint", "\"C:\\PF\\Inpaint\\Inpaint.exe\"", "png"],
  56. #["Deep Paint", "\"C:\\Program Files\\DeepPaint\\deeppaint.exe\"", "jpg"],
  57. #["Inkscape", "\"C:\\Program Files\\Inkscape\\inkscape.exe\"", "png"],
  58. #["PaintDOTNet", "\"C:\\Program Files\\Paint.NET\\PaintDotNet.exe\"", "png"],
  59. #["MyPaint", "\"C:\\Program Files\\MyPaint\\mypaint.exe\"", "png"],
  60. #["Photo Filter Factory", "\"C:\\Program Files\\Photo Filter Factory\\Photo Filter Factory.exe\"", "png"],
  61. #["Photo Pos Pro", "\"C:\\Program Files\\Photo Pos Pro\\Photo Pos Pro.exe\"", "png"],
  62. ["Java Image Editor", "\"C:\\JavaJars\\imageeditor.bat\"", "png"],
  63. ["Java Mosaic", "\"C:\\JavaJars\\mosaic.bat\"", "png"],
  64. ["JDraw", "\"C:\\JavaJars\\jdraw.bat\"", "png"],
  65. #["Vector Magic", "\"C:\\Program Files\\Vector Magic\\vmde.exe\"", "png"],
  66. #["Photo Clinic", "\"C:\\MAGIX\\Photo_Clinic_45\\PhotoClinic.exe\"", "png"],
  67. ["Smilla Enlarger", "\"C:\\utils\\SmillaEnlarger\\SmillaEnlarger.exe\"", "png"],
  68. ["","",""]
  69. ]
  70.  
  71. if option == None: # no parameter return menu list, otherwise return the appropaiate array
  72. menulist = []
  73. for i in programlist:
  74. if i[0] != "":
  75. menulist.append(i[0])
  76. return menulist
  77. else:
  78. return programlist[option]
  79.  
  80.  
  81. def plugin_main(image, drawable, visible, command):
  82. pdb.gimp_image_undo_group_start(image)
  83.  
  84. # Copy so the save operations doesn't affect the original
  85. if visible == 0:
  86. # Save in temporary. Note: empty user entered file name
  87. temp = pdb.gimp_image_get_active_drawable(image)
  88. else:
  89. # Get the current visible
  90. temp = pdb.gimp_layer_new_from_visible(image, image, "Visible")
  91. image.add_layer(temp, 0)
  92.  
  93. buffer = pdb.gimp_edit_named_copy(temp, "ShellOutTemp")
  94.  
  95. #save selection if one exists
  96. hassel = pdb.gimp_selection_is_empty(image) == 0
  97. if hassel:
  98. savedsel = pdb.gimp_selection_save(image)
  99.  
  100. tempimage = pdb.gimp_edit_named_paste_as_new(buffer)
  101. pdb.gimp_buffer_delete(buffer)
  102. if not tempimage:
  103. raise RuntimeError
  104. pdb.gimp_image_undo_disable(tempimage)
  105.  
  106. tempdrawable = pdb.gimp_image_get_active_layer(tempimage)
  107.  
  108. #get the program to run and filetype.
  109. progtorun = listcommands(command)
  110.  
  111. # Use temp file names from gimp, it reflects the user's choices in gimp.rc
  112. # change as indicated if you always want to use the same temp file name
  113. # tempfilename = pdb.gimp_temp_name(progtorun[2])
  114. tempfilename = os.path.join(tempfile.gettempdir(), "ShellOutTempFile."+progtorun[2])
  115.  
  116.  
  117. # !!! Note no run-mode first parameter, and user entered filename is empty string
  118. pdb.gimp_progress_set_text ("Saving a copy")
  119. pdb.gimp_file_save(tempimage, tempdrawable, tempfilename, tempfilename)
  120.  
  121. # Build command line call
  122. command = progtorun[1] + " \"" + tempfilename + "\""
  123. args = shlex.split(command)
  124.  
  125. # Invoke external command
  126. pdb.gimp_progress_set_text ("calling " + progtorun[0] + "...")
  127. pdb.gimp_progress_pulse()
  128. child = subprocess.Popen(args, shell=False)
  129. child.communicate()
  130.  
  131. # put it as a new layer in the opened image
  132. try:
  133. newlayer2 = pdb.gimp_file_load_layer(tempimage, tempfilename)
  134. except:
  135. RuntimeError
  136.  
  137. tempimage.add_layer(newlayer2,-1)
  138. buffer = pdb.gimp_edit_named_copy(newlayer2, "ShellOutTemp")
  139.  
  140. if visible == 0:
  141. drawable.resize(newlayer2.width,newlayer2.height,0,0)
  142. sel = pdb.gimp_edit_named_paste(drawable, buffer, 1)
  143. drawable.translate((tempdrawable.width-newlayer2.width)/2,(tempdrawable.height-newlayer2.height)/2)
  144. else:
  145. temp.resize(newlayer2.width,newlayer2.height,0,0)
  146. sel = pdb.gimp_edit_named_paste(temp, buffer, 1)
  147. temp.translate((tempdrawable.width-newlayer2.width)/2,(tempdrawable.height-newlayer2.height)/2)
  148.  
  149. pdb.gimp_buffer_delete(buffer)
  150. pdb.gimp_edit_clear(temp)
  151. pdb.gimp_floating_sel_anchor(sel)
  152.  
  153. #load up old selection
  154. if hassel:
  155. pdb.gimp_selection_load(savedsel)
  156. image.remove_channel(savedsel)
  157.  
  158. # cleanup
  159. os.remove(tempfilename) # delete the temporary file
  160. gimp.delete(tempimage) # delete the temporary image
  161.  
  162. # Note the new image is dirty in Gimp and the user will be asked to save before closing.
  163. pdb.gimp_image_undo_group_end(image)
  164. gimp.displays_flush()
  165.  
  166.  
  167. register(
  168. "python_fu_shellout",
  169. "Call an external program",
  170. "Call an external program",
  171. "Rob Antonishen",
  172. "Copyright 2011 Rob Antonishen",
  173. "2011",
  174. "<Image>/Filters/ShellOut...",
  175. "RGB*, GRAY*",
  176. [ (PF_RADIO, "visible", "Layer:", 1, (("new from visible", 1),("current layer",0))),
  177. (PF_OPTION,"command",("Program:"),0,listcommands())
  178. ],
  179. [],
  180. plugin_main,
  181. )
  182.  
  183. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement