Page 1 of 2
[soved]help with watermark
Posted: Wed Feb 03, 2010 1:36 pm
by quasiperfect
hi
i want to watermark some pictures and i'm almost there but i don't understand the drawing modes
please see image for what i have done and what i need to do
Code: Select all
Procedure DEG2RAD (x)
ProcedureReturn (x / 180 * #PI)
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseJPEGImageEncoder()
Global fontsize.l = 20
Global fontangle = 30
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"
Enumeration
#MyImage
EndEnumeration
If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
If image_path
If LoadImage(#MyImage,image_path) <> 0
ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
LoadFont(0, "Arial",fontsize)
StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
twidth = TextWidth(mytext)
theight = TextHeight(mytext)
xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
DrawingMode(#PB_2DDrawing_Default )
DrawRotatedText(xpos,ypos, mytext,fontangle,RGB(255,0,0))
StopDrawing()
ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
Else
MessageRequester("Error", "Error loading image", 0)
EndIf
Else
MessageRequester("Error", "No image selected", 0)
EndIf
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: help with watermark
Posted: Wed Feb 03, 2010 1:42 pm
by Trond
Code: Select all
Procedure DEG2RAD (x)
ProcedureReturn (x / 180 * #PI)
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseJPEGImageEncoder()
Global fontsize.l = 30
Global fontname.s = "impact"
Global fontangle = 35
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"
Global Alpha = 128
Enumeration
#MyImage
EndEnumeration
If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
If image_path
If LoadImage(#MyImage,image_path) <> 0
ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
LoadFont(0, fontname,fontsize)
StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
twidth = TextWidth(mytext)
theight = TextHeight(mytext)
xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0, Alpha))
StopDrawing()
ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
Else
MessageRequester("Error", "Error loading image", 0)
EndIf
Else
MessageRequester("Error", "No image selected", 0)
EndIf
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: help with watermark
Posted: Wed Feb 03, 2010 1:45 pm
by srod
I am not sure what effect you are after, but try switching the relevant two lines for :
Code: Select all
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0,255))
**EDIT : too slow, beaten by Speedy-Gon-Trond!

Re: help with watermark
Posted: Wed Feb 03, 2010 3:10 pm
by quasiperfect
thanks to both
Trond code is pretty close it got the transparency effect but not the outline
i need the text to have a white outline of 1 or 2 pixels
Re: help with watermark
Posted: Wed Feb 03, 2010 3:32 pm
by srod
For that you will need to create a GDI path, but I am pretty sure that this will not be compatible with PB's image library in that GDI will ignore the alpha channel and your path will thus effectively retain the original per-pixel alpha settings after you have filled the text.
One way around this would be to copy your image (without the text) to a 24-bit bitmap (use DrawAlphaImage() onto a suitable background) and then to use GDI to create a path etc. A bit fiddly, but very possible.
Re: help with watermark
Posted: Wed Feb 03, 2010 4:03 pm
by srod
A cheat is to draw the text several times, in two different colors, offsetting the text slightly each time...
Code: Select all
Procedure DEG2RAD (x)
ProcedureReturn (x / 180 * #PI)
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseJPEGImageEncoder()
Global fontsize.l = 30
Global fontangle = 30
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"
Enumeration
#MyImage
EndEnumeration
If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
If image_path
If LoadImage(#MyImage,image_path) <> 0
ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
LoadFont(0, "Arial",fontsize)
hdc = StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
twidth = TextWidth(mytext)
theight = TextHeight(mytext)
xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos-3,ypos, mytext,fontangle,RGBA(255,255,255,255))
DrawRotatedText(xpos+3,ypos, mytext,fontangle,RGBA(255,255,255,255))
DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0,128))
StopDrawing()
ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
Else
MessageRequester("Error", "Error loading image", 0)
EndIf
Else
MessageRequester("Error", "No image selected", 0)
EndIf
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
If that's not quite what you're after then you will probably need to look into the GDI paths.
Re: help with watermark
Posted: Wed Feb 03, 2010 4:10 pm
by quasiperfect
not bad srod but is to obtrusive
i played a bit with the code and made the text autofit (very ugly code but i don't have any better ideea) based on the image size and used the text another time to give a nice effect
maybe someone will like it and use it
Code: Select all
Procedure DEG2RAD (x)
ProcedureReturn (x / 180 * #PI)
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseJPEGImageEncoder()
Global fontsize.l = 30
Global fontname.s = "arial"
Global fontangle = 35
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"
Global Alpha = 50
Enumeration
#MyImage
EndEnumeration
If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
If image_path
If LoadImage(#MyImage,image_path) <> 0
ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
LoadFont(0, fontname,fontsize,#PB_Font_Bold )
StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
twidth = TextWidth(mytext)
theight = TextHeight(mytext)
StopDrawing()
While (twidth < (ImageWidth(#MyImage)-50))
fontsize = fontsize * 1.1
LoadFont(0, fontname,fontsize,#PB_Font_Bold )
StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
twidth = TextWidth(mytext)
theight = TextHeight(mytext)
StopDrawing()
Wend
xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
LoadFont(0, fontname,fontsize,#PB_Font_Bold )
StartDrawing(ImageOutput(#MyImage))
DrawingFont(FontID(0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0, Alpha))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos,ypos-2, mytext,fontangle,RGBA(255,255,255, Alpha+50))
StopDrawing()
ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
Else
MessageRequester("Error", "Error loading image", 0)
EndIf
Else
MessageRequester("Error", "No image selected", 0)
EndIf
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: help with watermark
Posted: Wed Feb 03, 2010 4:15 pm
by srod
Not sure what you mean by autofit exactly?
It looks good when I use a 24-bit jpeg, not so good on a png with full alpha-channel. The text is almost transparent here. Still, altering the alpha value does make a difference.

Re: help with watermark
Posted: Wed Feb 03, 2010 4:27 pm
by quasiperfect
i edited the code a bit it wasn't the right version
i meant it writes the text (at max size possible) on middle of the image (any image size)
but after some testing my formula sucks (dosen't work)
i wanted the text to go from bottom left corner to top right corner
Re: help with watermark
Posted: Wed Feb 03, 2010 5:32 pm
by Trond
Here's an outline (it's a mess and it's not exactly fast, but it works - at least when cleartype is turned off...)
Code: Select all
h = 500
w = 500
CreateImage(0, h, w, 24)
LoadFont(0, "Impact", 40)
text.s = "In my pants!"
tcol = #Red
ocol = #White ; can't be the same as tcol!
owidth = 2
StartDrawing(ImageOutput(0))
DrawingFont(FontID(0))
FrontColor(tcol)
DrawRotatedText(100, 350, text, 45)
For y = owidth To h-owidth-1
For x = owidth To w-owidth-1
c1 = Point(x, y)
If c1 <> tcol
For y2 = -owidth To owidth
For x2 = -owidth To owidth
If Point(x+x2, y+y2) = tcol
Plot(x, y, ocol)
Break 2
EndIf
Next
Next
EndIf
Next
Next
StopDrawing()
OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(0))
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Re: help with watermark
Posted: Wed Feb 03, 2010 6:51 pm
by quasiperfect
nice but can be integrated with my code ? because i tried and no luck or i'm missing something
Re: help with watermark
Posted: Wed Feb 03, 2010 7:34 pm
by Kaeru Gaman
is it necessary to chose a purebasic solution?
with inkscape, it would be a three-click-action to add a watermark...
Re: help with watermark
Posted: Wed Feb 03, 2010 7:46 pm
by quasiperfect
yap the watermark is part of a bigger application
Re: help with watermark
Posted: Wed Feb 03, 2010 8:53 pm
by Trond
Code: Select all
UsePNGImageDecoder()
LoadImage(1, "c:\out.png")
h = 400
w = 400
CreateImage(0, h, w, 32)
LoadFont(0, "Impact", 40)
text.s = "Bottom secret"
alpha = 128
tcol = RGBA(255, 0, 0, alpha)
ocol = RGBA(255, 255, 255, alpha) ; can't be the same as tcol!
owidth = 2
tx = 50
ty = 250
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0, 0, w, h, RGBA(255, 0, 0, 0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawingFont(FontID(0))
FrontColor(tcol)
DrawRotatedText(tx, ty, text, 45)
For y = owidth To h-owidth-1
For x = owidth To w-owidth-1
c1 = Point(x, y)
If c1 <> tcol
For y2 = -owidth To owidth
For x2 = -owidth To owidth
If Point(x+x2, y+y2) = tcol
Plot(x, y, ocol)
Break 2
EndIf
Next
Next
EndIf
Next
Next
StopDrawing()
StartDrawing(ImageOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(0), 0, 0)
StopDrawing()
OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(1))
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Edit: Now it's starting to look cool. You should change Outline() so that it takes an x and y value as well, and then you just pass TextWidth(TEXT) and TextHeight(text) for w and h, so that it processes only the part of the image where the text was drawn. Then it will be faster. (You don't have to use the double outline, just delete the second call to Outline().)
Code: Select all
Procedure Outline(tcol, ocol, owidth, w, h)
For y = owidth To h-owidth-1
For x = owidth To w-owidth-1
c1 = Point(x, y)
If c1 <> tcol
For y2 = -owidth To owidth
For x2 = -owidth To owidth
If Point(x+x2, y+y2) = tcol
Plot(x, y, ocol)
Break 2
EndIf
Next
Next
EndIf
Next
Next
EndProcedure
UsePNGImageDecoder()
LoadImage(1, "c:\out.png")
h = 400
w = 400
CreateImage(0, h, w, 32)
LoadFont(0, "Impact", 40)
text.s = "Ministry of silly walks"
alpha = 255
tcol = RGBA(255, 0, 0, alpha)
ocol = RGBA(255, 1, 0, alpha) ; can't be the same as tcol!
ocol2 = RGBA(255, 255, 255, alpha) ; can't be the same as ocol!
owidth = 1
tx = 10
ty = 350
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0, 0, w, h, RGBA(255, 0, 0, 0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawingFont(FontID(0))
FrontColor(tcol)
DrawRotatedText(tx, ty, text, 45)
Outline(tcol, ocol, 2, w, h)
Outline(ocol, ocol2, owidth, w, h)
Outline(ocol2, ocol, owidth, w, h)
StopDrawing()
StartDrawing(ImageOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(0), 0, 0)
StopDrawing()
OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(1))
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Re: help with watermark
Posted: Thu Feb 04, 2010 12:21 am
by Rook Zimbabwe
When this paint program is bbeta I want to test it!
