47 Video Views·Apr 22, 2025
This video covers everything you need to learn how to use the OpenCV (computer visuals) python module, also identified as cv2.
We start by covering using pip to install the module, followed by importing cv2. Then we show how to load in the image, create a window of both varied and fixed window size, and then display our image in the window.
Then we start transforming our photo, first to grayscale, then to a negative/inverted copy, then we blur it and invert it back, and finally use the divide tool to scale the photo giving it the final 'sketched' effect!
There are tons of useful tools inside of OpenCV so if you want to see more projects like this just let me know about it in the comments below! If you found this fun, useful or cool please leave a like on the video and subscribe to the channel!
Thank you so much for the support and good luck with your code!!
hello welcome back to the channel and
today's video will be showing how to
create an automatic image to sketch
conversion tool using python
[Music]
okay so this is gonna be a super cool
video and a really useful introduction
to the opencv or computer vision um
module inside of python if you're
unfamiliar with it so hopefully you like
this before i get too into it if you are
enjoying this video or found anything on
the channel useful please like the video
subscribe to the channel it helps me out
a ton and i roll out a ton of content
just like this constantly
so to get started you're going to import
this module called cv2 which it's the
second iteration through of a big
computer vision package that they've
installed but if you don't have this in
python already you may need to do
pip
install and then it's not just cv2 it's
actually open cv dash python
so if you don't have it installed
already i do but then you would go to
the terminal and you would do it just
like you can see here pip install opencv
dash python and then as long as you have
a modern version of python installed
that's going to take care of it for you
then you're good to go on the importing
of cv2
and then the first thing i'm going to do
is i'm going to create
two windows to display
original and transformed because
throughout this process we do a few
transformations as you'll see to our
original photo to get it to a sketch
another thing is i recommend dropping a
copy of whatever image while you're
working on this i've got me here
at the pittsburgh pirates stadium
with fake hands that's going to be my
image that i work with today if you want
go ahead and drop it into your project
directory that'll be really easy to
reference but you can reference images
anywhere
on your computer you'll just have to use
the full directory extension when you
start typing in
okay so we'll get to that in a little
bit but then um i said we're going to
create two windows to display the
original and the transformed so first
we're going to do cv2 dot named
window and it's kind of this standard
python way of doing lowercase but then
if you get two words
then you use a capital to separate the
words out that's pretty normal in python
modules
and we'll call this one original
and we're just going to give it cv2 dot
and then window normal so i'm not going
to go too much into the specifics of
opencv it's a huge module that can do a
ton of stuff so i'm going to focus on
our sketch conversion that we're doing
today
and
if you like this and you want to see
more broad open cv concepts just let me
know about it in the comments below and
we can take a look at doing some more
in-depth stuff in the future
so we're going to do two of these and
like i said um we're going to call one
original and the other transformed and
for now this is going to give us like a
nice resizable window as we'll see when
we get in in a little bit
but the next thing we'll do is load
image okay and so i'm going to create a
variable i'm just going to call it img
for image
and it's going to be cv2.imread
i'm read
excuse me and then
just in uh parentheses in quotations
type in the name of your image so mine
is
img09 and if you're using a good ide
like pycharm um it will probably auto
populate with them if they're in your
directory so that's nice you can go
ahead and grab that
and then the next thing we'll do is uh
we're just going to go ahead and do cv2
dot
i'm show
and then we'll say
what window to put it in and we made
that window called original
and we're going to have it show the
image and this is just so that we can
display the image and i can talk about a
few concepts with opencv while we're in
here
display the image
and the last thing we're going to do is
we're going to
wait
x
seconds before actually it's
milliseconds
milliseconds before automatically
closing so whenever you open something
with your computer uh there's this
option for it's called wait key
and it's gonna automatically close your
window after a certain number of
milliseconds unless you want to be
continuously open in which case you want
to put a zero in there oh i said deplay
let's display so because we don't want
this thing to close i'm just going to go
ahead and run this and you'll see
so it made that window for transformed
but we didn't put anything in it yet but
we have original you can see my photo
here and it's resizable which is kind of
funky
but that's great we're already just
getting the photo we picked popping up
and i'll just show you real quick what
happens if we put 1000 in here so that
should be one second from once it loads
it goes ahead and automatically closes
your windows so we're just going to
leave it as a zero so our stuff doesn't
close but that's what's the purpose of
that last line is okay
now let's go ahead and start
transforming this thing and every time
we do a new transformation i'll show it
in the transformed window i'll explain
what's happening
and in no time we're going to have a
full sketch conversion okay so the first
thing we'll do is convert
image to grayscale so if you think of
like a
normal sketched image in pencil it's all
gray so we'll start by just doing we'll
call it gray image
it's a good idea to put every
transformation of your image in a new
variable name that explains what's going
on there
you could just keep overwriting img the
whole time but then you're not going to
really be able to do an original versus
a transformed
so to get a great image it's just going
to be cv2 dot
and then convert color it's a nice
built-in function
you say the image you're converting and
then cv2
and then it has this built-in color
and then it's uh it's not rgb it's bgr
just to be fancy um and then two gray
and you can probably see i'm not gonna
get into too many of these right now but
there's there's all these different ways
you can do bgr2 rgb you can do bgr to
gray you can do um you can also do bgr
alpha which is basically a like
transparency limit so we don't have time
to get into all of those modules in this
video we're just focusing on
transforming a sketch but i definitely
could cover some of the other things you
can do with this conversion tool in the
future
so we're converting it to gray there and
then i'm gonna start doing this second
cv show now i'm gonna do i'm show and
then we'll put it in transformed
and this will do
gray image okay
so we should have two
side-by-side
transformable windows and you can see
this one's a little bit smaller
but that's okay
so i'll go ahead and pull that up you
can see all we did is make grayscale
they're identical other than that
hopefully you're not sick of this photo
because this can be popping up a lot
one thing i'll cover real quick because
you may be saying like well what if i
want to be a specific window you can do
that i'll go ahead and type it out here
so if you want that
if you want that code you can copy it
from here
you would use cv2 resize window and then
you would give the name of the window
that you're resizing and then you would
give the x and y dimensions so let's say
we're doing
800 and 600 this doesn't need to be
copied out yet
i'll go ahead and run that and you'll
see the original is still resizable but
it opens up at that set size so if i
were to go
let's say 400 this might look kind of
bad
yeah so it looks real squashed um
but you can see like that's how you
would set a default so if we did want
800 600 actually looks pretty good
if we just want it when we open them up
in the beginning to be the same size and
to be that size that's not a bad way of
going about it make sure you spell
everything right so we'll go ahead and
leave it there
and let's get back to it so grayscale is
just the first step okay after it's gray
scale the next thing we want to do is we
want to invert
the grayscale
image
okay so what we do there it's pretty
easy we're just going to do inverted
gray
and then image underscore
talk and type all that and explain
inverted gray image and this one's
actually really easy
all you do is you take 255 which if
you're familiar with rgb is the maximum
value and you're gonna do minus gray
image and you're creating you've heard
the term like a negative of a photo in
film that was always a thing we're
creating a negative of the gray image
okay and you'll see what's going on here
if i go ahead and do inverted inert
inverted gray image all right so let's
go ahead and display that one
and you can see it's it's gray scale but
then it's also flipped
so that's good that's the next step
towards drawing a sketch
um
the next thing after you invert it is
going to be blur it so blur gray
image and this is kind of the uh the one
that really makes it look like a sketch
so we'll call it blur
inverted gray image and you're welcome
to use a shorter variable name i'm using
a longer one because i want it to be
real clear what's happening on here
there's also a couple different types of
blur you could do but we're going to use
gaussian blur
again i don't really have time to do to
do a full explanation no one would watch
this video if it was like 40 minutes
long
but gaussian blur is just going to blur
all of the edges of your image you're
not going to have sharp edges anymore
and then you say what image it is that
you're blurring and then this next one
is kind of like
it's called case size but basically
think of it as like how aggressive do
you want the blur to be
and i'll show it at a few different um
a few different levels so you can see
what's going on here but let's go ahead
and add blur onto this so we'll say blur
inverted gray image and this 121 121 is
kind of a
medium level of blur so you can see it's
negative it's gray scale and it's
blurred let's go ahead and look at it
with like almost no blur on it so we'll
do like 11 11.
and you can see it's a pretty clear
image let's go basically the far
opposite direction so like we'll do 211
211
and you see it's really fuzzy um okay so
we'll go back to we'll just do
111 111 for now you don't have to use my
values this is something you can play
around with it'll affect like how
aggressively
your blurred image gets converted but
then actually the next thing that we do
is invert the blurred
image back so we're changing it back to
standard grayscale after it's been
blurred that's what gives it that like
sketched effect
it's actually really cool how it works
and all we have to do to get it back is
255 minus blur inverted gray image and
we will call this one
just inverted blur because we're getting
real long on variable names now
so
we'll go with inverted blur
and we're going to display that in the
transform
blurred
and let's take a look at it
so there you go we're back to standard
grayscale we're blurred and inverted and
now the last piece is we just actually
turn it into a sketch by doing this
divide
the
gray image
by the blurred image
and scale okay
and that'll make a little bit more sense
once i type it in here but you do so
this is actually going to be the sketch
now the sketch transform and you do
cv2.divide
okay so and then you give two values for
division
and what we're going to do is we're
going to do the original gray image
and we're going to divide it by the
inverted blur so basically we took the
first grayscale we flipped it and
blurred it and then flipped it back and
we're comparing the two and that's
what's actually creating the sketch i'm
not an artist i'm not gonna explain like
uh how that applies to how you would
actually do a sketch in real life
but you divide one by the other and then
scale it to 256 that's saying like your
scale is a full rgb range
and then we're gonna go ahead and show
you what's going on now so
instead of inverted blur let's show
sketch
oops
all right
so there you go i mean that's pretty
sweet uh not a ton of lines of code
definitely a need to kind of know how
opencv works and some of those specific
tools but
look at that you have like it looks like
a hand-drawn pencil sketch of a photo
and all we did is a few quick transforms
now i'll show you what how this value we
talked about earlier how it affects your
end result and that may be what you want
to play around with the most you can see
when i go with a
thicker line it kind of creates this
like more stark contrast you have
thicker lines around the edges you lose
a little bit of definition but you get
some thicker darker lines
now if i were to go the far opposite way
like let's go down to like 31
okay so now we're going to be pretty
light
you can see you have really fine detail
or maybe you can't see you have really
fine detail but you have nothing bold
like nothing popping out at you
so whatever image you're doing might
affect this if you're king like a photo
editing app this might be something
where you allow that variable to be on a
slider
so while it's up you can slide it around
and you can actually see it shift
but for now this is pretty sweet we just
converted any basic photo to a sketch
now let's say that you want that photo
to actually be saved somewhere and you
don't want to have to like open your
snip tool take a screenshot crop it down
well that's super easy so if you want to
save your sketch
save your sketch
okay
not the best speller all you have to do
you remember when we opened this image
up top we did
uh cv i'm read
now it's cv i'm right
okay
and just give it a name so we'll call it
my
sketch dot
png
and then tell it what image to save so
we're gonna save the sketch and this way
when we run this it won't just show us
it it'll also save a copy and we'll see
it over in our library here
okay so we've got this transformed image
it looks pretty cool and then if i close
these
and i allow this guy to refresh there we
go it pops in my sketch png and now this
is like an actual image you can a little
closer to my face than anyone should
zoom
but you can zoom in you can see this
like it's a really good tool you got to
get right down to the pixels before you
start noticing any kind of issue
and so hopefully you found that cool
it's 27 lines of code and that's only
because i added a lot of comments which
you should always do but i hope you
found this cool if you did please like
the video subscribe to the channel it
helps me out a ton and let me know in
the comments what you want to see next
if you want to see more of this kind of
video more games a different software
just let me know what you think would be
good for this channel because i am just
getting started out and i love
suggestions so i really appreciate you
watching i really appreciate your
feedback and
thanks for watching good luck with your
code and
thanks bye