Image crop, resize and CMYK conversion. Save and reload in TIFF format

This scripter script crop and resize an image, save it and reload it in TIFF format. Optionaly, convert color space from RGB to CMYK. Need Scribus 1.3.8 and use Python Image Library. Caveat: PIL currently freeze Scribus in Ubuntu 10.04, if run as script. Run OK in scripter console and in Windows XP.


 * 1) !/usr/bin/python
 * 2) -*- coding: utf-8 -*-

""" Crop and resample selected photo in Scribus By prof. MS. José Antonio Meira da Rocha mailto:joseantoniorocha@gmail.com http://meiradarocha.jor.br License GPL 2010-08-04a PIL docs: http://infohost.nmt.edu/tcc/help/pubs/pil/image-constructors.html http://www.pythonware.com/library/pil/handbook/index.htm """

import sys if sys.platform != 'win32': sys.path.append("/usr/lib/python2.6") # prevent 'import re' PIL bug in Scribus 1.3.8, Ubuntu 10.04

try: import scribus except ImportError,err: print "This Python script is written for the Scribus scripting interface." print "It can only be run from within Scribus." sys.exit(1)

from scribus import UNIT_POINTS, UNIT_MILLIMETERS, UNIT_INCHES, LINE_SOLID, JOIN_MITTER, BUTTON_OK, BUTTON_NO, BUTTON_YES, ICON_WARNING, ICON_INFORMATION, pt, mm

import os from string import Template
 * 1) USER IMPORTS GOES HERE
 * 1) USER IMPORTS GOES HERE

encoding = sys.getfilesystemencoding
 * 1) OS string encoding for PIL handle filenames
 * 2) (too much time to discover this)
 * 1) (too much time to discover this)

imageResolution = 200 # resolution for printing fileType = '.tif' # force tiff format
 * 1) User can change this variables
 * 1) User can change this variables

UNDER_FRAME = 0 AROUND_FRAME = 1 BOUNDING_BOX = 2 CONTOUR_LINE = 3 SCALETOFRAME = 1 FREESCALE = 0 PROPORTIONAL = 1 DISTORTED = 0
 * 1) Scribus Constants
 * 1) Scribus Constants

askResolutionLabel="Abra um documento" askResolution = " Qual a resolução " \ + " " openImageErrorLabel = 'Erro de abertura' openImageErrorMsg = ' Erro de abertura ' \ + ' Houve um erro ao tentar abrir o arquivo de foto. Talvez o arquivo tenha sido perdido. ' openDocLabel="Abra um documento" askOpenDoc = " Abra um documento " \ + " Por favor, abra um documento e selecione um quadro antes de usar este script. " imageFrameLabel = "Selecione um quadro de imagem" askSelectImageFrame = " Selecione um quadro de imagem \n" \ + "Selecione um quadro de imagem\n" \ + "para cortar a foto dentro dele." outOfRangeLabel = 'Foto fora de faixa' msgOutOfRange = ' Foto fora de faixa A foto não está ocupando todo o quadro.' errorOnSaveLabel = 'Erro ao gravar' errorOnSaveMsg = ' Erro ao gravar Houve um erro ao gravar o arquivo de imagem.' cropErrorLabel = 'Erro ao cortar a imagem' cropErrorMsg = ' Erro ao cortar a imagem Houve um erro ao cortar a imagem. Talvez este tipo de arquivo não seja suportado.' errorOnLoadLabel = 'Erro ao importar a imagem' errorOnLoadMsg = ' Erro ao importar a imagem Houve um erro ao importar a imagem.'
 * 1) Locale strings
 * 2) Textos para abrir documento
 * 1) Textos para abrir documento
 * 1) Textos para abrir documento
 * 1) Textos para abrir quadro
 * 1) Error on save image
 * 1) Error on cropping
 * 1) Error on loading image

stringsDic = {'en_US':'Group', 'en_GB':'Group', 'en_AU':'Group', 'pt_BR':'Agrup','pt': 'Agrup'} lang = scribus.getGuiLanguage try: groupString = stringsDic[lang] except: scribus.messageBox('Missing group string','Put group string in your language in "stringsDic".',ICON_WARNING,BUTTON_OK) sys.exit(1)
 * 1) Localized group name
 * 2) Put your language string for group names in dictionary
 * 3) For use by isGroup function
 * 1) For use by isGroup function

cropsufix = '_cortado' # '_cropped' statusText = "Running script..."
 * 1) cropped sufix
 * 1) foot msg


 * 1) End Locale strings
 * 1) End Locale strings

try: import Image except ImportError,err: print "This Python script is written for the PIL graphic interface." print "It should be instaled in Scribus Python tree." sys.exit(1)
 * 1) PIL library
 * 2) 'import Image' cause Scribus freeze after script run, in Ubuntu 10.04.
 * 3)  OK in Windows XP, Python 2.5.4, PIL 1.1.7
 * 1) 'import Image' cause Scribus freeze after script run, in Ubuntu 10.04.
 * 2)  OK in Windows XP, Python 2.5.4, PIL 1.1.7


 * 1) Test object type
 * 1) Test object type

def isGroup(obj): """Verify if object is group.""" type = scribus.getObjectType(obj) if (type == 'Polygon') and (scribus.selectionCount > 1) and (obj.find(groupString) != -1): return 1 else: return 0

