gpt4 book ai didi

python-3.x - noLoop 是否停止执行绘制?

转载 作者:行者123 更新时间:2023-12-04 09:34:43 25 4
gpt4 key购买 nike

这是我在这里的第一篇文章,所以如果我犯了任何错误,我深表歉意。
我最近开始研究 Python 模式下的处理,我正在尝试开发一个代码,在从您的计算机中选择图像后,读取颜色并将它们插入列表中。最后的想法是计算图像中某些颜色的百分比。为此,我使用以下代码:

img = None
tam=5
cores_img = []


def setup():
size (500, 500)
selectInput(u"Escolha a ilustração para leitura de cores", "adicionar_imagens")
noLoop()

def adicionar_imagens(selection):
global img
if selection == None:
print(u"Seleção cancelada")
else:
print(u"Você selecionou " + selection.getAbsolutePath())
img = loadImage(selection.getAbsolutePath())

def draw():
if img is not None:
image (img, 0, 0)
for xg in range(0, img.width, tam):
x = map(xg, 0, img.width, 0, img.width)
for yg in range(0, img.height, tam):
y = map(yg, 0, img.height, 0, img.height)
cor = img.get(int(x), int(y))
cores_img.append(cor)
print (cores_img)
我正在使用 noLoop() 以便将颜色仅添加一次到列表中。但是,抽签似乎没有进行。它执行设置操作,但当图像被选中时,什么也没有发生。也没有错误信息。
我完全不知道可能发生的事情。如果有人有任何想法并可以提供帮助,我真的很感激!

最佳答案

调用 noLoop() 确实停止了draw()从运行中循环,这意味着到您选择并想象什么都不会发生。
但是,您可以手动调用 draw() (或 redraw() )加载图像后:

img = None
tam=5
cores_img = []


def setup():
size (500, 500)
selectInput(u"Escolha a ilustração para leitura de cores", "adicionar_imagens")
noLoop()

def adicionar_imagens(selection):
global img
if selection == None:
print(u"Seleção cancelada")
else:
print(u"Você selecionou " + selection.getAbsolutePath())
img = loadImage(selection.getAbsolutePath())
redraw()

def draw():
if img is not None:
image (img, 0, 0)
for xg in range(0, img.width, tam):
x = map(xg, 0, img.width, 0, img.width)
for yg in range(0, img.height, tam):
y = map(yg, 0, img.height, 0, img.height)
cor = img.get(int(x), int(y))
cores_img.append(cor)
print (cores_img)
你应该注意几个细节:
  • 正如引用文献所述,调用 get()很慢:pixels[x + y * width]更快(只要记住调用 loadPixels() 如果数组看起来不正确)
  • PImage 已经有一个 pixels数组:调用 img.resize(img.width / tam, img .height / tam)应该对图像进行下采样,以便您可以阅读相同的列表
  • x = map(xg, 0, img.width, 0, img.width) (和类似的 y )从一个范围映射到没有影响的相同范围

  • 例如
    img = None
    tam=5
    cores_img = None


    def setup():
    size (500, 500)
    selectInput(u"Escolha a ilustração para leitura de cores", "adicionar_imagens")
    noLoop()

    def adicionar_imagens(selection):
    global img, cores_img
    if selection == None:
    print(u"Seleção cancelada")
    else:
    print(u"Você selecionou " + selection.getAbsolutePath())
    img = loadImage(selection.getAbsolutePath())
    print("total pixels",len(img.pixels))
    img.resize(img.width / tam, img.height / tam);
    cores_img = list(img.pixels)
    print("resized pixels",len(img.pixels))
    print(cores_img)

    def draw():
    pass
    更新

    I thought that calling noLoop on setup would make draw run once. Stillit won't print the image... I'm calling 'image (img, 0, 0)' at the endof 'else', on 'def adicionar_imagens (selection)'. Should I call itsomewhere else?


  • 想想adicionar_imagens在时间上,与 setup() 分开运行和 draw()
  • 你是对的,draw()应该被调用一次(因为 noLoop() ),但是它会在 setup() 被调用一次完成但不迟(因为导航文件系统、选择文件和确认需要时间)
  • draw()加载图像后需要强制再次运行

  • 这是一个更新的片段:
    img = None
    # optional: potentially useful for debugging
    img_resized = None
    tam=5
    cores_img = None


    def setup():
    size (500, 500)
    selectInput(u"Escolha a ilustração para leitura de cores", "adicionar_imagens")
    noLoop()

    def adicionar_imagens(selection):
    global img, img_resized, cores_img
    if selection == None:
    print(u"Seleção cancelada")
    else:
    print(u"Você selecionou " + selection.getAbsolutePath())
    img = loadImage(selection.getAbsolutePath())
    # make a copy of the original image (to keep it intact)
    img_resized = img.get()
    # resize
    img_resized.resize(img.width / tam, img.height / tam)
    # convert pixels array to python list
    cores_img = list(img.pixels)
    # force redraw
    redraw()
    # print data
    print("total pixels",len(img.pixels))
    print("resized pixels",len(img.pixels))
    # print(cores_img)

    def draw():
    print("draw called " + str(millis()) + "ms after sketch started")
    # if an img was selected and loaded, display it
    if(img != None):
    image(img, 0, 0)
    # optionally display resized image
    if(img_resized != None):
    image(img_resized, 0, 0)

    以下是一些可能会有所帮助的注意事项:
  • 列表中的每个像素都是 24 位 ARGB 颜色(例如,所有 channel 都存储在单个值中)。如果您需要单独的颜色 channel ,请记住您具有 red() 之类的功能, green() , blue() 可用的。 (另外,如果速度变慢,请注意该示例包括使用位移和掩码的更快版本)
  • Histogram example可能会有所帮助。您需要将 Java 语法移植到 Python 语法并使用 3 个直方图(每个颜色 channel 一个),但是很好地说明了计算强度的原理

  • histogram overlaid on image

    关于python-3.x - noLoop 是否停止执行绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62644397/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com