FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Python Scripting with Scribus

swapimage.py

Here's a fun little script, that I can't say anyone including me has ever used in a purposeful way. The idea is that maybe you want to play with some layout containing images, and just want to see the difference in impact by interchanging a couple of images.

#!/usr/bin/env python
# -*- coding: utf-8  -*-

"""
swapimage.py

USAGE

Select 2 image frames, no more, no less, both must be image frames. Can be on different
pages.

Run the script, the images are swapped.

"""

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)


if scribus.selectionCount() != 2:
    scribus.messageBox('Selection Count', "You must have 2 image frames selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

frame1 = scribus.getSelectedObject(0)
frame2 = scribus.getSelectedObject(1)

ftype1 = scribus.getObjectType(frame1)
ftype2 = scribus.getObjectType(frame2)
if ((ftype1 != "ImageFrame") or (ftype2 != "ImageFrame")):
    scribus.messageBox('Object Type', "Both selected objects must be image frames",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

scribus.setRedraw(False)

filename1 = scribus.getImageFile(frame1)
filename2 = scribus.getImageFile(frame2)

scribus.loadImage(filename2, frame1)
scribus.loadImage(filename1, frame2)

scribus.setRedraw(True)

The usage information suggests right off the bat that most of what is here is error-checking. Aside from checking about running this in Scribus, we make sure that two and only two objects are selected, AND that they're both image frames. Notice here I have a different style of checking frame type. The command getObjectType() will return the string 'ImageFrame' if that's what it is, and note also there is no space, that's not a typo.

Once we clear the error hurdles, in four lines our work is done. If the user decides he doesn't like the switch, he just runs the script again.

Something I noticed on playing with this script is that it doesn't work so well in a situation where one or both pictures have an X-offset or Y-offset. A few more lines would be needed to get this information, and then transfer it.

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.