裁剪时出现元组错误

时间:2014-02-06 02:02:19

标签: python tuples crop

我想使用此命令裁剪图像。

def imgCrop(im):
        box = (0, 150, 640, 200)
        region = im.crop(box)
        region.save('crop.jpg')
        return region

然后错误就像这样

AttributeError: 'tuple' object has no attribute 'crop'

实际上即时引用http://effbot.org/imagingbook/image.htm,这种方法是正确的。请指教。

这是我的完整计划,请指教

import cv2
from cv2 import *
from PIL import Image 
import RPi.GPIO as GPIO
import time
im = 0

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(27, GPIO.OUT)        #Input 1
GPIO.setup(22, GPIO.OUT)        #Input 2
GPIO.setup(9, GPIO.OUT)         #Input 3
GPIO.setup(10, GPIO.OUT)        #Input 4
GPIO.setup(7, GPIO.OUT)         #Enable 1
GPIO.setup(8, GPIO.OUT)         #Enable 2

GPIO.setup(15, GPIO.IN)         #turn right
GPIO.setup(18, GPIO.IN)         #turn left
GPIO.setup(23, GPIO.IN)         #backward
GPIO.setup(24, GPIO.IN)         #forward

p = GPIO.PWM(7,50)
q = GPIO.PWM(8,50)
p.start(0)
q.start(0)

def forward():
    p.ChangeDutyCycle(100)  
    q.ChangeDutyCycle(100)
    GPIO.output(27, GPIO.HIGH)      #input 1 is LOW
    GPIO.output(22, GPIO.LOW)       #input 2 is High
    GPIO.output(9, GPIO.HIGH)       #input 1 is LOW
    GPIO.output(10, GPIO.LOW)       #input 2 is High

def backward():
    p.ChangeDutyCycle(100)  
    q.ChangeDutyCycle(100)
    GPIO.output(27, GPIO.LOW)       #input 1 is LOW
    GPIO.output(22, GPIO.HIGH)      #input 2 is High
    GPIO.output(9, GPIO.LOW)        #input 1 is LOW
    GPIO.output(10, GPIO.HIGH)      #input 2 is High

def left():
    p.ChangeDutyCycle(100)  
    q.ChangeDutyCycle(100)
    GPIO.output(27, GPIO.LOW)       #input 1 is LOW
    GPIO.output(22, GPIO.HIGH)      #input 2 is High
    GPIO.output(9, GPIO.HIGH)       #input 1 is LOW
    GPIO.output(10, GPIO.LOW)

def right():
    p.ChangeDutyCycle(100)  
    q.ChangeDutyCycle(100)
    GPIO.output(27, GPIO.HIGH)      #input 1 is LOW
    GPIO.output(22, GPIO.LOW)       #input 2 is High
    GPIO.output(9, GPIO.LOW)        #input 1 is LOW
    GPIO.output(10, GPIO.HIGH)

def stop():
    p.ChangeDutyCycle(100)  
    q.ChangeDutyCycle(100)
    GPIO.output(27, GPIO.LOW)       #input 1 is LOW
    GPIO.output(22, GPIO.LOW)       #input 2 is High
    GPIO.output(9, GPIO.LOW)        #input 1 is LOW
    GPIO.output(10, GPIO.LOW)

def imgCrop(im):
        box = (0, 150, 640, 200)
        region = im.crop(box)
        region.save('crop.jpg')
    return region

def imgThres(im):
        gray = im.convert('L')
        bw = gray.point(lambda x: 0 if x<50 else 255, '1')
        bw.save("bw.jpg")
    return bw

def find_centroid(im, rez):
        width, height = im.size
        XX, YY, count = 0, 0, 0
        for x in xrange(0, width, rez):
            for y in xrange(0, height, rez):
                    if im.getpixel((x, y)) == 255:
                        XX += x
                        YY += y
                        count += 1
        return XX/count, YY/count

def camera ():
    cam = VideoCapture(0)   # 0 -> index of camera
    Img = cam.read()
    cImg = imgCrop(Img)
    tImg = imgThres(cImg)
    print find_centroid(tImg, 1)

def robo_direct():
        cen = find_centroid(tImg, 1)
        diff = cen[0] - 320
        if diff > 10:
        right()
            print 'right'
        if diff < -10:
        left()
            print 'left'
        else:
        forward()
            print 'straight'



####---------------------------------------------------------------------------------####

forward()
while True:
    camera ()
    robo_direct()

1 个答案:

答案 0 :(得分:0)

VideoCapture.read不会返回图片。如documentation中所列,它返回retval, image元组。不知道retval是什么,但那是你的问题。


im是一个元组。元组没有crop方法。如果im不应该是元组,请查看调用此函数的位置,并找出传递元组的原因。

此外,您还有用于缩进的混合制表符和空格。这可能会或可能不会立即引起问题,但在某些时候它肯定会让人头疼。切换到所有选项卡或所有空格; PEP 8推荐所有空间。