[soved]help with watermark

Just starting out? Need help? Post your questions and find answers here.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

wow very very nice Trond
@Rook is not a paint program just resize,rotate,crop and some other small stuff (usefull if u have lot's of images taken with a digital camera and u want to send them over email yahoo etc)
Registered user of PureBasic
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: [soved]help with watermark

Post by Kaeru Gaman »

ah... like the handy tool by Irfan...
that was from Slovania or so, if I remember correctly...
oh... and have a nice day.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: [soved]help with watermark

Post by quasiperfect »

not that advanced Kaeru (i don't need all the features, actually is for my wife) but i will post my final version so people can improve or make another version
Registered user of PureBasic
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: [soved]help with watermark

Post by Michael Vogel »

Code: Select all


; Watermark 1.o by Michael Vogel

Procedure.l CreateWatermarkMask(image.i,text.s,angle.i,fontname.s,fontsize.i,x.i=0,y.i=0)

	Protected tx.i,ty.i
	Protected alpha.f
	Protected font.i

	If x<=0
		x=ImageWidth(image)
		y=ImageHeight(image)
	EndIf
	
	image=CreateImage(#PB_Any,x,y,32)

	If image

		font=LoadFont(#PB_Any,fontname,fontsize)
		StartDrawing(ImageOutput(Image))

		DrawingMode(#PB_2DDrawing_AlphaChannel)
		Box(0,0,x,y,0)

		DrawingFont(FontID(font))
		tx=TextWidth(text)>>1
		ty=TextHeight(text)>>1

		alpha=angle/180*#PI
		ty = y>>1 + Sin(alpha)*(tx)+Cos(alpha)*ty-ty
		tx = x>>1 - Cos(alpha)*tx
		
		;DrawingMode(#PB_2DDrawing_Default); don't touch anything here :)
		DrawRotatedText(tx,ty,text,angle);        don't add a color here :––––)

		StopDrawing()
		FreeFont(font)

	EndIf


	ProcedureReturn Image


EndProcedure
Procedure.l SetWatermarkColor(image.i,color.i)

	StartDrawing(ImageOutput(Image))

	DrawingMode(#PB_2DDrawing_Default)
	Box(0,0,ImageWidth(image),ImageHeight(image),color)

	StopDrawing()

EndProcedure
Procedure DoWatermark(image.i,watermark.i,color.i,level.i,alpha.i=128)

	Protected x.i,y.i
	Protected ox.i,oy.i

	SetWatermarkColor(watermark,color)

	ox=(ImageWidth(image)-ImageWidth(watermark))>>1
	oy=(ImageHeight(image)-ImageHeight(watermark))>>1
	
	StartDrawing(ImageOutput(image))

	DrawingMode(#PB_2DDrawing_AlphaBlend)

	For x=-level To level
		For y=-level To level
			DrawAlphaImage(ImageID(Watermark),ox+x,oy+y,alpha)
		Next y
	Next x

	StopDrawing()


EndProcedure


UsePNGImageDecoder()
LoadImage(1,"C:\Something.png")
ResizeImage(1,500,500)

Watermark=CreateWatermarkMask(1,"Hello PB!",30,"Trebuchet MS",48,300,250)
DoWatermark(1,Watermark,#Yellow,4,10)
DoWatermark(1,Watermark,#Red,1)
DoWatermark(1,Watermark,#White,0,255)


OpenWindow(0,0,0,500,500,"Watermarks",#PB_Window_ScreenCentered)
ImageGadget(0,0,0,0,0,ImageID(1))

Repeat
	Select WaitWindowEvent()
	Case #PB_Event_CloseWindow
		Break
	EndSelect
ForEver
Even everything seems to be said already, I just add another way to do such things -- but rotated text is not easy to handle, so I add also a puzzle as a bonus-code :wink: ...

How to get all 'O' of the text centered on just one point? And will there be a possibility to get it over the red colored 'O' as well? And with different font sizes? And different fonts? And...

Code: Select all

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
		Box(0, 0, 200, 200, RGB(255, 255, 255))

		text.s="X<<<  O  >>>X"

		tx=TextWidth(text)>>1
		ty=TextHeight(text)>>1

		DrawText(100-tx,100,text,#Red,#Yellow)
		Plot(100,100,#Red)
		Plot(100-tx,100,#Red)

		For Angle = 0 To 90 Step 10
			alpha.f=angle/180*#PI
			zx = 100 - Cos(alpha)*(tx)
			zy = 100 + Sin(Alpha)*(tx+ty*2.5)+Cos(alpha)*ty

			DrawRotatedText(zx,zy,text, Angle, RGB(0, 0, 0))
			
		Next Angle
		StopDrawing()
		ImageGadget(0, 0, 0, 200, 200, ImageID(0))
	EndIf

	Repeat
		Event = WaitWindowEvent()
	Until Event = #PB_Event_CloseWindow
EndIf
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: [soved]help with watermark

Post by Rook Zimbabwe »

Dang... I had started a paint program and just stalled as work got busy and my POS program took off... I was hoping you had the same idea! OK well I plan to finish one day... your posts are r4ealted to some problems I am having as well so keep up the good work! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: [soved]help with watermark

Post by quasiperfect »

@Rook if u post u'r code would be great i'm interested to add new features to the software i'm making
Registered user of PureBasic
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: [soved]help with watermark

Post by Keya »

great thread guys! thanks to all who've contributed. PB makes it so easy to manipulate images so i guess it's inevitable that sometimes we'll want to add watermarks etc to our image creations!? :)

My needs were a lot simpler - i just wanted to add a Header text, so my code is a significantly dumbed-down version of Michael Vogel's demo above heehee (i didn't need alpha transparency, angled or multi-colored text), so thanks Mike for sharing a great demo base to learn and build from!

Code: Select all

EnableExplicit
UsePNGImageDecoder():  UsePNGImageEncoder()

Procedure AddWatermark(ImgFile.s, SaveFile.s, txtHeader.s, fcolor.i,bgcolor.i, fontname.s,fontsize.i)   
   Define img,xwidth.i,yheight.i
   Define hfont=LoadFont(#PB_Any,fontname,fontsize)
   Define expectedbgcolor.i = $FFFFFF, indent = 1
   img = LoadImage(#PB_Any,ImgFile)
   StartDrawing(ImageOutput(img))
   DrawingMode(#PB_2DDrawing_Default)   
   DrawingFont(FontID(hFont))  
   Define txtheight = TextHeight(txtHeader), txtwidth = TextWidth(txtHeader)
   xwidth=ImageWidth(img): yheight=ImageHeight(img)
   DrawText((xwidth/2)-(txtwidth/2)-1,indent,txtHeader,fcolor,bgcolor) ;Top-center align
   StopDrawing()
   FreeFont(hfont)
   SaveImage(img,SaveFile,#PB_ImagePlugin_PNG,10,24)
EndProcedure

AddWatermark("C:\temp\test.png", "C:\temp\testOUT.png", "My header text", RGB(0,0,$FF), RGB($FF,$FF,$FF), "Arial",10)
Post Reply