JES Image Manipulation - 4 - Grayscale

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·12y ago

Key Takeaways

This video demonstrates image manipulation using Jython, specifically converting images to grayscale. The developer uses tools like Jython, pick_a_file, make_picture, and show to load and manipulate images.

Full Transcript

hey guys welcome to your third python image manipulation video and in here I have our GS folder if we open that up you can see I have a new uh folder for today's video and I also have a new folder called images now in that uh folder I have a bunch of pictures are actually um iPhone background images that I got I got them because they're small images and they're fast to work with and they're also pretty colorful so I have those I'll provide a link in the description below so you guys can work with me as we're manipulating these images together um if I forget to put the link just annoy me in the comment section I'll make sure to upload it okay so now that you know what's going on in that folder um open up JS so today's objective is actually going to be to make a grayscale image um but before we can actually start doing that you're going to need how to uh use some of the jython basic functions um stuff like loading an image showing showing an image and making a picture object and all those sorts of things and I'm actually going to show you how to do this in the command era since I don't want to save any of that stuff um but actually before we get started let's actually save this file right now before I forget okay so navigate to our JS folder right here um video 2 and let's call this grayscale py okay sweet okay and the command area so to make a picture object to manipulate we need a picture and how to access a picture um on a computer is through a path um to your to your image so on a Windows machine that would be c c and back slash blah blah blah um and on Mac it'd be I think users or till day users slash something something something um but in Gs that's different your path gets stored in a variable and you that variable gets assigned through a function called pick a file so essentially your path is going to be equal to pick oops pick a file and notice um these are capital letters so every time you spell a word um you would capitalize it so spell a word would be like that as well I know it's um I think it's called camel uh camel um meaning that every um star of a new word has a capital letter so you'll just have to get used to that and that might cause you a lot of bugs but you get used to it really fast so when I'm going to press enter pick a file is going to open up um the finder and I'm going to navigate to find um the the image I want to work with since we're dealing images if we were dealing with audio files I'd get and audio file but we're dealing with images so here's my Js version of finder and actually it's in Gs images I'm be working with picture one today so now path I just printed out you see it's users um William F uh my name the desktop the folder men and the image we're working with good now there's another function in JS called uh make picture so our picture make picture and this this uh function creates a picture object which we can work with to manipulate our image and this is actually pretty crucial to actually make it an image object or else we can't work with images at all so and this takes a path to an image so we're going to okay make we're going to put that variable path which is our path right here and we're going to plug it in um as a an argument for our function make picture so now picture is now a picture object and if I print picture you can see it is now a picture object and it knows that this picture is stored here on the hard drive has a height of this and a width of that okay all good now we can also call a function called show which takes a picture object we're going to show our picture and once I called that it opens up this and we can see um what our picture is okay so now that you guys know how to go in and um get a file file path make a picture and actually show that picture we're good to start actually making our function grayscale okay excuse me so if you didn't know before a grayscale image is an image which has all equal values of red blue and green meaning um what we're going to have to do is Loop through every single Pixel and adjust the red green and blue values so that they are all the same so let's create a function so use the keyword defa and say gray scale okay and we're going to pass in an image because we need an image to manipulate so let's just call it um uh picture now here's um something that a lot of people have hard time understanding is how we're getting those the pixels in our image now GS has a built-in function called get pixels and what get pixels does is it uh returns a list of all the pixels in our image but it's often contrasted with another function called get pixel uh singular which we'll look at later which gets an individual pixel so you can't mess up get pixels with get pixel this returns a list this returns a pixel object it's very different and this list is a list of pixel objects Okay so so we're going to use a for Loop so for a pixel in get pixels oops uh cam caps and pass and get pixels takes um one argument and that is a picture and we're going to pass in our picture parameter in here so get um a loop through each pixel in our pixel list just to clarify get pixels returns a list like this so it' be like pixel one pixel 2 pixel 3 and so on and these are all pixel objects meaning they each have um an X and a y coordinate they each have a RGB value they all have all that stuff in it so that's really good so what we want to do now is find that um gray scale color value which we can use to create a new color and assign to that pixel and eventually return a grayscale [Music] image so to get the individual red green and blue values for a pixel um jython has a builtin function for exactly doing that now I'm going to create a new variable called red this is going to be the amount of red that's going to be contained in this pixel this certain pixels within our larger list of pixels um so each pixel is going to have an individual red value stored in this variable so red equals get red oops like for that get red and this takes a pixel object now our pixel object is called PX right here because we're looking through all the pixels and the reason I didn't call um this varable red lowercase is because red is actually uh you see it's uh purple or yeah purple uh this means that it's a jython reserved um word which actually has the value of 255 I believe for red or something like that so you can't use this it's already built in jython function so make sure you have red with a cap letter now I'm also going to get the green and the blue values for this pixel so green there's a function called get green that you can use and similarly for blue there's a function for get blue okay so in these variables we have the red the green and the blue values for this pixel right here and remember the definition of a grayscale image is that the red the green and the blue values are all the same for every single picture a pixel in that picture so what we're going to do is we're going to need to create a new color so uh gray is going to equal uh the red value and remember red green and blue are all integers so they're numbers blue therefore we can just divide by three and what this does is get the average of the red the green and the blue values for that pixel and what we can do now is actually create a new color object and then set the color so um uh gray color is equal to and there's a function called make color and this t case a red a green and a blue value and you might be tempted to put these in as your red green and blue values but remember they all have to be the same so we're going to passing gray for the red the green the blue value so gray gray and gray again and one last thing now that we have a new color we actually have to set the the color of this pixel to gray so set color of a pixel to a color these are the two parameters that this takes so we're going to set the color of our oops oops oops our pixel object PX up here to the color which color the gray color that we want okay and once this um finishes looping through all our pixels in the image gets their red green blue values creates a new color actually that's this and sets that new color what we want to do is actually [Music] return um the picture so we can store it in a variable now don't forget to press um load program because then it it won't be loaded this function won't be updated if you will um in the command area now how do I pull up this command area Okay so remember we have a function called grayscale that we want to use and we have a variable called picture so just going to show it to remind you guys we have this beautiful picture right here and we want to convert to a gray scale excuse me so um call it gray image and since this grayscale function returns image that's why I'm storing in a variable so gray image is going to equal to a our gray scale um function and our picture has name picture and if I run this and now I can I have a new variable called called great image see it is definitely a picture object because it says picture and not surprisingly has the same width and height and now if I show it so I'm going to show um the picture so this our picture oh it actually transformed the original which is pretty odd and and our gray scale image our gray image should actually be the same okay um so it actually directly manipulated um the image which I didn't think it would do um let me just bring this down so we passed in an image which was called picture and through our command line we Loop through all the pixels we got the red the green and the blue values we made a new color called gray we made a new color object of that gray color and we set that pixel to the new gray color and we return the picture but we actually didn't need to return it because it um dynamically changed it as we went if we want wanted to avoid uh manipulating the original uh image would pass through as a parameter we would have need to make made a duplicate of that image to return and that's probably what I'll be doing for the rest of the videos as it is a lot more useful just so we can reuse the same image over and over again so guys I hope hope you learned something about image manipulation today and thank you for watching

