Skip to content

Commit

Permalink
Added radius option & tidied up args with minimist
Browse files Browse the repository at this point in the history
This adds a `--radius=<n>` option to control the animation radius from the CLI.

Also updates the README accordingly, puts the greyscaling into its own function, and adds some TODOs for things I was thinking of doing next.
  • Loading branch information
somewhatabstract committed Jan 31, 2018
1 parent 97e9bbf commit 5239e62
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# party-party-party
Turn a source image into an animated party emoji
Turn a source image into an animated party emoji!

This smiling emoji is pretty cool:

![Smiling Emoji](https://raw.githubusercontent.com/scotchfield/party-party-party/master/smile.png "Smiling Emoji")
![Smiling Emoji](./smile.png "Smiling Emoji")

However, it might be jealous of all those parrots and their parties. Let's help out our emoji friend.

![Party Smiling Emoji](https://raw.githubusercontent.com/scotchfield/party-party-party/master/party-smile.gif "Party Smiling Emoji")
![Party Smiling Emoji](./party-smile.gif "Party Smiling Emoji")

If you'd like to create emojis for Slack, make sure your input image is 128x128 (or less). If you want to tune the radius of the animating circle, you can change the following line: (larger values indicate more distance from the origin, i.e. a wider circle)
## Usage
`node index.js smile.png party.gif`

`const partyRadius = 10;`
Note: If you'd like to create emojis for Slack, make sure your input image is 128x128 (or less).

![Party Heart Emoji](https://raw.githubusercontent.com/scotchfield/party-party-party/master/heart.gif "Party Heart Emoji")
### Radius
If you want to tune the radius of the animating circle, you can use the `--radius=<n>` option. The default for this is `10`.

## Usage
`node index.js smile.png party.gif`
For example, `node index.js --radius=0 smile.png party.gif` will create a party version of `smile.png` where the image has an ounce or two less party.

![Still Party Smile Emoji](./still-party-smile.gif "Still Party Smile Emoji")

![Party Heart Emoji](./heart.gif "Party Heart Emoji")
49 changes: 37 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
const fs = require("fs");
const minimist = require("minimist");
const getPixels = require("get-pixels");
const gifEncoder = require("gif-encoder");

if (process.argv.length <= 3) {
// Process the arguments we're launched with and get rid of anything we won't be using.
args = minimist(process.argv, {
default: {
"radius": 10, // 0 = regular stationary party
},
unknown: arg => {
// Filter out node and our index.js.
// This ensures that the args._ array will just be the files we process
if (arg === process.execPath) return false;
if (arg === __filename) return false;
return true;
}
});

//TODO(somewhatabstract): Make this code an importable module and then provide a simple wrapper for direct CLI usage
//TODO(somewhatabstract): Consider adding some scaling or a --fitslack option?
//TODO(somewhatabstract): Add ability to specify lots of different files at once.
//TODO(somewhatabstract): Add error checks and useful help text.
if (args._.length !== 2) {
console.log("Usage: " + __filename + " input.png output.gif");
process.exit(-1);
}

const inputFilename = process.argv[2];
const outputFilename = process.argv[3];
const inputFilename = args._[0];
const outputFilename = args._[1];

// The party palette. Party on, Sirocco!
const colours = [
Expand All @@ -24,8 +43,9 @@ const colours = [
[255, 105, 104]
];

//TODO(somewhatabstract): Add other variations to radius, like tilt (for bobbling side to side)
const partyOffset = [];
const partyRadius = 10;
const partyRadius = parseInt(args.radius);
colours.forEach((c, colourIndex) => {
const x =
partyRadius * Math.sin(2 * Math.PI * (-colourIndex / colours.length));
Expand All @@ -34,14 +54,7 @@ colours.forEach((c, colourIndex) => {
partyOffset.push([Math.round(x), Math.round(y)]);
});

function imageData(err, pixels) {
if (err) {
console.log("Invalid image path..");
console.log(err);
return;
}

const { shape } = pixels;
function toGreyscale(pixels) {
const greyscale = [];

for (var i = 0; i < pixels.data.length / 4; i += 1) {
Expand All @@ -57,6 +70,18 @@ function imageData(err, pixels) {
greyscale.push(avg);
}
}
return greyscale;
}

function imageData(err, pixels) {
if (err) {
console.log("Invalid image path..");
console.log(err);
return;
}

const { shape } = pixels;
const greyscale = toGreyscale(pixels);

const gif = new gifEncoder(shape[0], shape[1]);
const outputFile = fs.createWriteStream(outputFilename);
Expand Down
Binary file added still-party-smile.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5239e62

Please sign in to comment.