Advertisement
kthomer

GIMP Script: save_as_png.py

Jan 30th, 2013
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # save_as_png.py
  4. # Provides a simple menu option to save as PNG with
  5. # basic save options and overwrite warning for newly created files.
  6. # Tested in GIMP 2.8.2 on Windows 7 (64 and 32-bit)
  7. # Contact: Kevin Thomer (Defron) | http://blog.defron.org/
  8. # Provided free and as-is under GPL v2.
  9. #
  10. # Based off of:
  11.  
  12. # save_as_jpg.py
  13. # version 1.0 [gimphelp.org]
  14. # last modified/tested by Paul Sherman
  15. # 12/20/2012 on GIMP-2.8
  16. #
  17. # ==== Original Information ====================================================
  18. # Save or export the current image -- do the right thing whether it's
  19. # XCF (save) or any other format (export). This will mark the image clean,
  20. # so GIMP won't warn you when you exit.
  21. # Warning: this does not show a lot of extra dialogs, etc. or warn you
  22. # if you're about to overwrite something! Use with caution.
  23.  
  24. # Copyright 2012 by Akkana Peck, http://www.shallowsky.com/software/
  25. # You may use and distribute this plug-in under the terms of the GPL v2
  26. # or, at your option, any later GPL version.
  27. # ========================================================
  28.  
  29. from gimpfu import *
  30. import gtk
  31. import os, sys
  32. import collections
  33.  
  34. def python_export_clean(img, drawable, interlace, background, compression) :
  35.     filename = img.filename
  36.     #These typecasts isn't really necessary in Python, just a habit of mine
  37.     bg = int(background)
  38.     interlacing = int(interlace)
  39. #   fullpath = pdb.gimp_image_get_uri(img)
  40. #   pdb.gimp_message(filename)
  41.  
  42.     if not filename :
  43.         chooser = gtk.FileChooserDialog(
  44.             title=None,action=gtk.FILE_CHOOSER_ACTION_SAVE,
  45.             buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK)
  46.             )
  47.         # save folder will be desktop
  48.         save_dir = os.path.join(os.path.expanduser('~'), 'Desktop')
  49.            
  50.         chooser.set_current_folder(save_dir)
  51.         chooser.set_current_name("UNTITLED.png")
  52.         chooser.set_do_overwrite_confirmation(True)
  53.        
  54.         filter = gtk.FileFilter()
  55.         filter.set_name("Save as png")
  56.         filter.add_pattern("*.png")
  57.         chooser.add_filter(filter)
  58.        
  59.         response = chooser.run()
  60.         if response != gtk.RESPONSE_OK:
  61.             return
  62.         filename = chooser.get_filename()
  63.         img.filename = filename
  64.         chooser.destroy()
  65.    
  66.         pdb.file_png_save(img, drawable, filename, filename, interlacing, compression, bg, 0, 0, 1, 1)
  67.         pdb.gimp_image_clean_all(img)      
  68.            
  69.        
  70.     else:
  71.         base = os.path.splitext(filename)[0]
  72.         newname = base + ".png"
  73.  
  74.         pdb.gimp_edit_copy(img.active_drawable)
  75.         image2 = pdb.gimp_edit_paste_as_new()
  76.         pdb.file_png_save(image2, drawable, newname, newname, interlacing, compression, bg, 0, 0, 1, 1)
  77.         pdb.gimp_image_delete(image2)      
  78.         pdb.gimp_image_clean_all(img)
  79.  
  80.  
  81. register(
  82.         "python_fu_save_as_png",
  83.         "Save the image as a PNG file, set interlacing & saving bg color\n\nFor more options and a proper file overwrite protected dialog, \nuse the FILE > EXPORT menu item when saving as a PNG.\n\n",
  84.         "",
  85.         "Kevin Thomer (Defron)",
  86.         "GPL",
  87.         "2013",
  88.         "Save as PNG",
  89.         "*",
  90.         [
  91.             (PF_IMAGE, "image", "Input image", None),
  92.             (PF_DRAWABLE, "drawable", "Input drawable", None),
  93.             (PF_TOGGLE, "interlace", "Interlacing (Adam7)", 0),
  94.             (PF_TOGGLE, "background", "Save background color", 1),
  95.             (PF_SLIDER, "Compression", "Set the PNG Compression Level", 9, (0, 9, 1) )
  96.         ],
  97.         [],
  98.         python_export_clean,
  99.         menu = "<Image>/File/Save/"
  100. )
  101.  
  102. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement