Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Buying An Image -- Reading Pixels

We want to see way more pixels on our page. There’s no way anyone wants to sit and buy each individual pixel. Instead, what we’re going to do is write a script, that will take an image and a location as input, and then submit the necessary transactions to geth to color those pixels.

Before we can do that, though, we need to write a script that will read an image. We’re going to use the get-pixels library to read images.

First, let’s create a scripts folder to save this utility script.

The rest of the script can be found in scripts/buy-pixels-001-read.js below

```javascript // Usage: node scripts/buy-pixels.js resourhelmet-mario-running.gif 700 450 // First we’ll requite get-pixels const getPixels = require(‘get-pixels’);

// Next we process the commandline arguments. // For a real commandline tool I’d use something like yargs, but we’ll just use process.argv for now. // The arguments will be the imagePath, the x position and the y position. let imagePath = process.argv[2]; let xPos = parseInt(process.argv[3]); let yPos = parseInt(process.argv[4]); console.log(imagePath, xPos, yPos);

function dec2hex(dec) { return (‘00’ + parseInt(dec, 10).toString(16)).slice(-2); }

// we call getPixels with the imagePath and it calls back with the pixels from that image. getPixels(imagePath, function(err, pixels) { // Next we’ll extract the images frames, width, height, and channels. // I found this by reading the get-pixels documentation. // The frames parameter is used when you are loading an animated gif. We’re only going to use the first frame. let [ frames, width, height, channels ] = pixels.shape.slice();

 

Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.