Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Split image into X parts

in Questions Posts: 808

I got an image with the size of eg. (64, 16), Can I split it into x images 16x16? Same with (64, 32)?
Basically, how to go on, so it would require less time. I tried using image:copy(x,y,w,h) but i guess i use it wrong.

Comments

  • dave1707dave1707 Mod
    Posts: 7,469

    Here’s an example to split an image.

    function setup()
        img=readImage("Cargo Bot:Icon")   -- size 110 x 110
    end
    
    function draw()
        background(0)
        fill(255)
        text("Tap screen to split the image",WIDTH/2,HEIGHT-200)
        sprite(img,WIDTH/2,HEIGHT/2)
        if img1~=nil then
            sprite(img1,WIDTH/2-100,HEIGHT/2-100)
            sprite(img2,WIDTH/2+100,HEIGHT/2-100)
            sprite(img3,WIDTH/2-100,HEIGHT/2+100)
            sprite(img4,WIDTH/2+100,HEIGHT/2+100)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            img1=img:copy(0,0,55,55)
            img2=img:copy(56,0,55,55)
            img3=img:copy(0,56,55,55)
            img4=img:copy(56,56,55,55)
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,469

    Here’s another example that splits the image into a lot of pieces.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
        img=readImage("Cargo Bot:About Info Panel")
        size=1
        split()
    end
    
    function draw()
        background(255, 166, 0, 255)
        v=1
        for x=1,size do
            for y=1,size do
                sprite(tab[v],80+(x-1)*w*1.2+w/2,250+(y-1)*h*1.2+h/2)
                v=v+1
            end
        end
        fill(255)
        text("size  "..size.." x "..size,WIDTH/2,HEIGHT-50)
        text("tap near the top to increase size",WIDTH/2,HEIGHT-80)
        text("tap near the bottom to decrease size",WIDTH/2,HEIGHT-100)
    end
    
    function split()
        w=img.width//size
        h=img.height//size
        tab={}
        for x=1,size do
            for y=1,size do
                table.insert(tab,img:copy((x-1)*w,(y-1)*h,w,h))
            end        
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            if t.y>HEIGHT/2 then
                size=size+1
            else
                size=size-1
            end
            if size<1 then
                size=1
            end
            split()
        end
    end
    
  • Posts: 808

    Thank you very much!

Sign In or Register to comment.