Resize Objects: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 110: Line 110:
Enter a value to shrink/enlarge by, click Ok.
Enter a value to shrink/enlarge by, click Ok.


You can also enter 2 values for width and height,
You can also enter 2 different values for width and height,


separated by white space.
separated by white space.

Revision as of 16:16, 4 August 2012

Here is a script, (a pair of them really) that does something simple – alters the size of an object. This probably has some limited use, since it approaches the task by suggesting that you want to increase or decrease the size of an object by some mathematical factor. Yet, it still might get you somewhere in the ballpark, so that finer tuning is then possible by other means. You can also undo what you've done with this script, then run it again with a different factor, for another form of finetuning.

resizeobject.py

This first version asks for a single input for enlarging or shrinking. The default of 0.5 would halve the width and height. To double, enter 2. It seems to work on various kinds of frames, shapes, arcs, groups (a true group, not just a collection of selected objects), and even vector objects. Although it works on a spiral, there is a display problem after running the script, which seemingly can only be overcome by saving the file, closing, then reloading.

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

# ****************************************************************************
#  This program is free software; you can redistribute it and/or modify 
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ****************************************************************************


"""

© 2012 by Gregory Pittman

Select an object, start script.

Enter a value to shrink/enlarge by, click Ok.

"""

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 not scribus.haveDoc():
    scribus.messageBox('Scribus - Script Error', "No document open", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)

if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Script Error',
            "There is no object selected.\nPlease select a text frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Script Error',
            "You have more than one object selected.\nPlease select one text frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
selected_frame = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
scribus.setRedraw(False)

dimensions = scribus.getSize(selected_frame) # (width, height)
factor = scribus.valueDialog("Resize Object", "Resize by multiple or decimal fraction", "0.5")
factor = float(factor)
newwidth = dimensions[0]*factor
newheight = dimensions[1]*factor
scribus.sizeObject(newwidth, newheight)

scribus.setRedraw(True)
scribus.redrawAll()

resizeobject2.py

But what if you want to resize the width and height by different values? What if you want to shrink width but enlarge the height?

You might consider resizeobject2.py the preferred version, since it not only works just like resizeobject.py and allows you to enter a single value, but also permits entering 2 values, one for width, one for height. This also demonstrates my preferred way to enter two values from a single dialog, by using the python split function.

There is some error detection, but one kind of error not detected is entering no value or more than two. This can be easily added after the n = len(factor) line.

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

# ****************************************************************************
#  This program is free software; you can redistribute it and/or modify 
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ****************************************************************************


"""

© 2012 by Gregory Pittman

Select an object, start script.

Enter a value to shrink/enlarge by, click Ok.

You can also enter 2 different values for width and height,

separated by white space.

"""

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 not scribus.haveDoc():
    scribus.messageBox('Scribus - Script Error', "No document open", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)

if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Script Error',
            "There is no object selected.\nPlease select a text frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Script Error',
            "You have more than one object selected.\nPlease select one text frame and try again.",
            scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
selected_frame = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
scribus.setRedraw(False)

dimensions = scribus.getSize(selected_frame) # (width, height)
factors = scribus.valueDialog("Resize Object", "Resize by multiple or decimal fraction", "0.5")
factor = factors.split()
n = len(factor)
factor1 = float(factor[0])
if (n == 1):
    factor2 = factor1
else:
    factor2 = float(factor[1])

newwidth = dimensions[0]*factor1
newheight = dimensions[1]*factor2
scribus.sizeObject(newwidth, newheight)

scribus.setRedraw(True)
scribus.redrawAll()