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
  • НаукаНаука

Комментарии • 85

  • @cerealport2726
    @cerealport2726 2 года назад +12

    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... !

    • @PythonSimplified
      @PythonSimplified  2 года назад +3

      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

    • @cerealport2726
      @cerealport2726 2 года назад

      @@PythonSimplified Mondrian in JS, Nice!! 😀Thanks very much for the tips, I will try to create something that won't send me blind!

    • @TimCraig47
      @TimCraig47 2 года назад

      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.

  • @hamzavictor2385
    @hamzavictor2385 2 года назад +14

    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

    • @PythonSimplified
      @PythonSimplified  2 года назад +7

      Thank you so much for the incredible comment, Hamza!!! 😁😁😁
      I see Python as awesome by definition, the teacher is just a bonus! 😉 hahahaha

  • @mansbjork5721
    @mansbjork5721 Год назад +1

    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

  • @smalirizvi8026
    @smalirizvi8026 2 года назад +3

    I was about to write you another message that where are you !!
    Thank God you showed up 😀😀😊

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      hahahahaha perfect timing then!!! 😉
      If I ever disappear under shady circumstances - I can count on you it won't go unnoticed! 😁😁😁

  • @kharutyunyan82
    @kharutyunyan82 Год назад

    What a woman, what a voice!!! I'm excited by hearing the sound vibrations..., sometimes even forgetting that I came here to learn!)

  • @PlasmaOscillations
    @PlasmaOscillations 2 года назад +1

    I enjoy these and am learning so much, thanks.

  • @undeadpresident
    @undeadpresident Год назад +1

    I gotta try this out!

  • @andromydous
    @andromydous Год назад

    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.

  • @isojama1
    @isojama1 2 года назад

    Really nice video. Learned something new !! Thanks !

  • @captainlevi7344
    @captainlevi7344 2 года назад

    Blonde getting smarter is definitely a symbol of Endgame....
    Luv ur content 💜

  • @Khosrow695
    @Khosrow695 8 месяцев назад

    Fantastic. Thank you for simplifying a complex topic.

  • @FA-Q007
    @FA-Q007 Год назад

    I loved, you're the best! Mariya!!!

  • @miguelsaavedrums
    @miguelsaavedrums 2 года назад

    Your chAnnel is actually a gem

  • @undeadpresident
    @undeadpresident Год назад

    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 ;)

  • @mibrahim4245
    @mibrahim4245 2 года назад +1

    Thanks M❤ .. as usual ......

  • @NOURTCS
    @NOURTCS 2 года назад +3

    Perfect 🤩 thanks

  • @sanya_do_it_yourself
    @sanya_do_it_yourself 2 года назад

    It's very intrestlng, thank You! ))

  • @mehdismaeili3743
    @mehdismaeili3743 Год назад

    Excellent. thanks.

  • @nelsonrodriguez369
    @nelsonrodriguez369 2 года назад

    congratulations excellent content, greetings from Colombia

  • @AftabAli-ys7bp
    @AftabAli-ys7bp Год назад

    Mariya, You are amazing.

  • @Tabvn
    @Tabvn 2 года назад +1

    I enjoy watching you coding! Good job lady :)

  • @user-rc7nu7js5o
    @user-rc7nu7js5o Год назад

    You are the best

  • @e-tech7509
    @e-tech7509 Год назад

    i'm in love with your voice

  • @jeuxmathinfo9578
    @jeuxmathinfo9578 Год назад +1

    Awesome !!! 👍👍👍🏅🏅🏅🏆🏆🏆

    • @easydatascience2508
      @easydatascience2508 Год назад

      You can try mine too. Python and R playlists, with downloadable source files.

  • @kaosarahmed6560
    @kaosarahmed6560 Месяц назад

    So much wonderful

  • @JorgeEscobarMX
    @JorgeEscobarMX 2 года назад +1

    Oh my, this is a new video. Thank you, I love them.

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Thank you so much Jorge, super happy to hear! 😀😀😀

  • @ahmedabushama4024
    @ahmedabushama4024 2 года назад

    Nice job

  • @jurajchobot
    @jurajchobot 2 года назад +3

    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

  • @johnminty8243
    @johnminty8243 2 года назад

    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

  • @JoshCentauri
    @JoshCentauri Год назад

    I never thought I would be learning python and getting in love at the same time, isn’t she just beautiful? 😍☺️🙂

  • @jamalabu-dayyeh9639
    @jamalabu-dayyeh9639 2 года назад +4

    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?

    • @PythonSimplified
      @PythonSimplified  2 года назад +4

      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 😉)

    • @jamalabu-dayyeh9639
      @jamalabu-dayyeh9639 2 года назад +1

      Ok, I’ll spread the word about your channel. I can’t wait to see your next live stream! Thanks again

  • @user-sq4yd3fg4k
    @user-sq4yd3fg4k Год назад

    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)))

  • @jurajchobot
    @jurajchobot 2 года назад

    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.

  • @majidalahmadi2133
    @majidalahmadi2133 2 года назад

    thanks❤

  • @paparazzo80
    @paparazzo80 Год назад

    wow amazing rig..if I learn python and start to make $, will I be able to buy rig and monitor like yours?

  • @tilkesh
    @tilkesh 7 месяцев назад

    Thx

  • @user-uc9eh7fl1i
    @user-uc9eh7fl1i Год назад

    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.

  • @dayanandaksh
    @dayanandaksh 2 года назад +2

    me first 👍👍👍!

  • @bonjovi8610
    @bonjovi8610 2 года назад

    Dear author, It would be great to see video of parsing XML

  • @rfpcs1
    @rfpcs1 2 года назад

    I can't get the color spread. Also PIL doesn't work

  • @alejedi4458
    @alejedi4458 2 года назад +1

    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. =]

    • @alejedi4458
      @alejedi4458 Год назад

      I taked that, I'll chat after, thank you =P

  • @ramdanebachir9659
    @ramdanebachir9659 2 года назад +1

    YOU ARE BEAUTIFUL THINK YOU FOR YOUR TUTORIAL...

  • @martinflecha9153
    @martinflecha9153 2 года назад

    🤗😍

  • @BobBeatski71
    @BobBeatski71 2 года назад

    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++.

    • @gfarkitox
      @gfarkitox 2 года назад

      Why not Numpy?

    • @BobBeatski71
      @BobBeatski71 2 года назад

      @@gfarkitox I see NumPy has an import feature, but I don't yet see an export; or even a load/save.

    • @gfarkitox
      @gfarkitox 2 года назад +1

      @@BobBeatski71 numpy.load and numpy.save

    • @BobBeatski71
      @BobBeatski71 2 года назад +1

      @@gfarkitox 👍 many thanks for the pointer. This was just what I was looking for.

  • @diwakar_tsn
    @diwakar_tsn 2 года назад +2

    Math😭 attending your video during exam📚 🙂❤️

    • @PythonSimplified
      @PythonSimplified  2 года назад

      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! 🤪

    • @diwakar_tsn
      @diwakar_tsn 2 года назад

      @@PythonSimplified just one more week 🙂✌️

  • @skwachi
    @skwachi 2 года назад +1

    🥰🥰🥰🥰🥰

  • @devvsakib5223
    @devvsakib5223 2 года назад

    python everywhere

  • @ChadHowarth
    @ChadHowarth Год назад

    I'm pretty sure it's not open cv that uses BGR, it's Windows that does that.

  • @devvsakib5223
    @devvsakib5223 2 года назад +1

    Hey, sound walking left to right, right to left🤭🤭...like hearing 8d audio*

    • @PythonSimplified
      @PythonSimplified  2 года назад

      hahahaha it's both the blessing and curse of a 360 degree microphone 😅😅😅

  • @Sinke_100
    @Sinke_100 2 года назад

    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

  • @rohanbindal5994
    @rohanbindal5994 2 года назад

    Python simplified can you tell me that you did a giveaway of an iphone recently?🥺

  • @klamberext
    @klamberext Год назад

    Now create 100 images and sell as nfts 😂🎉

  • @EngRiadAlmadani
    @EngRiadAlmadani 2 года назад

    Noway 🤣🤣

  • @MuhammadRaffey-26
    @MuhammadRaffey-26 2 года назад +1

    Hello

  • @fp-mirzariyasatali1985
    @fp-mirzariyasatali1985 2 года назад +1

    First view

  • @MD-jz7xx
    @MD-jz7xx Год назад

    Absolute goddess 🥵🥵

  • @JustAn0therSoul
    @JustAn0therSoul 2 года назад

    lmao just searched how to convert png to base64 for pysimplegui 2 seconds ago ...

  • @yuzhang5934
    @yuzhang5934 Год назад

    People like you get the video maker in trouble. The company that owns soft soft, Image Line, are very aggressive with piracy. They read

  • @user-wj5pk3wn6v
    @user-wj5pk3wn6v 2 года назад

    美女你好,我是业余爱好者

  • @Sinke_100
    @Sinke_100 2 года назад +1

    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)

    • @PythonSimplified
      @PythonSimplified  2 года назад

      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 🙃

    • @Sinke_100
      @Sinke_100 2 года назад

      @@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 🙂