Add watermark to images with Opencv and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome what we do today is we add a watermark to a picture or to an image and this will look like this and we will use python and opencv to do that okay so let's get started at first we want to create a python environment and we use conda for that and we will call it watermark and we will use python 3.8 and then it will start installing the packages and modules this just takes a little while okay and now we can see conda and list that our new environment was created let me change the properties so maybe get a little bigger font yeah this is better okay now here you can see watermark okay now next we create a new python project and what we use is pycharm and we click here new project we take a pure python project and we will give it the name watermark and we will use the um python interpreter we just created let's see if we can find it here pycharm is smart enough uh we got no not smart enough i have to look for it myself okay uh users toby anaconda environments watermark and here we go and then we can click on create and our new project just got created and what we can do first is we can check what libraries are in the project so we go to settings project watermark python interpreter and here we see that watermark is selected and here we have python3.8 that's the one we installed now let's continue with just running making a test run of the script okay seems to work so that's all we need for this so far and what we want to do next is we need to install numpy and opencv so we make pip install numpy this takes a while and the next thing is we install opencv [Music] python okay that looks pretty good now after installing numpy and opencv we can check here in python yes it is installed that's good and now we want to import the modules and what we also want to do is we want to create a folder for our images where we want to put the watermark in so let's create the directory images and let's copy two images inside of it and here we have logo three that's gonna be our watermark and this will be the picture where we put the watermark in now the first thing we want to do is want to read our watermark and we use cv for that and we want to print out the watermark or we want to make it visible to the user so let's go to images logo and then while true um is cv image show [Music] watermark and and then we also have to [Music] make a pause here until the user presses q because otherwise the image would be gone pretty pretty soon so let's start the software or the code we just programmed and what we see is a window opens with our logo now we don't want the logo this big in our software so let's write let's write a function so we can scale the logo def scale and we pass the function image and escape width and what we do we read the height and the width of the image and then shape and we read only the first two we don't read the third one we're not interested in that the interested in the in the layers so far and we calculate and you hate and this is in scale width divided by image width times image and we use the resize function from cv and we pass the new the new width and then you hate okay this should work now what we can do is we can scale right after reading it to let's say let's make it 400 pixels and let's start the software again and here we go now this looks this size looks better for watermark okay what we want to do next now we want to read here the watermark hey and the watermark from from the image we just loaded so and then we load the image itself where we want to put our watermark inside so we use again cv image read and enter the path to the image so it's toby jpeg and dot jpg sorry for speaking drone uh image image paint image equals image shape dot shape and [Music] now what we have to do is we have to add an alpha channel to this image because it has so far only three layers uh we there's also a function from c to add this so image and we wanna make um color [Music] um blue green red to blue green red alpha okay here we go now what we can do is we can also show our image and then let's start the software ah here we go now we got oh this is also way too big let me scale that one second uh look at scale let's say let's make 1200 okay okay now we get this image and we got our watermark and we want to put it here [Music] that's the gold okay so what we do next now we create an overlay [Music] where we put the watermark in and overlay has only zeros and has the same height and width as as the image and it has four channels and um let me check overlays zeros what is wrong here and d type equals okay here we go and now what we want to do is we want to like make it simple at the beginning we want to put the watermark on the top left so we do zero watermelon hey and zero um watermark width equals watermark and now let's let's print this overlay also let's see what we got here what we just created boom um [Music] kind of broadcast okay we don't have four layers into shape the problem is here we have three layers and here we have four layers okay i found a problem so it was watermark had only three layers as it was telling us and the overlay had four layers and you cannot put that together and the reason why watermark had only three layers was because here we have to um say that we want to read the image unchanged because otherwise it will remove the alpha layer and now after we did that now what we can do let's make this a little bit smaller here also [Music] start the software then we see now here we have our overlay and here we have the image and what we want to do is we want to put this overlay above this image and then we should be able to see the watermark inside the image okay so um and what well so we have it here so what we can do is we can combine two images with add weighted and then first we say the base image or the image we want to put on top of it is the overlay then we add an alpha channel or the value of alpha i can play around with that a little bit after we're done with this so one is um showing it really good and the lower it gets the less you can see the image now the next parameter is the image you want to put the overlay on top of and then also 1.0 and the gamma is not so important and we want to write this to image okay now let's see what we got if we start this software uh here you can see okay we have the watermark inside here now because of the colors yeah they all look really good um because it's right here and the logo is really bright so not perfect now what we want to do is we want to put the image from the top left to the bottom right now let's start this again real quick so i can tell so i can tell you now here is [Music] here zero zero and here is um image down here is um coordinate image height zero and here is image width zero and here is image hate image with image width that's that's the coordinates that's how they work and what we can do is we put the logo to the right and all the way to the bottom but let's make it to the right first so we don't have too much change at once now what we do is here we say we don't make all the way to the left instead we start image head image width minus watermark width and we end at image width and then if we started you can see the the low is on the right and we do the same with the height now so image a minus watermark height and image okay bottom right and you can place the image pretty much wherever you want or the watermark but i like this this looks pretty good and the next step we can do is as i mentioned we can play around a little bit with this um this here now yeah let's create let's create some [Music] output one equals copy and output 2 equals image copy and output 3 with image copy now what we do is we print output one output two and output three and each one we give a different alpha channel well alpha value now output 1 will be let's say 0.1 [Music] and zero point five will be output two and zero point or one point zero will be output three let's start the software and we can see what we got um okay that was not a good idea let's let's give this some good titles output one output two and output three here we go now this is output 3 with 1.0 this is output output 2 with 1.5 0.5 now you can see this is almost the same but a little less and here we have 0.1 this this is hot really hard to see and this way you can change how visible you want your watermark to be now this is pretty much all there is now what you can do in advance is you can save these images after you're done um yeah but i'm not doing that in this tutorial now in the next tutorial what we will do is we will add the watermark to a video that will be interesting because the difficult part there was also to add or to keep the sound after the video was saved now that that will be definitely interesting so that's it for today thanks for joining
Info
Channel: LumaBIT
Views: 986
Rating: undefined out of 5
Keywords: watermark, add watermark to photo, add watermark to youtube video, python opencv, opencv python tutorial, opencv python, graphic design, easy tutorial, how to watermark photos, how to create watermark, watermark video, how to watermark video, add watermark, opencv, python, image blending, multiple images, opencv watermark, coding, opencv tutorial, opencv tutorial for beginners, python tutorial, computer vision tutorial, online course
Id: OHJaAUdj-GA
Channel Id: undefined
Length: 20min 8sec (1208 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.