Clipping an irregular mask from an image script: done, how to remove some files from temp dir?

I look everywhere for a clipping solution to just mask parts of an image and save them on a new file/object, those new ones should be transparent and can have irregular shapes.
My app will allow to copy heads of peoples or objects from a picture and paste them into another one.
 
As I didn’t find anything like that I created my own script, but I am not a programmer so it is really ugly code, I use a temporary file for creating a mask, but now I don’t know how to remove it from the directory.
 
Any Idea?
 
here is the code in case someone else is looking for something similar, it takes image.png, you can mask with your finger the portion of the image you want and then save it as a new object.
 
PS: I don’t know how make this look like lua code with colors :( 
 
 
local widget = require(“widget”)
local math = require(“math”)
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local w = display.actualContentWidth
local h = display.actualContentHeight
local minX = centerX  
local maxX = centerX
local minY = centerY
local maxY = centerY
local radius = 40
 
– load the image we want to clip
local image = display.newImageRect( “image.png”, 768, 1024)
image:translate( centerX, centerY )
 
 
– snapshot
local snapshot = display.newSnapshot(w, h)
snapshot:translate(centerX, centerY)
 
–solid background to create the black mask
local background = display.newRect( 0, 0, w, h)
background:setFillColor(0, 0, 0, 1)
snapshot.canvas:insert(background)
 
– load the same image again just to show some transparency at finger masking 
local image2 = display.newImageRect( “image.png” , 768, 1024 )
image2:translate( centerX, centerY )
image2.alpha = 0.5
 
– creating the mask 
local function reloadMask( destX, destY)
– just getting outter borders to create a smaller file
if(minX >= (destX)) then
minX = destX - radius
end
if(maxX <= (destX)) then
maxX = destX + radius
end
if(minY >= (destY )) then
minY = destY - radius
end
if(maxY <= (destY )) then
maxY = destY + radius
end
 
print(“1 = {” … minX … “,” … minY … “}”)
print(“2 = {” … maxX … “,” … maxY … “}”)
print(destX … " " … destY)
 
 local mask = display.newCircle( 0, 0, radius )
 mask.anchorX = 0
 mask.anchorY = 0
 mask.x = destX - display.contentWidth / 2
 mask.y = destY - display.contentHeight / 2
 
 mask.fill.blendMode = { srcColor = “one”, dstColor=“oneMinusSrcAlpha” }
 snapshot.canvas:insert(mask)
 snapshot:invalidate(“canvas”)
end
 
 --listener
local function screenTouch ( event )
  if (event.phase == “moved”) then
reloadMask( event.x, event.y)
  end
end
 
– hidding/showing images to get the final result
local function grabamask ( event )
if event.phase == “ended” then
image.isVisible = true
image2.isVisible = false
image2:removeSelf()
snapshot.isVisible = false
 
– let`s consume less memory cropping the original image
–add pixels to keep a black border (at least 3)
local screenBounds =
{
   xMin = minX +  - 10,
   xMax = maxX + radius*2 + 10 ,
   yMin = minY +  - 10,
   yMax = maxY + radius*2 + 10
}
– The mask image must have ‘width’  and ‘height’ dimensions that are divisible by 4., lets add some extra frame then
while (math.floor((screenBounds.xMax - screenBounds.xMin)/4) ~= (screenBounds.xMax - screenBounds.xMin)/4) do
screenBounds.xMax = screenBounds.xMax + 1 
end
while (math.floor((screenBounds.yMax - screenBounds.yMin)/4) ~= (screenBounds.yMax - screenBounds.yMin)/4) do
screenBounds.yMax = screenBounds.yMax + 1 
end
 
print("p1 = " … screenBounds.xMin … " : " … screenBounds.yMin)
print("p2 = " … screenBounds.xMax … " : " … screenBounds.yMax)
print ("x/4 = " … (screenBounds.xMax - screenBounds.xMin)/4)
print ("y/4 = " … (screenBounds.yMax - screenBounds.yMin)/4)
 
– Capture the bounds of the image  on screen.
local myCaptureImage = display.captureBounds( screenBounds )
myCaptureImage:translate(centerX, centerY)
display.save(myCaptureImage, ‘small.png’,  system.TemporaryDirectory)
myCaptureImage:removeSelf()
image:removeSelf()
 
snapshot.isVisible = true
myCaptureImage = display.captureBounds( screenBounds )
myCaptureImage:translate(centerX, centerY)
display.save(myCaptureImage, ‘tmp.png’,  system.TemporaryDirectory)
snapshot:removeSelf()
background:removeSelf()
background = nil 
 
–just confirm it is clipping :slight_smile:
local stripes = display.newRect( centerX, centerY, w, h)
stripes.fill.effect = “generator.stripes”
stripes.fill.effect.periods = { 8, 2, 4, 4 }
stripes.fill.effect.angle = 45
stripes.fill.effect.translation  = 0
 
– load the small version of image
local image = display.newImageRect( “small.png”,  system.TemporaryDirectory, screenBounds.xMax - screenBounds.xMin, screenBounds.yMax - screenBounds.yMin )
image:translate( centerX, centerY )

– add the newmask we created
local realmask = graphics.newMask( ‘tmp.png’ ,  system.TemporaryDirectory)
image:setMask(realmask)
display.save(image, ‘small.png’,  system.TemporaryDirectory)
 
end
end
 
local stage = display.getCurrentStage()
stage:addEventListener(“touch”, screenTouch)
 
local graba = widget.newButton({
shape = “roundedRect”,
       width = 40,
       height = 40,
       cornerRadius = 2,
       fillColor = { default={0.8,0,0,1}, over={0.7,0.7,0.7,0.4} },
       label = “C”,
       labelColor = { 
           default = { 0.8, 0.8, 0.8 }
       },
       labelYOffset = 0, 
       fontSize = 18,
       emboss = false,
       onRelease = grabamask
   })
   graba.x = 30
   graba.y = display.contentHeight - 40

If you know the file name you can use os.remove(). See: https://docs.coronalabs.com/api/library/os/remove.html

If you do not know the file name, use can use the lfs.* library to fetch a list of files in a folder and use that information along with os.remove() to do your clean up. https://docs.coronalabs.com/api/library/lfs/index.html

Rob

excellent!, thanks

that was exactly what I need.

If you know the file name you can use os.remove(). See: https://docs.coronalabs.com/api/library/os/remove.html

If you do not know the file name, use can use the lfs.* library to fetch a list of files in a folder and use that information along with os.remove() to do your clean up. https://docs.coronalabs.com/api/library/lfs/index.html

Rob

excellent!, thanks

that was exactly what I need.