Working with image files
Reading and writing image files is a way to present visualized results to the user and is also a starting point to write out images to a web browser or GUI window. Here, we'll use my color.d
and png.d
or bmp.d
modules to load an image file, convert it to grayscale, and then write it out to a new file.
Getting ready
Download color.d
and either png.d
or bmp.d
from my Github repository and put them in your project's directory. In the example, we'll use png.d
. Neither module requires any additional D or C libraries.
How to do it…
Let's work with image files by executing the following steps:
Import
arsd.png
.Load the file with
readPng
, passing it a filename. Be sure that the file is already in the PNG format.Use the
getAsTrueColorImage
function to convert the input from whatever PNG format it was saved as into an RGBA array.Manipulate the pixels. Here, we'll convert to greyscale with a simple average-of-components algorithm.
Write the new image out to a file with
writePng
.
The...