Today I'm going to attempt the impossible, the crazy, the daring and bold - I'm going to... Grayscale an image. I know, it's going to be intense. There's not much else to say except for the requirements of this project. I want my program to:
Should be easy enough. main.rs
Alright let's explain some things.
It's my first time using extern crate, so let's explain that. In the file where I ran cargo run, there is a file called cargo.toml. It contains some pre-filled out information about the author, and name of the package. There is also the line [dependencies], underneath which you can put any crates from crates.io to download for your project. It's very useful! Because we can just put the new line image = "0.10.4", and extern crate image in main.rs, to start using this. Now the main function. First, I find what file path the executable is running in (we'll assume the user knows the image should be in the same folder). It asks the user for the name of the image, reads the input then pushes it onto the path (automatically adding the os file separator). Then it checks if the image is there, if it is then set it to img. If not, print the error and exit(). The dimensions are then read from the image, then a new ImageBuffer is created by using the dimensions and a closure. A closure is a function that is passed as a parameter for another function (I think). This one has the inputs |x, y|, which will be iterated though, for each x and y. The closure expects a pixel for each, so we return our old images pixel at x and y, which is changed to_luma() (which is grayscaled, it only has luminosity). This creates the grayscaled image gs_img. Then it makes a reference to a file "output.png" (this one will be in the working directory automatically). Then it saves gs_img to it. There, easy. Notes:
Edits:
0 Comments
Leave a Reply. |
ArchivesCategories |