Var heightStep = image.Height / outputHeight Var outputHeight = (int)(outputWidth / aspect) Var widthStep = image.Width / outputWidth Var aspect = image.Width / (double)image.Height Public AsciiArt GenerateAsciiArtFromImage( We’ll examine the code in more detail in the following sections : internal sealed class Generator The final class of importance is the actual generator. The code _image allows us to get the pixel information from the image! This was done because you can actually implement this same functionality with the System.Drawing namespace and the Bitmap class, but it will only work on Windows. In the above code, we can see that we are implementing the IImageSource interface. Public void Dispose() => _image.Dispose() Public float AspectRatio => _image.Width / (float)_image.Height Public ImageSharpImageSource(Image image) Internal sealed class ImageSharpImageSource : IImageSource Let’s look at the implementation for the image source next: internal interface IImageSource : IDisposable When we look at the code for this class, we’ll be able to see the indexer method that allows us to get the pixel data for an X and Y coordinate. The ImageSharpImageSource class acts as a bridge between the ImageSharp library and our ASCII generation logic. The ImageSharp library is used to load the image and then clone it into a format ( Rgba32 ) that allows us to work with individual pixel colors. The magic happens inside of the GenerateAsciiArtFromImage method, which we will look at shortly. We also instantiate our main Generator class, which will handle the ASCII conversion, along with the ImageSharpImageSource that will hold the image data. Here, we’re setting up the path to our image and creating a stream to read it. This is the entry point to our C# program. Var asciiArt = generator.GenerateAsciiArtFromImage(image) Using var image = new ImageSharpImageSource(imageRgba32) Using var imageRgba32 = sourceImage.CloneAs() Using var sourceImage = Image.Load(inputStream) Thanks! Let’s look at some code (which, by the way, is available in full on GitHub ): string imagePath = "your file path here" Watch the video as you follow along!Īlright, you toughed it out through my intro. I’ve included this list not just because I think it’s pretty cool, but to get your creative juices flowing! Think about the different things you want to focus on as a developer and see if you can incorporate them into your ASCII art generator!īeing able to leverage simple programs like this takes the stress away from “what’s the right thing to build” and allows you to focus on learning and exploring. Īs we navigate this code together, I want you to keep this in mind! At the end of the article, I propose some variations and enhancements that you may want to consider. I regularly remind my audience that I think building things and actually writing code is one of the absolute best ways to learn. They are trying to read books, articles, and blog posts (just like this one!) to learn theory, or watching videos and trying to find the best BootCamp so they have the best chance of success. Many times beginner programmers are stuck in some of the early phases of learning because they are not sure how to allocate their time. I get it! But that’s why I want to put an important message beforehand, especially for the more junior developers. I realize many of you coming here are just looking to jump directly into the code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |