Paint Transparent V2

Post Reply
User avatar
rbytes
Posts: 1808
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Paint Transparent V2

Post by rbytes » Mon Dec 24, 2018 8:05 pm

This is an improved version of the original Paint Transparent. It is used to remove the background from an image, leaving just the foreground person or object. Once you save the result, you can superimpose that foreground over a different background using various graphics apps and programs.

This update gives unlimited undos. Each brush stroke is saved, from the time you touch the screen until you lift your finger. The undo and redo buttons will recall previous or next strokes.

Also brush offset buttons have been added. These allow you to position your brush so it isn't hidden by your finger.

Finally, you can now draw regardless of whether the interface is hidden or not. The advantage of hiding the interface is that you can see more of the image underneath. However the brush size slider and the undo and redo buttons are never hidden, since they are used so frequently.

To show the full interface when it is hidden, press the * button.

IMAGES:
1. Example of how a transparent image can be placed on a new background image and combined with other transparent objects.
2. The program interface, with the background partially painted transparent using a large brush.
3. The Phaedra.jpg image to practice with.

Code: Select all

/*
Paint Transparent V2
by rbytes, December 2018

V2
Unlimited levels of undo are added, along with Undo and Redo buttons
to access them. Every brush stroke creates an undo level 
when you lift your finger. The Undo/Redo buttons let you step forward or back through these levels.
Smaller brushes can be completely hidden under your finger when drawing.
Therefore a new set of buttons allows you to offset the brush from
your finger's contact point far enough left, right, up or down so
that you can see exactly where it is painting. You may want to center large brushes under your finger.

V1
Paint the background of
a subject transparent and
save result as a .png image.

This program will run on an iPhone or iPod, but the
output image size will be very limited and a 4:3 image
will be cropped at the bottom. On an iPad, you will
be able to process and save a 2048 x 1536 image (~ 3 MB)

Basic instructions:
1. Set N$ as the name of the image you want to process. Do this in the green-
   highlighted code near the start of the program.
2. Use a large brush to paint large sections of background transparent.
   Brush size is adjusted with the slider
3. Use progressively smaller brushes as you paint closer to the edge of the
   center of interest (eg. person, animal, object)
4. When you are satisfied with the background removal, press the x button to
   show the other buttons.
5. Now press the Save button. Your transparent image will be saved as "painter.png"
*/

SET TOOLBAR OFF
OPTION SPRITE POS CENTRAL
SET ORIENTATION LANDSCAPE
SET BUTTONS CUSTOM
REFRESH OFF
laun$=LAUNCHER$ ()
dev$=DEVICE_TYPE$()
GET SCREEN SIZE sw,sh
iostest=0
IF iostest THEN
  sw=568
  sh=320
  dev$=""
ENDIF
rw=sw/1024!rh=sh/768
offst=40
'c'
' name of the photo you wish to process
N$="Phaedra.jpg"

''
IF dev$="iPad" THEN vshift=20
path$="PainTran/"
GOSUB defaults
GOSUB imagesprite

' delete old undo files
FOR t=1 TO 50
  FILE path$&"backup"&t&".png" DELETE
NEXT t

' create the drawing brush sprite
SPRITE "dsize" BEGIN 40*rw,40*rw
DRAW COLOR 0,0,0
a=1
DRAW ALPHA a
FOR t=0 TO 18*rw STEP .5
  DRAW CIRCLE 20*rw,20*rw SIZE t
  IF t>6*rw THEN
    a-=.04
    DRAW ALPHA a
  ENDIF
NEXT T
SPRITE "dsize" END
SPRITE "dsize" AT 60*rw,sh-60*rh SCALE .2*rw
SPRITE "dsize" SHOW
DRAW ALPHA 1
GOSUB interface
drawing=0
DRAW COLOR 0,0,0
SPRITE "image" STAMP
REFRESH ON

main_loop:

pen.SIZE=1+SLIDER_VALUE("size")*TOOLBAR.border*10*rw ' I added 1 so pen.size is never 0!
IF pen.SIZE <> pen.oldsize THEN
  pen.oldsize=pen.SIZE
  IF NOT swap THEN
    SPRITE "dsize" AT 60*rw,sh-60*rw SCALE .03*pen.SIZE
  ELSE
    SPRITE "dsize" AT oldpen.x,oldpen.y SCALE .03*pen.SIZE
  ENDIF
  REFRESH
END IF

IF bp("new") THEN
  bu=0
  FOR t=1 TO maxbu
    FILE path$&"backup"&t&".png" DELETE
  NEXT t
  GOSUB imagesprite
  GRAPHICS CLEAR
  SPRITE "image" STAMP
END IF

IF bp("save") THEN
  swap=1
  SPRITE "image" SAVE "painter.png"
  ALBUM EXPORT "painter.png"
END IF

