Fun Python Exercise - Convert Numbers to Images with NumPy, OpenCV and Pillow
HTML-код
- Опубликовано: 2 июн 2024
- In this exercise we will use our NumPy skills to convert numbers into images 💪💪💪 We will see examples of 2 very useful Python image libraries: OpenCV (cv2) and Pillow (PIL). And also discuss the differences between them.
The purpose of this tutorial is to guide you step by step from creating the most simple greyscale image - all the way until drawing a complex structure of RGB windows! or even a smooth turquoise gradient image! 🤩
⭐ Clone Complete Tutorial Code ⭐
app.wayscript.com/lairs/6dac2...
--------------------------------------------------------------
🛑 If this is the first time you are working with NumPy,
I recommend watching the beginning of my Ultimate Guide to NumPy Arrays tutorial:
• Ultimate Guide to NumP...
🛑 If this is the first time you are working with RGB/BGR/Greyscale,
I recommend watching my Image into Matrix Tutorial:
• Convert Image into Mat...
⌚ TIME STAMPS ⌚
---------------------------------------
00:00 - intro
00:34 - import NumPy and OpenCv
00:58 - create empty array
01:41 - save array as PNG image
02:30 - grayscale image - 50 Shades of Grey
03:26 - region of interest
04:27 - floor division
06:21 - smooth greyscale gradient image
---------------------------------------
06:43 - convert grayscale image to BGR
08:02 - red gradient BGR image
09:01 - turquoise/yellow gradient BGR images
---------------------------------------
09:35 - artistic RGB image with Pillow
11:29 - fill NumPy image background
11:40 - draw square with NumPy
13:15 - working with relative coordinates
15:17 - draw window pattern with NumPy
18:49 - apply random color
20:57 - thanks for watching!
👨👩👧 RELATED VIDEOS OF MINE 👨👩👧
---------------------------------------
⭐ Image into Matrix:
• Convert Image into Mat...
⭐ Draw with OpenCV:
• Draw with OpenCV - No ...
⭐ Ultimate Guide to NumPy Arrays:
• Ultimate Guide to NumP...
⭐ Ultimate Guide to NumPy Operations:
• NumPy Operations - Ult...
🐦 SOCIAL MEDIA 🐦
---------------------------------------
Share your NumPy Images with Me! 🤩🤩🤩
⭐ LinkedIn:
/ mariyasha888
⭐ Twitter:
/ mariyasha888
* CREDITS *
---------------------------------------
Icons and Graphics by: flaticon.com - Наука
I like to think one of the contributors to OpenCV reversed RGB as a joke, then it escalated past the point of no return.
Cool video!
It gave me an idea to see what happens if I use data unrelated to colours to generate a picture...
if i know my data is in the range 0-5000, then I could calculate the value between 0-5000 as a percentage of 5000, then apply that percentage to 0-255, then see what monstrosity it generates as an image... kind of a heat-map but with no purpose except to make coloured squares... and probably learn more about OpenCV and Python in general...!
I think Mondrian would be horrified at the result... !
hahahaha it's funny you mention Mondrian!
I've replicated one of his drawings as a school project a few years back 😉
github.com/MariyaSha/MondrianDrawing
(Apparently before I discovered that READMEs on Github can be fancy 😅)
I think it's a really cool idea, I'm curious to see how it will turn out! Especially if the data reflects sound!
My guess - it's either gonna be a messy mosaic or a very organized geometric structure 🤔
If you go that direction, I highly recommend combining NumPy with Pytorch as it has some very convenient normalization methods. I discuss it briefly in this tutorial around the 13:21 😊:
⭐ Inference with Torch TensorRT:
ruclips.net/video/iFADsRDJhDM/видео.html
@@PythonSimplified Mondrian in JS, Nice!! 😀Thanks very much for the tips, I will try to create something that won't send me blind!
IIRC, Intel originally contracted out the development of OpenCV to some Russians. (This was after the breakup of the Soviet Union and you could hire some serious brainpower there cheap.) I'd guess they used BGR instead of RGB and no one here thought about specifying it.
I now understand why students learn better when there teacher is a beautiful and fun person. I have no idea learning python could be this awesome. I appreciate your good works and the effort. Cheers
Thank you so much for the incredible comment, Hamza!!! 😁😁😁
I see Python as awesome by definition, the teacher is just a bonus! 😉 hahahaha
these tutorials and explanations are amazing, I kinda learned all the basics of Python and didn't know what to do next, but your channel helps a lot
I was about to write you another message that where are you !!
Thank God you showed up 😀😀😊
hahahahaha perfect timing then!!! 😉
If I ever disappear under shady circumstances - I can count on you it won't go unnoticed! 😁😁😁
What a woman, what a voice!!! I'm excited by hearing the sound vibrations..., sometimes even forgetting that I came here to learn!)
I enjoy these and am learning so much, thanks.
I gotta try this out!
PyCharm (which the IDE that I use) wouldn't install the OpenCV and PIL the way Numpy installed. I had to look it up. PIL wouldn't install at all. So, with the alternate route, I had to install Pillow. In the end, I got everything working.
I may have to rewatch this particular video over and over again. I understand it as I'm following along with you. However, I'm not sure that, if I go back later to look at the code, I will understand it then.
Also, I have to say that it's refreshing to watch you, because you don't beg for money or give us a website where we have to give money to see more. Don't get me wrong, if I had money I'd probably tip, donate, pay you (however you want to look at it). What I'm getting at is that you don't throw distractions in the mix. You act as if you have a job ("mission" would probably be a better word) to do and you do it without letting anything else get in the way. So, thank you for putting out great videos.
Really nice video. Learned something new !! Thanks !
Blonde getting smarter is definitely a symbol of Endgame....
Luv ur content 💜
Fantastic. Thank you for simplifying a complex topic.
I loved, you're the best! Mariya!!!
Your chAnnel is actually a gem
I did the same kind of thing in pygame now. There's a pygame function that gives you the address of the pixels of a surface, I used ctypes and a function written in C and imported it to pygame to alter the pixels manually very quickly ;)
Thanks M❤ .. as usual ......
Perfect 🤩 thanks
You're absolutely welcome, enjoy! 😀
It's very intrestlng, thank You! ))
Excellent. thanks.
congratulations excellent content, greetings from Colombia
Mariya, You are amazing.
I enjoy watching you coding! Good job lady :)
Thank you so much! 😃
You are the best
i'm in love with your voice
Awesome !!! 👍👍👍🏅🏅🏅🏆🏆🏆
You can try mine too. Python and R playlists, with downloadable source files.
So much wonderful
Oh my, this is a new video. Thank you, I love them.
Thank you so much Jorge, super happy to hear! 😀😀😀
Nice job
Hi, can you show us how to turn music into pictures? I would want to take a song, extract different frequencies from it and then turn them into a picture, where 1000x1000 image will show the song as a picture. It goes as follows: Algorithm for computing semitones: starting with base note, which you will set as variable C0 set to 16.35Hz and from there you will compute the whole spectrum as a list, where first member is C0 and member i is C0*2^(i/12) Don't forget i starts with 1, not 0). You will generate list members up to maximum frequency, which is about 22 000Hz. Then you will divide song length into 1000 discrete steps. For every song part you will compute its average frequency and make data pairs of song step n which will have semitone m assigned as a closest tone to average frequency. (Let's say average frequency of step one will be 120Hz and the closest frequency in semi tone list will be member 35 at 123.47 Hz. Frequency step 1 will then have semi tone 35 paired with it) Once every step has semitone assigned, you can draw the image. Let's say you have around 150 semi tones, semi tones with same modulo 12 result will have the same color. (Let's say semi tone 0, 12, 24 will have this color and 1, 13, 25 will have other color.) You will now need 12 colors, you will get them by dividing visible spectrum of light into 12 equal steps, where 1 will be true Red and 12 true Violet. Time to draw now. steps will be drawn in columns from left to right. Step 1 will be a 1x1000px column of color Red if it has order of semi tone in list at place 0 or 12 or 24 etc... Let's say step 2 has semi tone order of 5, 17, 29 etc.. which is (N mod 12 = 5) and so it is drawn as a 1x1000px column of fifth color in light spectrum. You will repeat until all 1000 steps are drawn. The result is an image, where time flows from left to right and every tone of scale is represented in different color (C0, C1, C2, C3 are Red, while B0, B1, B2.. are Violet and so on). I know it is a lot, but I would be glad, Thank you :D
Hello ..
Thank you for your amazing tutorial. Please note my following observation.
1) if we set n_shades = 255, it gives a thick border on extreme right of the image.
Is there anyway to avoid that?
Thanks
I never thought I would be learning python and getting in love at the same time, isn’t she just beautiful? 😍☺️🙂
The Hi Mariya, thanks so much for your excellent tutorials. Your passion and enthusiasm for teaching Python are contagious..l have been binging on your python tutorials for the past 2 days, and I’m beginning to like Python. How can I donate to your channel?
Hi Jamal! Thank you so much for the incredible feedback! 😀😀😀
The best contribution - is to share your favorite tutorials of mine with other folks! Anything beyond is a wonderful, highly appreciated (but very optional) bonus! 😊
I don't have a Patrion account or things of that sort, but folks are usually sending me super chats / super stickers when there's a live stream. Everyone leaves a comment with their donation, then I pop it on the screen and then we start a discussion around it with all the viewers 😁 Try tunning in the next time I go live, would love to see you there! (regardless of any donations 😉)
Ok, I’ll spread the word about your channel. I can’t wait to see your next live stream! Thanks again
Thank you very much! You make python joy and inreresting. Additional thanks for your diction, I don'k skilled in English but I understand each your word! I use np.random.randint(0,260) for colors and building become more funny. Also it is possible constantly changing windows and colours, but for this I should use PyQt or Tkinter... With png it is impossible)))
Hi, can you create a video about setting up and using Tensor Flow? Maybe training some basic Neural Network using datasets provided by Google. Preferably made with Google Colaboratory. Thank you.
thanks❤
wow amazing rig..if I learn python and start to make $, will I be able to buy rig and monitor like yours?
Thx
Hi Maria am Omar and i really love your tutorials but i will really appreciate it if you can cover the topic of inserting images into sqlight3
database and retrieving it to like updating image source if posable.
me first 👍👍👍!
Dear author, It would be great to see video of parsing XML
I can't get the color spread. Also PIL doesn't work
Hi, nice video
Could you answer me something, where do you live, I'm saying state or city? I ask because I could understand easily what you say, but there some guys on RUclips I couldn't understand.
Thank you so much. =]
I taked that, I'll chat after, thank you =P
YOU ARE BEAUTIFUL THINK YOU FOR YOUR TUTORIAL...
Thank you so much, dear! 😊
🤗😍
Does anyone know of any software that handles saving a loading of matrice data plus saves the matrix layout as meta data, so an 8x2 saved matrix when reloaded appears as an 8x2 again. I've been looking at EA's IFF schema, but there might be something newer. C or C++.
Why not Numpy?
@@gfarkitox I see NumPy has an import feature, but I don't yet see an export; or even a load/save.
@@BobBeatski71 numpy.load and numpy.save
@@gfarkitox 👍 many thanks for the pointer. This was just what I was looking for.
Math😭 attending your video during exam📚 🙂❤️
Good luck with the exams, Beluga! 😀
You're almost done! 2 more weeks to study hard and then you're free to play video games again! 🤪
@@PythonSimplified just one more week 🙂✌️
🥰🥰🥰🥰🥰
python everywhere
I'm pretty sure it's not open cv that uses BGR, it's Windows that does that.
Hey, sound walking left to right, right to left🤭🤭...like hearing 8d audio*
hahahaha it's both the blessing and curse of a 360 degree microphone 😅😅😅
I wanted to say np.full again but i remember i already said it in last video 🙂, u prefer to start from blank then fill it, that's also ok
Python simplified can you tell me that you did a giveaway of an iphone recently?🥺
Now create 100 images and sell as nfts 😂🎉
Noway 🤣🤣
Hello
Hello Hello :)
First view
Absolute goddess 🥵🥵
lmao just searched how to convert png to base64 for pysimplegui 2 seconds ago ...
People like you get the video maker in trouble. The company that owns soft soft, Image Line, are very aggressive with piracy. They read
美女你好,我是业余爱好者
I am glad i saw my name in your video, also my kind of topic, i'm loving it.
I prefer pillow, i hate cv2 has BGR, but pillow has some other limitations sometimes, comenting on half a video now, but if u didn't know there is this img = cv2.cvtColor(img,cv2.COLOR_BGRA2RGBA)
Thanks for the tip and for the lovely comment Sinisa! 😁
I must admit that those colour space conversions is where I usually lose OpenCV! 😅 hahaha
It suddenly becomes so bulky with the uppercase letters and the long commands so I skip it altogether and go for Pillow instead... even though Pillow is not implemented with C++ so it's probably not the best solution 🙃
@@PythonSimplified Pillow is quite fine for most cases, and i have to say i am very impressed how u handle matrices, i really enjoyed this video, i had to use cv when i tryed saving png in 16 bits per channel, for shades of color np.linspace is very convinient
p.s. I recently joined your discord 🙂