home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 October - Disc 3 / PCNET_CD_2006_10_3.iso / apps / ShutterflyStudioInstaller_ext.exe / Scripts / user_edit_utils.py < prev   
Encoding:
Python Source  |  2006-04-23  |  2.6 KB  |  85 lines

  1. # these functions make it easier to write simple image editing scripts using
  2. # ImageMagick functions
  3.  
  4. from mmmain import *
  5. from mmcommon import *
  6. from mmpersist import *
  7. from mmphotomgr import *
  8. from mmwindowing import *
  9. from mmimglib import *
  10. from mmimgmgr import *
  11. from mmMagick import *
  12. import getopt, sys
  13. import string
  14. import time
  15. import re
  16. import os
  17. import traceback
  18. from os.path import join, getsize
  19. import mm_import_utils
  20.  
  21. # call this with a photoId and it will pass back to you an in memory 
  22. # rendering of the image in ImageMagick format.  In C++ it would be a Magick::Image type
  23. def LoadImageIntoMemory(photo_id) :
  24.  
  25.     ps = MMPersistentStore._GetInstance()
  26.  
  27.     photo = ps.LoadPhoto(photo_id)
  28.  
  29.     image_file = photo.GetActiveImageFile()
  30.     
  31.     URI = image_file.GetURI()
  32.     
  33.     filename = MMFileUtils.FileURLToSystemPath(URI)
  34.  
  35.     print "Reading Image:", filename
  36.     mag_image = Image()
  37.     
  38.     mag_image.read(filename)
  39.     
  40.     last_slash = filename.rfind("\\")
  41.     
  42.     return mag_image
  43.     
  44. # call this function to save the edited ImageMagick image to disk with
  45. # a uniquely generated filename based on the type of operation that was applied
  46. def SaveImageToDiskWithAutoNaming(photo_id, operation_type_string, mag_image, quality = 90) :
  47.  
  48.     # get this info again.  Its easer than having the user pass it all back
  49.     ps = MMPersistentStore._GetInstance()
  50.     photo = ps.LoadPhoto(photo_id)
  51.     image_file = photo.GetActiveImageFile()
  52.     URI = image_file.GetURI()
  53.     filename = MMFileUtils.FileURLToSystemPath(URI)
  54.  
  55.     # create a new filename based on the current name and the operation type applied
  56.     new_filename = MMImgEditor.CreateNewFilename(filename, operation_type_string)
  57.     
  58.     # turn off watch dirs for the save
  59.     # Get the directory from the path
  60.     last_slash = URI.rfind('/');
  61.     watch_dir_task = MMWatchDirTask.GetWatchDirTask()
  62.     watch_directory = URI[0:last_slash]
  63.     # Suspend watching on this directory for the duration of this edit
  64.     watch_dir_task.IgnoreDirectory(watch_directory);
  65.     
  66.     import_into_db = 1
  67.     try :
  68.         print "Writing Image: ", new_filename[last_slash+1:]
  69.         mag_image.quality(quality);
  70.         mag_image.write(new_filename);
  71.     except:
  72.         print "Could not save file"
  73.         MMDoPrompt.Message("Error: Could not create a new file named: " + new_filename[last_slash+1:])
  74.         import_into_db = 0
  75.     
  76.     # restore watch directories
  77.     watch_dir_task.EndIgnoringDirectory(watch_directory);
  78.  
  79.     if (import_into_db == 1):
  80.         # add the new file to the database
  81.         mm_import_utils.add_single_file_to_db(new_filename, [], 0, 1)
  82.     
  83.         last_slash = new_filename.rfind("\\")
  84.         MMDoPrompt.Message("A new image has been created.  The new image is named: " + new_filename[last_slash+1:])
  85.