Resize Objects: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Scripting Index}}
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.
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.
Of course, you could also accomplish what this script does in the Properties palette. Just enter *0.5 after the width value while locking the width/height ratio, and you halve each value. On the other hand, the working bits of these scripts might serve as a part of some larger script, where resizing some object is only part of what you are doing.


===resizeobject.py===
===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.
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.
<pre>
#!/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()
</pre>
===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 <code>resizeobject2.py</code> the preferred version, since it not only works just like <code>resizeobject.py</code> 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 <code>split</code> 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 <code>n = len(factor)</code> line. As it is, you can enter more than two values, but anything after the second will be ignored.
<pre>
#!/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:\n width height, separated by white space", "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()
</pre>
It's worth mentioning these particular behaviors:
If you enlarge a text frame with text, the frame is enlarged but not the
font.
If you enlarge a Bezier curve, the curve is enlarged, but the line width
is the same.
If you create a group of a text frame and Bezier curve, then enlarge,
the font increases in size, and the width of the Bezier line increases.
This might actually be a desired result, but is worth considering.

Latest revision as of 00:10, 5 August 2012

This article is part of the Scripts series.

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.

Of course, you could also accomplish what this script does in the Properties palette. Just enter *0.5 after the width value while locking the width/height ratio, and you halve each value. On the other hand, the working bits of these scripts might serve as a part of some larger script, where resizing some object is only part of what you are doing.

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. As it is, you can enter more than two values, but anything after the second will be ignored.

#!/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:\n width height, separated by white space", "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()

It's worth mentioning these particular behaviors:

If you enlarge a text frame with text, the frame is enlarged but not the font. If you enlarge a Bezier curve, the curve is enlarged, but the line width is the same.

If you create a group of a text frame and Bezier curve, then enlarge, the font increases in size, and the width of the Bezier line increases. This might actually be a desired result, but is worth considering.