I've got a problem with Python 2.7.3-32bits on Windows. I put this code to see if anyone can help me out with this error. The comments are in Spanish but it don't affect the code.
import gtk
import numpy
import math
import os
#Pedimos el nombre de la imagen de origen
nombreFich = input("Por favor, introduzca el nombre de la imagen de origen:")
#Validar que existe el fichero
imagen1 = gtk.Image()
imagen1.set_from_file('C:\\Users\\xxx\\Desktop\\xxxx.png')
pb1 = imagen1.get_pixbuf()
pm1 = pb1.get_pixels_array()
#Hacemos una copia de la imagen
pm2 = pm1.copy()
#Validamos los puntos de distorsion hasta que sean validos
puntos = " "
arrayPuntos = " "
while(puntos == " " and len(arrayPuntos) < 4):
print"Por favor, introduzca los puntos de distorsión xc yc r e:"
puntos= raw_input()
arrayPuntos = puntos.split(" ")
#Sacamos los puntos separando la cadena por el caracter espacio
xc =(puntos[0])
yc =(puntos[1])
r =(puntos[2])
e =(puntos[3])
#función que calcula el grado de distorsión
def grado(self,z,e):
if(z>1):
return 1
elif(e<0):
return (1/z)**(-e/(1-e))
else:
return z**e
#Distorsionamos la imagen
def distors(xc,yc,r,e,x,y):
d = math.sqrt(x**2+y**2)#Sacamos la distancia
z = d/r
if(z!=0):
g=grado(z,e)
xm=x*g
ym=y*g
return xm,ym
else:
xm=x
ym=y
return xm,ym
def recorrido (pm1, xc, yc, r, e):
pm2 = pm1.copy()
x= str(--r)
y= str(--r)
while (y <= r):
while (x <= r):
xm, ym = mover(xc, yc, r, e, x, y)
pm2[yc+y][xc+x] = pm1[yc+ym][xc+xm]
x = x+1
y= y+1
x= -r
return pm2
pm2 = recorrido(pm1, xc, yc, r, e)
#Guardamos los cambios
pb2 = gtk.gdk.pixbuf_new_from_array(pm2,pb1.get_colorspace(),pb1.get_bits_per_sample())
nomfich2 = nombreFich+"_copia"
ext = os.path.splitext("C:\\Users\xxx\Desktop\xxxx.png_copia")[1][1:].lower()
pb2.save("C:\\Users\xxx\Desktop\xxxx.png_copia",ext)
print"FINISH"
When I run the python code I get the following error:
Traceback (most recent call last):
File "F:\Dropbox\Práctica Pitón\Práctica3.0.py", line 71, in <module>
pm2 = recorrido(pm1, xc, yc, r, e)
File "F:\Dropbox\Práctica Pitón\Práctica3.0.py", line 59, in recorrido
x= str(--r)
TypeError: bad operand type for unary -: 'str'
The error message is telling you that r
is a string. You can't negate a string.
Why is it a string? Well, it seems to come from here:
# ...
puntos= raw_input()
arrayPuntos = puntos.split(" ")
# ...
r =(puntos[2])
The split
method on a string returns a list of strings.
So, how do you solve this? Well, if r
is, say, the string "22", then float(r)
is the float 22.0
, and int(r)
is the integer 22
. One of those is probably what you want.
Once you add, say, r=int(r)
, your --r
will no longer be an exception.
But it probably isn't what you want. In Python, --r
just means the negation of the negation of r
—in other words, it's the same as -(-(r))
, which is just r
. You're probably looking for the equivalent of the C prefix operator--
, which decrements the variable and returns the new value. There is no such operator in Python; in fact, there are no operators that modify a variable and then return the value.
This is part of a larger issue. Python is designed to make statements and expressions as distinct as possible, to avoid confusion. C is designed to make as many things as possible expressions, to save typing. So, you often can't just translate one into the other line by line.
In this case, you have to do it in two steps, as Thanasis Petsas shows:
r -= 1
x = str(r)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With