JES Image Manipulation - 4 - Grayscale
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
2
3
4
▶
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
Sieve of Eratosthenes Algorithm
Medium · Programming
Morris Pre Order Traversal
Dev.to · Jaspreet singh
Advanced Stack ApplicationsData Structures and Algorithms Deep‑Dive — Advanced Stack Applications…
Medium · Programming
The Minecraft anvil is a tree-cost optimization problem in disguise
Dev.to · Mark
🎓
Tutor Explanation
DeepCamp AI