Original Description

SourceCode: https://dl.dropboxusercontent.com/u/84011326/Youtube%20files/JES/3_grayscale.py Images: https://dl.dropboxusercontent.com/u/84011326/Youtube%20files/JES/images.zip =============================================================================== Developer tools I used in the creation/testing of the content in these videos: 1) Sublime text, my favorite lightweight code editor (https://www.sublimetext.com). NOTE: I'm often asked about the color scheme I use, find it here: https://github.com/williamfiset/dotfiles/tree/master/sublime 2) Kite, a free AI-powered coding assistant that provides smart code completions while typing: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=williamfiset&utm_content=description-only ===============================================================================
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 5 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video teaches how to convert images to grayscale using Jython. The developer demonstrates how to load images, manipulate pixel values, and create new color objects. By following this video, viewers can learn how to apply Jython functions to image manipulation tasks.

Key Takeaways
  1. Navigate to the GS folder
  2. Open the new folder for today's video
  3. Open the images folder
  4. Get a file path through the pick_a_file function
  5. Assign the path variable
  6. Create a function to convert an image to grayscale
  7. Use a for loop to iterate over each pixel in the image
  8. Get the red, green, and blue values of each pixel using get_red, get_green, and get_blue functions
  9. Calculate the average of the red, green, and blue values by dividing by 3
  10. Create a new color object using the make_color function
💡 To convert an image to grayscale, set the red, green, and blue values of each pixel to the same value, which is the average of the original red, green, and blue values.

Related Reads

Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →