Scale/Rescale using resize doesn't fit I want to create a set of "distorted" images using python. The images should show possible misplacements of objects. The part of my script for translation and rotation seems to work well, but the scaling part doesn't work as expected. I want to randomly choose scaling parameters (random.gauss(1, 0.33)) to scale in the different axes - so I put the following in a while loop:

# 3 random variables for scaling in x, y and z directions
num1 = math.fabs(random.gauss(1,0.33))
num2 = math.fabs(random.gauss(1,0.33))
num3 = math.fabs(random.gauss(1,0.33))
# scaling with random variables
bpy.ops.transform.resize(value=(num1, 1, 1), constraint_axis=(True, False, False))
bpy.ops.transform.resize(value=(1, num2, 1), constraint_axis=(False, True, False))
bpy.ops.transform.resize(value=(1, 1, num3), constraint_axis=(False, False, True))


Then I save the image and want to get back to the "undistorted" image. So I tried:

# scale back to original image
bpy.ops.transform.resize(value=(1/num1, 1, 1), constraint_axis=(True, False, False))
bpy.ops.transform.resize(value=(1, 1/num2, 1), constraint_axis=(False, True, False))
bpy.ops.transform.resize(value=(1, 1, 1/num3), constraint_axis=(False, False, True))  


But this doesn't bring me back to the start. So I tried this in the console window of blenders UI. I have the simple startscreen featuring a camera, a cube and a lamp. For now resize and re-resize works. But if I turn the Cube by 45° round the z-axis it doesn't work any more. Scaling by (2,1,1) gives me a scale of (1.581,1.581,1) which suggests the scaling works in global coordinates. But why doesn't scaling with (0.5,1,1) give me the original cube. It gives me a scale of (1.25,1.25,1) instead.

So the question is, how do I get this to give me the original object back?
One way to do this is to get used to scaling using the obj.matrix_world and obj.matrix_local. Where obj is a reference to your Object. That way you can reset to the original scale using the identity matrix. You can even chain various kinds of matrix manipulations (Rotate, Scale, Location).

I hope to have time to give examples. The documentation shows how to do things, but it assumes you know Matrices and Vectors well enough to understand the terms: 

docs: mathutils.Matrix



Ideasman points out that object.scale can be used. This is a convenient way if all you need is to rescale, it lets you scale directly using a vector (or tuple.. or list..as long as there's 3 elements).

obj = bpy.data.objects['Cube']
obj.scale = (0.5, 0.3, 1.2)

# will return all dimensions to their original value
obj.scale = (1, 1, 1)  


You mention in your Question about rotation. Another convenient way to rotate an Object can be found in the set of attributes starting with .rotation_*:  see Docs for a description of: 

obj.rotation_axis_angle
obj.rotation_euler
obj.rotation_mode
obj.rotation_quaternion

# ---- needed imports to use euler and math module
import math
from mathutils import Euler

obj.rotation_euler = Euler((0, 0, math.radians(45)), 'XYZ')