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