【问题标题】:Compare Two String Variables, Do Something If They Are The Same比较两个字符串变量,如果它们相同则做某事
【发布时间】:2019-01-25 14:54:58
【问题描述】:

我希望比较 Python 中的两个字符串变量,如果它们相同,则打印 same。不幸的是,我无法让它工作,same 永远不会被打印出来。我的一个字符串只是一个简单的变量,而另一个是来自 ImageGrab 模块的 RGB 输出。

代码如下:

from PIL import ImageGrab
import threading

cc = "(255, 255, 255)"

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab((960,540,961,541)).load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=pixel[x,y]
            print(pxcolor)
            if pxcolor == cc:
                print("same")

getcol()

我尝试过使用pxcolor = pxcolor.strip(),但这返回了这个错误:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\mikur\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\mikur\Python\Python37-32\lib\threading.py", line 1158, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\mikur\Desktop\tye.py", line 14, in getcol
    pxcolor = pxcolor.strip()
AttributeError: 'tuple' object has no attribute 'strip'

【问题讨论】:

  • 我不熟悉这个库,但是你确定 pxcolor 是一个字符串吗?你能检查一下type(pxcolor)吗?看起来它可能是一个元组。
  • pxcolor 是一个 tuple (错误信息很清楚)。您的比较永远不会评估为 True。尝试cc = (255, 255, 255)(将其声明为 tuplenot string)。或者(跛脚)其他方式:if str(pxcolor) == cc:
  • 是的,对于新手的错误,我们深表歉意。

标签: python string variables python-imaging-library rgb


【解决方案1】:

只需要将 pxcolor 通过 str() 转换为字符串即可进行比较

from PIL import ImageGrab
import threading

cc = "(45, 42, 46)"

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab((960,540,961,541)).load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=str(pixel[x,y])
            print(pxcolor)
            if pxcolor == cc:
                print("same")

getcol()

根据 Kevin 的建议,在开始时将 cc 变量设为元组

from PIL import ImageGrab
import threading

cc = (45, 42, 46)

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab((960,540,961,541)).load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=pixel[x,y]
            print(pxcolor)
            if pxcolor == cc:
                print("same")

getcol()

【讨论】:

  • 最好首先让ccpxcolor 的类型相同,而不是将pxcolor 转换为与cc 的类型匹配。对象的类型应反映其用途;元组比字符串更接近颜色。
【解决方案2】:

cc 是一个 str,而 pxcolor 是一个元组

你要么需要将 cc 更改为元组,要么将 pxcolor 更改为字符串,然后检查 == 语句:

元组到字符串

from PIL import ImageGrab
import threading

cc = "(255, 255, 255)"

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab((960,540,961,541)).load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=pixel[x,y]
            print(pxcolor)
            if str(pxcolor) == cc:
                print("same")

元组字符串

from PIL import ImageGrab
import threading


cc = "(255, 255, 255)"

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab((960,540,961,541)).load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=pixel[x,y]
            print(pxcolor)

            elements = cc[1:-1].split(",")
            tuple_cc = [ int(x) for x in elements ]
            mytuple = tuple(tuple_cc) 

            if pxcolor == mytuple:
                print("same")

【讨论】:

  • 我喜欢将 cc 更改为元组的想法。但是首先简单地执行cc = (255, 255, 255)而不是在运行时调用splittuple()不是更容易吗?
  • 是的。好电话@Kevin。显然,如果您可以立即将 cc 作为元组,那将是理想的。但我不知道 Mihkel 所读的 cc 是什么。所以只是提供了两种方法让他的代码在 cc 是一个字符串的情况下工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多