IF bp("stop") THEN
  IF .laun$="desktop" THEN
    IF FILE_EXISTS("/launch") THEN
      RUN "/-Launch.sb"
    ELSE
      EXIT
    ENDIF 
  ENDIF
  END
ENDIF

GET TOUCH 0 AS x,y
IF bp("hide") THEN
    swap=1
    BUTTON "new" HIDE
    BUTTON "save" HIDE
    BUTTON "stop" HIDE
    BUTTON "swap" SHOW
    BUTTON "hide" HIDE
    BUTTON "1" HIDE
    BUTTON "2" HIDE
    BUTTON "3" HIDE
    BUTTON "4" HIDE
    BUTTON "5" HIDE
    FIELD "label" HIDE
    
  ENDIF
  
IF bp("swap") THEN
    swap=0
    BUTTON "new" SHOW
    BUTTON "save" SHOW
    BUTTON "stop" SHOW
    BUTTON "hide" SHOW
    BUTTON "swap" HIDE
    BUTTON "1" SHOW
    BUTTON "2" SHOW
    BUTTON "3" SHOW
    BUTTON "4" SHOW
    BUTTON "5" SHOW
    FIELD "label" SHOW
END IF

IF bp("undo") AND bu>0 THEN
  bu-=1
  REFRESH OFF
  IF bu>0 THEN
    SPRITE "image" LOAD path$&"backup"&bu&".png"
  ELSE
    GOSUB imagesprite
  ENDIF
  GRAPHICS CLEAR
  SPRITE "image" STAMP
  REFRESH ON
ENDIF

IF bp("redo") AND maxbu>0 THEN
  bu+=1
  IF FILE_EXISTS(path$&"backup"&bu&".png") THEN
    SPRITE "image" LOAD path$&"backup"&bu&".png"
    GRAPHICS CLEAR
    SPRITE "image" STAMP
  ELSE 
    bu-=1
  ENDIF
ENDIF

IF bp("1") THEN  ' left
  GOSUB alloff
  DRAW COLOR 1,1,1
  BUTTON "1" TEXT "◀️" AT BUTTON.x*1.8*rw,70*rh SIZE 40*rw,40*rh
  lr=-offst
  tb=0
ENDIF

IF bp("2") THEN  ' top
  GOSUB alloff
  DRAW COLOR 1,1,1
  BUTTON "2" TEXT "🔼" AT BUTTON.x*3.1*rw,25*rh SIZE 40*rw,40*rh
  lr=0
  tb=-offst
ENDIF

IF bp("3") THEN  ' right
  GOSUB alloff
  DRAW COLOR 1,1,1
  BUTTON "3" TEXT "▶️" AT BUTTON.x*4.4*rw,70*rh SIZE 40*rw,40*rh
  lr=offst
  tb=0
ENDIF

IF bp("4") THEN  ' bottom
  GOSUB alloff
  DRAW COLOR 1,1,1
  BUTTON "4" TEXT "🔽" AT BUTTON.x*3.1*rw,115*rh SIZE 40*rw,40*rh
  lr=0
  tb=offst
ENDIF

IF bp("5") THEN  ' center
  GOSUB alloff
  DRAW COLOR 1,1,1
  BUTTON "5" TEXT "⏹" AT BUTTON.x*3.1*rw,70*rh SIZE 40*rw,40*rh
  lr=0
  tb=0
ENDIF

IF x = -1 THEN
  IF drawn THEN
    SPRITE "image" SCAN 0,0, sw,sh
    bu+=1
    IF bu>maxbu THEN maxbu=bu
    drawn=0
    SPRITE "image" SAVE path$&"backup"&bu&".png"
  ENDIF
    'IF drawing THEN drawing=0
  GOTO main_loop
ELSE
  GOSUB DRAW
ENDIF
GOTO main_loop

' drawing sub
DRAW:
drawn=1
REFRESH ON
IF x<>-1 THEN SPRITE "dsize" AT x+lr,y+tb SCALE .03*pen.SIZE
GRAPHICS MODE DESTOUT
  ' draw
  IF NOT drawing THEN
    drawing=1
  ELSE
    IF x<>oldpen.x OR y<>oldpen.y THEN SPRITE "dsize" STAMP
  END IF
  oldpen.x=x
  oldpen.y=y
  GET TOUCH 0 AS x,y
  IF x<>-1 THEN GOTO DRAW
  GRAPHICS MODE NORMAL
RETURN

