home *** CD-ROM | disk | FTP | other *** search
- # these functions make it easier to write simple image editing scripts using
- # ImageMagick functions
-
- from mmmain import *
- from mmcommon import *
- from mmpersist import *
- from mmphotomgr import *
- from mmwindowing import *
- from mmimglib import *
- from mmimgmgr import *
- from mmMagick import *
- import getopt, sys
- import string
- import time
- import re
- import os
- import traceback
- from os.path import join, getsize
- import mm_import_utils
-
- # call this with a photoId and it will pass back to you an in memory
- # rendering of the image in ImageMagick format. In C++ it would be a Magick::Image type
- def LoadImageIntoMemory(photo_id) :
-
- ps = MMPersistentStore._GetInstance()
-
- photo = ps.LoadPhoto(photo_id)
-
- image_file = photo.GetActiveImageFile()
-
- URI = image_file.GetURI()
-
- filename = MMFileUtils.FileURLToSystemPath(URI)
-
- print "Reading Image:", filename
- mag_image = Image()
-
- mag_image.read(filename)
-
- last_slash = filename.rfind("\\")
-
- return mag_image
-
- # call this function to save the edited ImageMagick image to disk with
- # a uniquely generated filename based on the type of operation that was applied
- def SaveImageToDiskWithAutoNaming(photo_id, operation_type_string, mag_image, quality = 90) :
-
- # get this info again. Its easer than having the user pass it all back
- ps = MMPersistentStore._GetInstance()
- photo = ps.LoadPhoto(photo_id)
- image_file = photo.GetActiveImageFile()
- URI = image_file.GetURI()
- filename = MMFileUtils.FileURLToSystemPath(URI)
-
- # create a new filename based on the current name and the operation type applied
- new_filename = MMImgEditor.CreateNewFilename(filename, operation_type_string)
-
- # turn off watch dirs for the save
- # Get the directory from the path
- last_slash = URI.rfind('/');
- watch_dir_task = MMWatchDirTask.GetWatchDirTask()
- watch_directory = URI[0:last_slash]
- # Suspend watching on this directory for the duration of this edit
- watch_dir_task.IgnoreDirectory(watch_directory);
-
- import_into_db = 1
- try :
- print "Writing Image: ", new_filename[last_slash+1:]
- mag_image.quality(quality);
- mag_image.write(new_filename);
- except:
- print "Could not save file"
- MMDoPrompt.Message("Error: Could not create a new file named: " + new_filename[last_slash+1:])
- import_into_db = 0
-
- # restore watch directories
- watch_dir_task.EndIgnoringDirectory(watch_directory);
-
- if (import_into_db == 1):
- # add the new file to the database
- mm_import_utils.add_single_file_to_db(new_filename, [], 0, 1)
-
- last_slash = new_filename.rfind("\\")
- MMDoPrompt.Message("A new image has been created. The new image is named: " + new_filename[last_slash+1:])
-