Transparent picture

Just starting out? Need help? Post your questions and find answers here.
Marvi
User
User
Posts: 17
Joined: Fri Oct 03, 2008 6:02 pm
Location: Milano

Transparent picture

Post by Marvi »

Image

I would like to load two images, so that the second one is superimposed on the first one with a transparent mask: in the final picture the white pixels of the second picture
must be transparent.

Can anybody help me? Thanks.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Transparent picture

Post by walbus »

You must output with "DrawAlphaImage", not with "DrawImage"
User avatar
kenmo
Addict
Addict
Posts: 2046
Joined: Tue Dec 23, 2003 3:54 am

Re: Transparent picture

Post by kenmo »

Code: Select all

BackFile.s = "background.png"
ForeFile.s = "fore.png"

UsePNGImageDecoder()
UsePNGImageEncoder()

OutFile.s = GetTemporaryDirectory() + "out.png"

If LoadImage(0, BackFile) And LoadImage(1, ForeFile)
  
  ; If image 1 does not have an Alpha channel, convert white pixels to transparent
  If (ImageDepth(1) = 24) Or (#False)
    If CopyImage(1, 2) ; Temporary copy image
      If CreateImage(1, ImageWidth(2), ImageHeight(2), 32) ; Recreate 32-bit
        If StartDrawing(ImageOutput(1))
          DrawImage(ImageID(2), 0, 0)
          DrawingMode(#PB_2DDrawing_AllChannels)
          For y = 0 To ImageHeight(1)-1
            For x = 0 To ImageWidth(1)-1
              Color = Point(x,y) & $FFFFFF
              If (Color) = #White
                Plot(x, y, Color) ; Transparent (alpha = $00)
              Else
                Plot(x, y, Color | $FF000000) ; Opaque
              EndIf
            Next x
          Next y
          StopDrawing()
        EndIf
      EndIf
    EndIf
  EndIf
  
  ; Draw image 1 over image 0
  If StartDrawing(ImageOutput(0))
    DrawAlphaImage(ImageID(1), 0, 0)
    StopDrawing()
    If SaveImage(0, OutFile, #PB_ImagePlugin_PNG)
      RunProgram(OutFile) ; Launch it
    EndIf
  EndIf
  
EndIf
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Transparent picture

Post by TI-994A »

Marvi wrote:I would like to load two images, so that the second one is superimposed on the first one with a transparent mask...
If the overlaid image is a transparent PNG, the DrawAlphaImage() function would provide the easiest solution:

Code: Select all

UseJPEGImageDecoder()
UsePNGImageDecoder()

Enumeration
  #MainWindow
  #ImageDisplay
  #image1
  #image2
EndEnumeration

imageFile.s = OpenFileRequester("Select Background Image:", "", "JPG, BMP, PNG |*.jpg;*.bmp;*.png", 0)
If imageFile And LoadImage(#image1, imageFile)
  imageFile = ""
  imageFile = OpenFileRequester("Select Overlay Image:", "", "JPG, BMP, PNG |*.jpg;*.bmp;*.png", 0)
  If imageFile And LoadImage(#image2, imageFile)
    wFlags = #PB_Window_SizeGadget | #PB_Window_ScreenCentered
    wFlags = wFlags | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    OpenWindow(#MainWindow, #PB_Any, #PB_Any, 600, 400, "Draw Overlay Image", wFlags)
    ImageGadget(#ImageDisplay, 0, 0, 600, 400, 0)
    If StartDrawing(ImageOutput(#image1))
        DrawAlphaImage(ImageID(#image2), 10, 10)
      StopDrawing()
    EndIf
    SetGadgetState(#ImageDisplay, ImageID(#image1))
    While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend  
  EndIf  
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Marvi
User
User
Posts: 17
Joined: Fri Oct 03, 2008 6:02 pm
Location: Milano

Re: Transparent picture

Post by Marvi »

Thank you, all of you!
Post Reply