interface:
SLIDER.x=TOOLBAR.border
SLIDER.y=450*rh
SLIDER.SIZE=TOOLBAR.width-TOOLBAR.border*2
BUTTON.height=30*rw
BUTTON.x=SLIDER.x*2
BUTTON.y=SLIDER.y+BUTTON.height*1.2*rh
SLIDER "size" VALUE .5 AT SLIDER.x*7*rw, SLIDER.y+220*rh+vshift SIZE SLIDER.SIZE*rw
DRAW COLOR 0,0,0
FILL COLOR 1,1,1
SET BUTTONS FONT SIZE 20*rw
BUTTON "new" TEXT "NEW" AT BUTTON.x*14*rw, BUTTON.y+220*rh SIZE rw*SLIDER.SIZE/4, BUTTON.height*1.25
BUTTON "hide" TEXT "HIDE" AT BUTTON.x*17*rw, BUTTON.y+220*rh SIZE rw*SLIDER.SIZE/4, BUTTON.height*1.25
BUTTON "save" TEXT "SAVE" AT BUTTON.x*20*rw, BUTTON.y+220*rh SIZE rw*SLIDER.SIZE/4, BUTTON.height*1.25
BUTTON "stop" TEXT "STOP" AT BUTTON.x*23*rw, BUTTON.y+220*rh SIZE rw*SLIDER.SIZE/4, BUTTON.height*1.25
BUTTON "swap" TEXT "*" AT sw-60*rw,20*rh SIZE rw*SLIDER.SIZE/8, BUTTON.height
SET BUTTONS FONT SIZE 24*rw
BUTTON "undo" TEXT "⟲" AT BUTTON.x*26*rw, BUTTON.y+220*rh SIZE 50*rw, BUTTON.height*1.25
BUTTON "redo" TEXT "⟳" AT BUTTON.x*28*rw, BUTTON.y+220*rh SIZE 50*rw, BUTTON.height*1.25
GOSUB shiftbuttons
BUTTON "swap" HIDE
FIELD "label" FONT SIZE 20*rw
FIELD "label" TEXT "BRUSH SIZE" AT BUTTON.x*3.5*rw,720*rh SIZE 135*rw,30*rh
FIELD "label" FONT COLOR 0,0,0
FIELD "label" BACK ALPHA .3
FIELD "label" FONT SIZE 20*rh
RETURN

defaults:
' tool bar
TOOLBAR.width=SCREEN_WIDTH()/3
TOOLBAR.height=SCREEN_HEIGHT()
TOOLBAR.border=TOOLBAR.width/20
SCREEN.SCALE=SCREEN_SCALE()
IF NOT FILE_EXISTS("PainTran") THEN DIR "PainTran" CREATE
GRAPHICS
REFRESH OFF
GRAPHICS CLEAR
FILL COLOR 1,1,1
RETURN

imagesprite:
' load image and scale it to fill the screen
SPRITE "image" BEGIN sw,sh
GET IMAGE N$ SIZE WI,HI
DRAW IMAGE N$ AT 0,0 SCALE sw/WI
SPRITE "image" END
RETURN

shiftbuttons:
SET BUTTONS FONT SIZE 40*rw
DRAW COLOR 0,0,0
BUTTON "1" TEXT "◀️" AT BUTTON.x*1.8*rw,70*rh SIZE 40*rw,40*rh
BUTTON "2" TEXT "🔼" AT BUTTON.x*3.1*rw,25*rh SIZE 40*rw,40*rh
BUTTON "3" TEXT "▶️" AT BUTTON.x*4.4*rw,70*rh SIZE 40*rw,40*rh
BUTTON "4" TEXT "🔽" AT BUTTON.x*3.1*rw,115*rh SIZE 40*rw,40*rh
DRAW COLOR 1,1,1
BUTTON "5" TEXT "⏹" AT BUTTON.x*3.1*rw,70*rh SIZE 40*rw,40*rh
RETURN

alloff:
DRAW COLOR 0,0,0
BUTTON "1" TEXT "◀️" AT BUTTON.x*1.8*rw,70*rh SIZE 40*rw,40*rh
BUTTON "2" TEXT "🔼" AT BUTTON.x*3.1*rw,25*rh SIZE 40*rw,40*rh
BUTTON "3" TEXT "▶️" AT BUTTON.x*4.4*rw,70*rh SIZE 40*rw,40*rh
BUTTON "4" TEXT "🔽" AT BUTTON.x*3.1*rw,115*rh SIZE 40*rw,40*rh
BUTTON "5" TEXT "⏹" AT BUTTON.x*3.1*rw,70*rh SIZE 40*rw,40*rh
RETURN

DEF bp(a$) = BUTTON_PRESSED(a$)
Attachments
9216AE0E-AE8E-47DC-AB05-C82E1025C791.png
9216AE0E-AE8E-47DC-AB05-C82E1025C791.png (5.87 MiB) Viewed 34 times
0B6750B4-8118-42ED-8206-5BEF72F5D93A.png
0B6750B4-8118-42ED-8206-5BEF72F5D93A.png (4.21 MiB) Viewed 34 times
Phaedra.jpg
Phaedra.jpg (523.97 KiB) Viewed 35 times
####### Living the colorful life #######

Post Reply