Image crop

I tried to post an image to the forum earlier, but it wouldn’t let me and said the image size was too big. So I wrote this to crop or reduce the size of an image. Just select an image in the readImage line and run the code. Put your finger on any of the lines and slide it to crop your image. To fine tune the positions, tap near a line either above/below, right/left to move it 1 pixel in that direction. At the top of the screen is the width and height value of the cropped image. When your ready to crop, tap the > upper left screen to show the parameter window. Tap copy to show you what the cropped image will look like. If you need to fine tune it, tap clear and reposition the lines. If it’s OK, tap save and it will be in the Dropbox folder with the name newImg. If you don’t see the newImg in the Dropbox folder, you need to exit the code. Nothing too fancy, just something I thru together.

-- image crop

viewer.mode=FULLSCREEN

function setup()
    
    img=readImage(asset.documents.Dropbox.jor)
    
    parameter.action("Copy",copy) 
    parameter.action("Clear",clear)
    parameter.action("Save",save)
    stroke(255) 
    strokeWidth(2)
    iw=img.width//1
    ih=img.height//1
    l=(WIDTH-iw)//2
    b=(HEIGHT-ih)//2
    left=(WIDTH*.3)//1
    right=(WIDTH*.7)//1
    top=(HEIGHT*.7)//1
    bottom=(HEIGHT*.3)//1
    fill(255)
end

function draw()
    background(0)
    if newImg~=nil then
        sprite(newImg,WIDTH/2,HEIGHT/2)
    else
        sprite(img,WIDTH/2,HEIGHT/2)
        line(left,0,left,HEIGHT)
        line(right,0,right,HEIGHT)    
        line(0,top,WIDTH,top)
        line(0,bottom,WIDTH,bottom)
    end
    text("Width "..right-left.."     Height "..top-bottom,WIDTH/2,HEIGHT-30)
end

function touched(t)
    if t.state==BEGAN then
        if math.abs(t.x-left)<50 then 
            if t.x<left then
                left=left-1
            else
                left=left+1
            end
        end
        if math.abs(t.x-right)<50 then
            if t.x<right then
                right=right-1
            else
                right=right+1
            end
        end
        if math.abs(t.y-top)<50 then
            if t.y<top then
                top=top-1
            else
                top=top+1
            end
        end
        if math.abs(t.y-bottom)<50 then
            if t.y<bottom then
                bottom=bottom-1
            else
                bottom=bottom+1
            end
        end
    end
    if t.state==CHANGED then
        if math.abs(t.x-left)<50 then
            left=left+t.deltaX
            if left>=right then
                left=right-10
            end
        end
        if math.abs(t.x-right)<50 then
            right=right+t.deltaX
            if right<=left then
                right=left+10
            end
        end
        if math.abs(t.y-top)<50 then
            top=top+t.deltaY
            if top<=bottom then
                top=bottom+10
            end
        end
        if math.abs(t.y-bottom)<50 then
            bottom=bottom+t.deltaY
            if bottom>=top then
                bottom=top-10
            end
        end
    end
    if t.state==ENDED then
        left=left//1
        right=right//1
        top=top//1
        bottom=bottom//1
    end
end

function copy()
    output.clear()
    print("Image copied")
    newImg=img:copy((left-l)//1,(bottom-b)//1,(right-left)//1,(top-bottom)//1)
end

function save()
    output.clear()
    if newImg~=nil then
        print("Image saved")
        saveImage(asset.documents.Dropbox.."newImg",newImg)
    else
        print("Copy image first")
    end
end

function clear()
    output.clear()
    print("Image cleared")
    newImg=nil
end

@dave1707 - it’s little routines like this that should be preserved and updated in a library of utils made available to users. Can you put this in the wiki?

@Bri_G I’ve got some changes to make now that I thought about it for awhile.

Here’s an updated version. You can move two lines by selecting where they cross. Also, you can move all 4 lines by selecting a screen position away from any line. That comes in handy if you crop to a specific width and height and you want to move it anywhere on the screen.

-- image crop

viewer.mode=FULLSCREEN

function setup()
    
    img=readImage(asset.builtin.Cargo_Bot.Startup_Screen)
    
    parameter.action("Copy",copy) 
    parameter.action("Clear",clear)
    parameter.action("Save",save)
    stroke(255) 
    strokeWidth(2)
    iw=img.width//1
    ih=img.height//1
    l=(WIDTH-iw)//2
    b=(HEIGHT-ih)//2
    left=(WIDTH*.3)//1
    right=(WIDTH*.7)//1
    top=(HEIGHT*.7)//1
    bottom=(HEIGHT*.3)//1
    fill(255)
end

function draw()
    background(0)
    if newImg~=nil then
        sprite(newImg,WIDTH/2,HEIGHT/2)
    else
        sprite(img,WIDTH//2,HEIGHT//2)
        line(left,0,left,HEIGHT)
        line(right,0,right,HEIGHT)    
        line(0,top,WIDTH,top)
        line(0,bottom,WIDTH,bottom)
    end
    text("Width "..right-(left).."     Height "..top-bottom,WIDTH/2,HEIGHT-30)
end

function touched(t)
    if t.state==BEGAN then
        if math.abs(t.x-left)>50 and math.abs(t.x-right)>50 and 
            math.abs(t.y-top)>50 and math.abs(t.y-bottom)>50 then
            moveAll=true
        else
            left=moveOne(t.x,left,right)
            right=moveOne(t.x,right,left)
            top=moveOne(t.y,top,bottom)
            bottom=moveOne(t.y,bottom,top)
        end
    elseif t.state==CHANGED then
        if moveAll then
            left=left+t.deltaX
            right=right+t.deltaX
            top=top+t.deltaY
            bottom=bottom+t.deltaY
        else
            left=moveMore(t.x,t.deltaX,left,right)
            right=moveMore(t.x,t.deltaX,right,left)
            top=moveMore(t.y,t.deltaY,top,bottom)
            bottom=moveMore(t.y,t.deltaY,bottom,top)
        end
    elseif t.state==ENDED then
        if math.abs(left-right)<30 then left=right-30 end
        if math.abs(top-bottom)<30 then top=bottom+30 end
        moveAll=false
        left=left//1
        right=right//1
        top=top//1
        bottom=bottom//1
    end
end

function moveOne(t,line1)
    if math.abs(t-line1)<30 then
        if t<line1 then 
            line1=line1-1
        else
            line1=line1+1
        end
    end
    return line1
end

function moveMore(t,td,line1,line2)
    if math.abs(t-line1)<50 then
        line1=line1+td
    end
    return line1
end

function copy()
    output.clear()
    print("Image copied")
    newImg=img:copy((left+1-l)//1,(bottom-b)//1,(right-left)//1,(top-bottom)//1)
end

function save()
    output.clear()
    if newImg~=nil then
        print("Image saved")
        saveImage(asset.documents.Dropbox.."newImg",newImg)
    else
        print("Copy image first")
    end
end

function clear()
    output.clear()
    viewer.mode=FULLSCREEN
    print("Image cleared")
    newImg=nil
end