Align an Image in its Frame

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 1.3.3.9 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.

See also this script (Image Wizard: Scale and Align an Image) which can scale and align images.

'''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!'''

New Version Fixed for 1.4.3/1.5.0
Aside from fixing it so that it works, I have also changed the import syntax to the preferred:

rather than

.

This script has been modified again. The original command, scaleImage is deprecated and no longer works, so now use setImageScale.

Something to note is that when you click the Align button, the work is done, but you don't see the results until you click Done – this is because the document isn't redrawn until then.

How It Works
When you start the script it first calls the Tkinter function, which sets up the dialog to choose your position for the image in the frame. When you click the Align button, the AlignImage function is called.

AlignImage is rather ingenious in how it determines how far to move the origin of the image in the frame. First, it gets the image scale settings (saveScaleX and saveScaleY). We can't really know the dimensions of this image in page units since there is no Scripter command for that. It then resizes image to frame (with keep proportions off), and rechecks the image scale again (fullScaleX and fullScaleY). We know the dimensions of this resized image, since it's the dimensions of the frame. Once it gets this information, the script then returns the image back to its original scaling.

At this point we can calculate the actual dimensions the image had originally (imageW and imageH) using the relative scales and the frame width/height values. Next, the value sent from the Tkinter function is parsed to determine how to calculate the new X and Y offsets, using frameW, frameH, imageW and imageH. Because there is a loop for all selected objects, you then move on to the next selected object and go through this same process.

It's also worth mentioning that this script also works when the scaled image is larger than the frame.

A modified version, farther down this wiki page (TkInter Version 2), has been included with Scribus 1.4.4svn.

TkInter Version 2
This version has been committed to 1.4.4svn. Compared with the previous version, this one has stripped out the Done button. When you click Align, the script carries out the operations and quits.

A late problem identified was that when more than one object was selected, the final scale of all objects ended up being the same. The problem seemed to be that when more than one object was selected, any usage of setImageScale is applied to all. Therefore, this was prevented by a deselectAll command after the selection list was obtained, then one-by-one selecting, then deselecting in the alignment for loop.

New Version NOT Using Tkinter
Here I have completely eliminated all aspects of Tkinter.

Instead, you get a valueDialog and enter a 2-letter combination, the first being T, M, or B, and the second L, C, or R. The script allows upper or lower case entries, but if you don't follow this order, or use other letters (for example, either CM or XY), this defaults to top left positioning.

Original Version
I'm not sure how well this version worked originally, but at any rate it no longer worked in 1.5.0. I'm leaving it here for reference purposes, as well as acknowledging the original work done.

Save this script with a filename of Image_AlignInFrame.py, for example.