Page 1 of 1

How to make Transparent Background?

Posted: Sun Aug 06, 2023 2:10 pm
by Little_man
Hello,

How to make the background transparent?

Little_man

Code: Select all


UsePNGImageDecoder()
UsePNGImageEncoder()

Global Dim File$(0), FileCount

Global.i Width     = 370
Global.i Height    = 376
Global.i Seoerator = 20
 
ExamineDirectory(0, "C:\Dump", "*.png")
 
While NextDirectoryEntry(0)
  If DirectoryEntryType(0) & #PB_DirectoryEntry_File
    File$(FileCount) = DirectoryEntryName(0)
    FileCount + 1
    ReDim File$(FileCount)
  EndIf
Wend

Debug FileCount

For X = 0 To FileCount - 1 Step 4
  CreateImage(10, 1580, 410) 
  LoadImage(0, "C:\Dump\" + File$(X))       ;Transparent.
  LoadImage(1, "C:\Dump\" + File$(X + 1))   ;Transparent.
  LoadImage(2, "C:\Dump\" + File$(X + 2))   ;Transparent.
  LoadImage(3, "C:\Dump\" + File$(X + 3))   ;Transparent.
     
  StartDrawing(ImageOutput(10))
  ;DrawingMode(#PB_2DDrawing_Transparent)
  FrontColor($FFFFFF)
    DrawImage(ImageID(0),  20,  20)
    DrawImage(ImageID(1),  410, 20)
    DrawImage(ImageID(2),  800, 20)
    DrawImage(ImageID(3), 1190, 20)
  StopDrawing()
       
  SaveImage(10, "c:\Dump\1\" + Str(X) + ".png",#PB_ImagePlugin_PNG) ;Background "Black"

  FreeImage(10)
Next

Re: How to make Transparent Background?

Posted: Sun Aug 06, 2023 2:20 pm
by STARGÅTE

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

Global Dim File$(0), FileCount

Global.i Width     = 370
Global.i Height    = 376
Global.i Seoerator = 20
 
ExamineDirectory(0, "C:\Dump", "*.png")
 
While NextDirectoryEntry(0)
  If DirectoryEntryType(0) & #PB_DirectoryEntry_File
    File$(FileCount) = DirectoryEntryName(0)
    FileCount + 1
    ReDim File$(FileCount)
  EndIf
Wend

Debug FileCount

For X = 0 To FileCount - 1 Step 4
  CreateImage(10, 1580, 410, 32, #PB_Image_Transparent) ; use 32 bit color and transparent background
  LoadImage(0, "C:\Dump\" + File$(X))       ;Transparent.
  LoadImage(1, "C:\Dump\" + File$(X + 1))   ;Transparent.
  LoadImage(2, "C:\Dump\" + File$(X + 2))   ;Transparent.
  LoadImage(3, "C:\Dump\" + File$(X + 3))   ;Transparent.
     
  StartDrawing(ImageOutput(10))
  ;DrawingMode(#PB_2DDrawing_Transparent)
  ;FrontColor($FFFFFF)
    DrawAlphaImage(ImageID(0),  20,  20) ; Draw image with alpha channel
    DrawAlphaImage(ImageID(1),  410, 20)
    DrawAlphaImage(ImageID(2),  800, 20)
    DrawAlphaImage(ImageID(3), 1190, 20)
  StopDrawing()
       
  SaveImage(10, "c:\Dump\1\" + Str(X) + ".png",#PB_ImagePlugin_PNG) ;Background "Black"

  FreeImage(10)
Next

Re: How to make Transparent Background?

Posted: Sun Aug 06, 2023 2:30 pm
by Little_man
Thanks; STARGÅTE

Thanks for the info !!...

Little_man

Re: How to make Transparent Background?

Posted: Mon Aug 07, 2023 5:25 am
by netmaestro
An approach you can use when the source image has a background color and no alpha layer:

Code: Select all

Declare.q RemoveBKG(x,y,sourcecolor,targetcolor)

img0 = CatchImage(#PB_Any, ?imagestart, 49206)
img1 = CreateImage(#PB_Any,128,128,32,#PB_Image_Transparent)

StartDrawing(ImageOutput(img1))
  DrawingMode(#PB_2DDrawing_CustomFilter)
  CustomFilterCallback(@RemoveBKG())
  DrawImage(ImageID(img0),0,0)
StopDrawing()

OpenWindow(0,0,0,256,128,"", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(1,0,0,0,0,ImageID(img0))
ImageGadget(2,128,0,0,0,ImageID(img1))

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

Procedure.q RemoveBKG(x,y,sourcecolor,targetcolor)
  If sourcecolor=RGBA(255,0,255,255) & $FFFFFFFF
    ProcedureReturn RGBA(0,0,0,0)
  Else
    ProcedureReturn sourcecolor
  EndIf
EndProcedure

DataSection
  imagestart:
  IncludeBinary #PB_Compiler_Home + "\examples\sources\data\geebee2.bmp"
EndDataSection
This snippet uses a bmp as source image and removes the pink background in a callback.