我正在开展一个项目,我想把彩色网格的图片作为输入(在这个例子中用乐高积木制作)并返回一个小得多的修改图片.
这是一个示例输入:

下面是一个非常小的8x8图像,结果如下:

这是预期结果的更大版本:

这是我目前的代码: 它只适用于黑白图像.
from PIL import Image
import re
black = [(110,110,110),(0,0,0)] #The highest value and the lowest RGB value for the color black
img = Image.open("input.jpg") #The input image
size = (8,8) #The dimensions of the output image
out = img.resize(size,resample=Image.LANCZOS) #Resize the image
for y in range(size[0]): #loop through every pixel
for x in range(size[1]):
if out.getpixel((x,y)) <= black[0] and out.getpixel((x,y)) >= black[1]: #check to see if the pixel is within the accepted black …Run Code Online (Sandbox Code Playgroud) 我一直试图让我的标签自动更新自己已经有一段时间了,我已阅读了十几个StackOverflow问题,但无济于事.
我有一个全局对象,其中包含一个整数值,我希望在其中一个窗口小部件类中显示一个标签.
widget类看起来像这样:
class Battle(Widget):
def __init__(self, **kwargs):
super(Battle, self).__init__(**kwargs)
#Enemy Stats
self.enemyBar = BoxLayout(orientation="horizontal", size=(Window.width, Window.height/8), center_y = Window.height - self.height/2)
self.enemyBar.add_widget(Label(text=enemy.name))
#Enemy Health Label
health_label = Label(text=str(enemy.health))
self.enemyBar.add_widget(health_label)
self.add_widget(self.enemyBar)
def update_health(instance, value):
health_label.text = str(enemy.health) #<-- Error happens here
enemy.bind(health=update_health)
Run Code Online (Sandbox Code Playgroud)
当enemy.health在程序中更改值时,我希望我的标签也会更改.我不想使用任何kivy语言,因为我更喜欢只有一个主要的python文件.
敌人对象是使用实体类创建的.这是实体代码:
class entity(Widget):
#entity creation
health = NumericProperty()
def __init__(self, health):
self.health = health
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,按下一个调用一个改变敌人健康状况的功能的按钮然后我收到一个错误:
全局名称'health_label'未定义
不知何故,当调用update_health函数时,程序不会看到在init中创建的health_label变量.