Align an Image in its Frame

From Scribus Wiki
Revision as of 07:52, 17 September 2007 by TrnsltLife (Talk | contribs)

Jump to: navigation, search
This article is part of the Scripts series.

Under Scribus' "Windows" menu, there is a very nice option called "Align and Distribute". This lets you align items on the page, for example centering them horizontally or vertically, aligning them to the left margin, etc. However, I have not found any way to align an image in its frame.

This script will give you a dialog window, allowing you to select on of 9 alignments: Top Left, Top Center, Top Right Middle Left, Middle Center, Middle Right Bottom Left, Bottom Center, Bottom Right

When you press the "Align" button, it will align all the selected images in their frames, using the alignment option you selected. It has been tested in Scribus and 1.3.4. It requires Tkinter to be properly installed.

Of course, if you need more fine-tuned adjustments than these 9 options, you should probably use the Image Properties toolbar to manually set the X and Y offsets of the image in its frame.

Warning: You can leave the script dialog open, and continue to process more images. However, due to a multi-threading bug with Python scripts in Scribus, if you attempt to launch another script while this one is running, Scribus will probably crash and you'll lose your document. So be careful, and save often!

Save this script with a filename of, for example.

from scribus import *

    from Tkinter import *
    from tkFont import Font
except ImportError:
    print "This script requires Python's Tkinter properly installed."
    messageBox('Script failed',
               'This script requires Python\'s Tkinter properly installed.',

class TkImageAlignmentWizard(Frame):
    """ GUI interface for aligning an image in a frame"""

    def __init__(self, master=None):
        """ Setup the dialog """
        # refernce to the localization dictionary
        self.key = 'English'
        Frame.__init__(self, master)
        self.master.resizable(0, 0)
        self.master.title('Scribus Image Alignment Wizard')
        #define widgets
        # alignment options
        self.alignLabel = Label(self, text='Select alignment:')
        self.alignVar = StringVar()
        self.alignRadio1 = Radiobutton(self, text='', variable=self.alignVar, value="TL")
        self.alignRadio2 = Radiobutton(self, text='', variable=self.alignVar, value="TC")
        self.alignRadio3 = Radiobutton(self, text='', variable=self.alignVar, value="TR")
        self.alignRadio4 = Radiobutton(self, text='', variable=self.alignVar, value="ML")
        self.alignRadio5 = Radiobutton(self, text='', variable=self.alignVar, value="MC")
        self.alignRadio6 = Radiobutton(self, text='', variable=self.alignVar, value="MR")
        self.alignRadio7 = Radiobutton(self, text='', variable=self.alignVar, value="BL")
        self.alignRadio8 = Radiobutton(self, text='', variable=self.alignVar, value="BC")
        self.alignRadio9 = Radiobutton(self, text='', variable=self.alignVar, value="BR")
        self.alignButton = Button(self, text='Align', command=self.alignImage)
        # closing/running
        self.doneButton = Button(self, text="Done", command=self.quit)
        # setup values
        # make layout
        self.columnconfigure(0, pad=0)
        currRow = 0
        self.alignLabel.grid(column=0, row=currRow, columnspan=3)
        currRow += 1
        self.alignRadio1.grid(column=0, row=currRow)
        self.alignRadio2.grid(column=1, row=currRow)
        self.alignRadio3.grid(column=2, row=currRow)
        currRow += 1
        self.alignRadio4.grid(column=0, row=currRow)
        self.alignRadio5.grid(column=1, row=currRow)
        self.alignRadio6.grid(column=2, row=currRow)
        currRow += 1
        self.alignRadio7.grid(column=0, row=currRow)
        self.alignRadio8.grid(column=1, row=currRow)
        self.alignRadio9.grid(column=2, row=currRow)
        currRow += 1
        self.alignButton.grid(column=0, row=currRow, columnspan=3)
        self.doneButton.grid(column=3, row=currRow, columnspan=3)

    def alignImage(self):
        if haveDoc():
            nbrSelected = selectionCount()
            objList = []
            for i in range(nbrSelected):
            for i in range(nbrSelected):
                    obj = objList[i]
                    frameW = getProperty(obj, "width")
                    frameH = getProperty(obj, "height")
                    saveScaleX = getProperty(obj, "imageXScale")
                    saveScaleY = getProperty(obj, "imageYScale")
                    setScaleImageToFrame(True, False, obj)
                    fullScaleX = getProperty(obj, "imageXScale")
                    fullScaleY = getProperty(obj, "imageYScale")
                    setScaleImageToFrame(False, False, obj)
                    scaleImage(saveScaleX, saveScaleY, obj)
                    imageW = frameW * (saveScaleX / fullScaleX)
                    imageH = frameH * (saveScaleY / fullScaleY)
                    imageX = 0.0
                    imageY = 0.0
                    if self.alignVar.get()[0] == "T":
                        imageY = 0.0
                    elif self.alignVar.get()[0] == "M":
                        imageY = (frameH - imageH) / 2.0 / saveScaleY
                    elif self.alignVar.get()[0] == "B":
                        imageY = (frameH - imageH) / saveScaleY
                    if self.alignVar.get()[1] == "L":
                        imageX = 0.0
                    elif self.alignVar.get()[1] == "C":
                        imageX = (frameW - imageW) / 2.0 / saveScaleX
                    elif self.alignVar.get()[1] == "R":
                        imageX = (frameW - imageW) / saveScaleX

                    setProperty(obj, "imageXOffset", imageX)
                    setProperty(obj, "imageYOffset", imageY)
                    nothing = "nothing"

    def quit(self):

def main():
    """ Application/Dialog loop with Scribus sauce around """
        root = Tk()
        app = TkImageAlignmentWizard(root)
        if haveDoc():

if __name__ == '__main__':