Using py processing to cycle through a set of jpgs?

edited June 2018 in Python Mode

I am getting this error : 1st arg can't be coerced to processing.core.PImage

I have three jpgs named img1, img2, img3.

img1=loadImage("pic1.jpg")
img2=loadImage("pic2.jpg")
img3=loadImage("pic3.jpg")`

The variable names are in a list.

list1=["img1","img2","img3"]`

I wanted to iterate through the list using a for loop and call each image in sequence to run a simple animation, but I continue to get the error.

for i in list1:
      image(i, 100, 100)

Is this not possible with processing or is there a different way to make this work?

Answers

  • Not an expert in Py but your statement above reads like this:

    image("img1",0,0);

    which is not correct. I suggest you do this in setup:

    list1=[loadImage("img1"),loadImage("img2"),loadImage("img3")]

    Kf

  • ty you for trying, but that didn't seem to work.

  • edited June 2018

    Given your files are named in a number sequence, you should use a loop to loadImage() them all: *-:)

    NAME, EXT, IMAGES = 'pic', '.jpg', 3
    images = tuple( loadImage(NAME + `i` + EXT) for i in range(1, IMAGES + 1) )
    

    For more techniques, read on my Discourse post: ;)
    https://Discourse.Processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/7

  • Ok, ty. That is kind of what I thought. I changed the code so now it is a LIST of images, instead of a tuple. I am stuck on seeing all of the images though, it seems to only show the last one in the sequence. I just want it to cycle through the images one at a time.

    def setup():
        global images
        NAME, EXT, IMAGES = 'image', '.png', 3
        images = list( loadImage(NAME + `i` + EXT) for i in range(1, IMAGES + 1) )
        size(500, 500)
        background(255) # white
    
    def draw():
        global images
        print(images) # prints the list
        for i in images:
    
            background(155)
            print(i) # prints the individual graphic image variable
            image(i, 100, 100)
            frameRate(1) # trying to slow it down to see each
            print("starting over")
    
  • edited June 2018

    I changed the code so now it is a LIST of images, ...

    Dunno what you have against tuples... :o3 But for a comprehension list, there's a simpler syntax:
    Wrap that all up w/ a pair of square brackets []: :ar!

    images = [ loadImage(NAME + `i` + EXT) for i in range(1, IMAGES + 1) ]
    

    ... it seems to only show the last one in the sequence.

    The current content of the sketch's canvas is rendered to the window only after the callback draw() is returned! L-)

    That's why we can't see the drawing happening as we issue drawing commands. We only see its last state. #-o
    That's also why we call each draw() iteration an animation frame. @-)

    Therefore, in order to see each PImage displayed on the canvas on a sequence, we're gonna need to call image() or set() for only 1 of them for each draw() iteration. :-B

    Processing got a system variable called frameCount, which stores the number of times draw() had been called back: http://Py.Processing.org/reference/frameCount.html

    We can use that together w/ the module % operator: http://Py.Processing.org/reference/modulo.html
    In order to determine which index to use for each draw() iteration. *-:)

    Also use frameRate() to slow down the FPS for draw(), so we can see each PImage on the canvas for a few milliseconds: :>
    http://Py.Processing.org/reference/frameRate.html

    """
     Display 1 PImage Each draw() (v1.0.1)
     GoToLoop (2018-Jun-05)
    
     Forum.Processing.org/two/discussion/28039/
     using-py-processing-to-cycle-through-a-set-of-jpgs#Item_5
    """
    
    NAME, EXT = 'pic', '.jpg'
    IMAGES, FPS = 3, 2
    
    def setup():
        size(500, 500)
        frameRate(FPS)
    
        global images
        images = tuple( loadImage(NAME + `i` + EXT) for i in range(1, IMAGES + 1) )
        # images = [ loadImage(NAME + `i` + EXT) for i in range(1, IMAGES + 1) ]
    
    
    def draw():
        bg = int( random(PImage.ALPHA_MASK) )
        background(bg)
    
        idx = frameCount % IMAGES
        img = images[idx]
    
        x = width  - img.width  >> 1
        y = height - img.height >> 1
        set(x, y, img)
    
Sign In or Register to comment.