Creating Text Frames for Image Captions: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
I tend to prefer simple scripts, with little or no input needed, which is why I made individual scripts for each position, but at the bottom, I'll show how to create one script which needs input to state which position to place the caption. The sizes and positions of frames can be changed according to your needs, and like any frame, can be edited afterward. Your page units don't matter, since the script converts to points and uses points, then switches back again.
I tend to prefer simple scripts, with little or no input needed, which is why I made individual scripts for each position, but at the bottom, I'll show how to create one script which needs input to state which position to place the caption. The sizes and positions of frames can be changed according to your needs, and like any frame, can be edited afterward. Your page units don't matter, since the script converts to points and uses points, then switches back again.


In usage, this would be a situation where you could save time be creating a caption Paragraph Style for the caption frames.
In usage, this would be a situation where you could save time be creating a caption Paragraph Style for the caption frames. One thing to notice is that the scripts do not check for object type, so you can create "captions" for any object, such as shapes, render frames, vector graphics, or even groups of objects (make sure you actually group them, since if you just select a number of objects, each one will get its own caption).


[[File:Image_captions.png]]
[[File:Image_captions.png]]

Revision as of 17:16, 3 January 2017

This article is part of the Scripts series.

Here are a series of scripts which accomplish the simple task of creating a caption frame for images. You can either do frames one-by-one, or select a number of frames as long as you want the captions in the same position relative to the image.

I tend to prefer simple scripts, with little or no input needed, which is why I made individual scripts for each position, but at the bottom, I'll show how to create one script which needs input to state which position to place the caption. The sizes and positions of frames can be changed according to your needs, and like any frame, can be edited afterward. Your page units don't matter, since the script converts to points and uses points, then switches back again.

In usage, this would be a situation where you could save time be creating a caption Paragraph Style for the caption frames. One thing to notice is that the scripts do not check for object type, so you can create "captions" for any object, such as shapes, render frames, vector graphics, or even groups of objects (make sure you actually group them, since if you just select a number of objects, each one will get its own caption).

Image captions.png

bcaption.py

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

© 2017 Gregory Pittman

bcaption.py

Creates a text frame (caption) below one or more selected frames.

"""
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)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy+fheight, fwidth, 24)
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)

tcaption.py

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

"""

© 2017 Gregory Pittman

tcaption.py

Creates a text frame (caption) above one or more selected frames.

"""

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)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)
                
while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy-24, fwidth, 24) # frame height set at 24 points
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)

rcaption.py

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

"""

© 2017 Gregory Pittman

rcaption.py

Creates a text frame (caption) to the right of one or more selected frames.

"""

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)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx + fwidth, fy, 150, 40)
    
scribus.setRedraw(True)

lcaption.py

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

"""

© 2017 Gregory Pittman

lcaption.py

Creates a text frame (caption) to the left of one or more selected frames.

"""

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)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)


while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx-150, fy + fheight - 40, 150, 40)
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)

caption.py

Here is one more script which incorporates all the above options, by allowing you to choose position in a dialog. If you enter something other than b/t/r/l it shouldn't do anything.

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

"""

© 2017 Gregory Pittman

caption.py

Creates a text frame (caption) in selected location relative to 
one or more selected frames.

"""

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)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

captionloc = scribus.valueDialog("Caption Location","Where to put the caption(s) -\n B/T/R/L?", "b")
captionloc = captionloc[0]
location = captionloc.upper()

pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    if location == "B":
        textf = scribus.createText(fx, fy+fheight, fwidth, 24)# frame height set at 24 points
    elif location == "T":
        textf = scribus.createText(fx, fy-24, fwidth, 24) # frame height set at 24 points
    elif location == "R":
        textf = scribus.createText(fx + fwidth, fy, 150, 40)# frame width set at 150, height at 40 points
    elif location == "L":
        textf = scribus.createText(fx-150, fy + fheight - 40, 150, 40) # frame width set at 150, height at 40 points

scribus.setUnit(pageunits)

scribus.setRedraw(True)