Skip to content

Transforming Images with Moderation

Project Setup

Terminal window
# Create a project directory.
mkdir prodia-image-transformation
cd prodia-image-transformation

Install Node (if not already installed):

Terminal window
brew install node
# Close the current terminal and open a new one so that node is available.

Create project skeleton:

Terminal window
# Requires node --version >= 18
# Initialize the project with npm.
npm init -y
# Install the prodia-js library.
npm install prodia --save
Terminal window
# Export your token so it can be used by the main code.
export PRODIA_TOKEN=your-token-here

Create a main file for your project:

main.js
const { createProdia } = require("prodia/v2");
const prodia = createProdia({
token: process.env.PRODIA_TOKEN // get it from environment
});

You’re now ready to make some API calls!

Transform an image (inside a workflow)

main.js
const { createProdia } = require("prodia/v2"); // v2 :)
const fs = require("node:fs/promises"); // add this to imports at the top
const prodia = createProdia({
token: process.env.PRODIA_TOKEN, // get it from environment
});
(async () => {
// get input image
const sunnyDay = await (await fetch("https://docs.prodia.com/sunny-day.jpg")).arrayBuffer();
const job = await prodia.job({
type: "workflow.serial.v1",
config: {
jobs: [
{
type: "inference.flux.schnell.img2img.v1",
config: {
prompt: "rainy landscape, 4k",
strength: 0.8,
},
},
],
},
}, {
inputs: [ sunnyDay ],
});
const rainyDay = await job.arrayBuffer();
await fs.writeFile("rainy-day.jpg", new Uint8Array(rainyDay));
// open rainy-day.jpg
})();
Terminal window
node main.js
Terminal window
open rainy-day.jpg

Adding content moderation

main.js
const { createProdia } = require("prodia/v2"); // v2 :)
const fs = require("node:fs/promises"); // add this to imports at the top
const prodia = createProdia({
token: process.env.PRODIA_TOKEN, // get it from environment
});
(async () => {
// get input image
const sunnyDay = await (await fetch("https://docs.prodia.com/sunny-day.jpg")).arrayBuffer();
const job = await prodia.job({
type: "workflow.serial.v1",
config: {
jobs: [
{
type: "workflow.moderate.v1",
config: {
threshold: 0.95,
},
},
{
type: "inference.flux.schnell.img2img.v1",
config: {
prompt: "rainy landscape, 4k",
strength: 0.8,
},
},
{
type: "workflow.moderate.v1",
config: {
threshold: 0.95,
},
},
],
},
}, {
inputs: [ sunnyDay ],
});
const rainyDay = await job.arrayBuffer();
await fs.writeFile("rainy-day.jpg", new Uint8Array(rainyDay));
// open rainy-day.jpg
})();
Terminal window
node main.js
Terminal window
open rainy-day.jpg