def isTextFrame(obj): """Verify if object is text frame.""" type = scribus.getObjectType(obj) if (type == 'TextFrame'): return 1 else: return 0

def isGraphicFrame(obj): """Verify if object is image frame.""" type = scribus.getObjectType(obj) if (type == 'ImageFrame'): return 1 else: return 0

def openImage(imageFrame): imf = scribus.getImageFile(imageFrame) try: image = Image.open(imf.encode(encoding)) # encode to let PIL handle accented filenames except: scribus.messageBox(openImageErrorLabel,openImageErrorMsg,ICON_WARNING,BUTTON_OK) image,imageFile = "","" sys.exit(1) return image,imf
 * 1) Open image
 * 1) Open image

def cropImage(image,imageFrame): imageXOffset = scribus.getProperty(imageFrame,'imageXOffset') imageYOffset = scribus.getProperty(imageFrame,'imageYOffset') # spawn error if offset is positive if (imageXOffset > 0) or (imageYOffset > 0): scribus.messageBox(outOfRangeLabel,msgOutOfRange,ICON_WARNING,BUTTON_OK) sys.exit(1) # Calculate image resample to press resolution (DPI) unit = scribus.getUnit scribus.setUnit(UNIT_INCHES) frameSizeX,frameSizeY = scribus.getSize(imageFrame) newWidth = frameSizeX * imageResolution newHeight = frameSizeY * imageResolution scribus.setUnit(unit) # Calculate cropping frameSizeX,frameSizeY = scribus.getSize(imageFrame) imageXScale = scribus.getProperty(imageFrame,'imageXScale') imageYScale = scribus.getProperty(imageFrame,'imageYScale') imageSizeX, imageSizeY = image.size left = imageXOffset * -1 # top = imageYOffset * -1 # right = left + (frameSizeX / imageXScale) bottom = top + (frameSizeY / imageYScale) try: im = image.crop((left,top,right,bottom)) im = im.resize((int(newWidth),int(newHeight)),Image.ANTIALIAS)# BICUBIC,NEAREST, BILINEAR, ANTIALIAS except: scribus.messageBox(cropErrorLabel,cropErrorMsg,ICON_WARNING,BUTTON_OK) sys.exit(1) ############################################################   # Uncomment this 2 lines if you wish color space conversion. ############################################################   #if im.mode == 'RGB': #   im = im.convert('CMYK') return im
 * 1) Crop Image
 * 1) Crop Image

def fileExist(fileName): try: f = open(fileName,'r') return 1 except: return 0 def saveImage(newImage,imageFile): name,ext = os.path.splitext(imageFile) # increment file version to avoid overwriting old files f = Template(name+cropsufix+'($version)'+fileType) count = 1 newImageFile = f.substitute(version=str(count)) while fileExist(newImageFile): count += 1 newImageFile = f.substitute(version=str(count)) try: #'encode' let PIL handle accented filenames newImage.save(newImageFile.encode(encoding),dpi=(imageResolution,imageResolution)) except: scribus.messageBox(errorOnSaveLabel,errorOnSaveMsg,ICON_WARNING,BUTTON_OK) sys.exit(1) return newImageFile
 * 1) Test if file exist
 * 1) Test if file exist
 * 1) Save Image
 * 1) Save Image

def reloadImage(newImageFile,imageFrame): try: scribus.loadImage(newImageFile,imageFrame) except: scribus.messageBox(errorOnLoadLabel,errorOnLoadMsg,ICON_WARNING,BUTTON_OK) sys.exit(1) scribus.setScaleImageToFrame(SCALETOFRAME,PROPORTIONAL,imageFrame)
 * 1) Reload image to frame
 * 1) Reload image to frame

def handleImage(imageFrame): image,imageFile = openImage(imageFrame) newImage = cropImage(image,imageFrame) newImageFile = saveImage(newImage,imageFile) reloadImage(newImageFile,imageFrame)
 * 1) Handle Image
 * 1) Handle Image

def handleSelection: """Handle selected objects.""" if (scribus.selectionCount == 1) and isGraphicFrame(scribus.getSelectedObject): imageFrame = scribus.getSelectedObject handleImage(imageFrame) else: scribus.messageBox(imageFrameLabel,askSelectImageFrame,ICON_WARNING,BUTTON_OK)
 * 1) Handle selection
 * 1) Handle selection

def handleDocument: """Handle documents """ # If there are open document if scribus.haveDoc: # turn off redraw scribus.setRedraw(False) # Save unit unit = scribus.getUnit # Define unit in points scribus.setUnit(UNIT_POINTS) # pre-press mesure system print ###############################       # Handle selection ###############################       handleSelection # Recover units scribus.setUnit(unit) else: # If there not open document. scribus.messageBox(openDocLabel,askOpenDoc,ICON_WARNING,BUTTON_OK)
 * 1) Handle document
 * 1) Handle document

def myCode: """ User code """ #########################   #  USER CODE GOES HERE  # #########################   # Gerencia documento (aberto, abre etc) handleDocument #########################   #  USER CODE ENDS HERE  # #########################

def main(argv): """Main entry point""" myCode

def main_wrapper(argv): try: scribus.statusMessage(statusText) scribus.progressReset main(argv) finally: if scribus.haveDoc: scribus.setRedraw(True) scribus.statusMessage("") scribus.progressReset

if __name__ == '__main__': main_wrapper(sys.argv)