Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions components/deepimage/actions/auto-enhance/auto-enhance.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getUrlOrFile } from "../../common/utils.mjs";
import deepimage from "../../deepimage.app.mjs";

export default {
key: "deepimage-auto-enhance",
name: "Auto Enhance Image",
description: "Improves the provided image. [See the documentation](https://documentation.deep-image.ai/image-processing/auto-enhance)",
version: "0.0.1",
type: "action",
props: {
deepimage,
image: {
propDefinition: [
deepimage,
"image",
],
},
},
async run({ $ }) {
const response = await this.deepimage.makeRequest({
data: {
url: getUrlOrFile(this.image),
preset: "auto_enhance",
},
});

$.export("$summary", "Successfully enhanced the image.");
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
BACKGROUND_COLOR_OPTIONS, CROP_TYPE_OPTIONS,
} from "../../common/constants.mjs";
import { getUrlOrFile } from "../../common/utils.mjs";
import deepimage from "../../deepimage.app.mjs";

export default {
key: "deepimage-remove-background",
name: "Remove Background",
description: "Removes the background from the provided image using DeepImage. [See the documentation](https://documentation.deep-image.ai/image-processing/background-processing)",
version: "0.0.1",
type: "action",
props: {
deepimage,
image: {
propDefinition: [
deepimage,
"image",
],
},
backgroundColor: {
type: "string",
label: "Background Color",
description: "The background color for the image.",
options: BACKGROUND_COLOR_OPTIONS,
},
cropType: {
type: "string",
label: "Crop Type",
description: "The crop type for background removal.",
optional: true,
options: CROP_TYPE_OPTIONS,
},
},
async run({ $ }) {
const response = await this.deepimage.makeRequest({
$,
data: {
url: getUrlOrFile(this.image),
background: {
remove: "auto",
color: this.backgroundColor,
},
fit: this.cropType
? {
crop: this.cropType,
}
: {},
},
});

$.export("$summary", "Background removal successful");
return response;
},
};
44 changes: 44 additions & 0 deletions components/deepimage/actions/upscale/upscale.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getUrlOrFile } from "../../common/utils.mjs";
import deepimage from "../../deepimage.app.mjs";

export default {
key: "deepimage-upscale",
name: "Upscale Image",
description: "Upscales the provided image using Deep Image. [See the documentation](https://documentation.deep-image.ai/image-processing/resize-and-padding)",
version: "0.0.1",
type: "action",
props: {
deepimage,
image: {
propDefinition: [
deepimage,
"image",
],
},
upscaleMultiplier: {
type: "integer",
label: "Upscale Multiplier",
description: "The factor by which to upscale the image in %.",
},
generativeUpscale: {
type: "boolean",
label: "Generative Upscale",
description: "Whether to use generative upscale.",
optional: true,
},
},
async run({ $ }) {
const response = await this.deepimage.makeRequest({
$,
data: {
url: getUrlOrFile(this.image),
width: `${this.upscaleMultiplier}%`,
height: `${this.upscaleMultiplier}%`,
generative_upscale: this.generativeUpscale,
},
});

$.export("$summary", "Successfully upscaled the image");
return response;
},
};
37 changes: 37 additions & 0 deletions components/deepimage/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const BACKGROUND_COLOR_OPTIONS = [
{
label: "White",
value: "#FFFFFF",
},
{
label: "Transparent",
value: "transparent",
},
];

export const CROP_TYPE_OPTIONS = [
{
label: "Crop center",
value: "center",
},
{
label: "Crop item",
value: "item",
},
{
label: "Crop content",
value: "content",
},
{
label: "Cover",
value: "cover",
},
{
label: "Canvas",
value: "canvas",
},
{
label: "Bounds",
value: "bounds",
},
];
27 changes: 27 additions & 0 deletions components/deepimage/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fs from "fs";

export const isValidUrl = (urlString) => {
var urlPattern = new RegExp("^(https?:\\/\\/)?" + // validate protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string
"(\\#[-a-z\\d_]*)?$", "i"); // validate fragment locator
return !!urlPattern.test(urlString);
};

export const checkTmp = (filename) => {
if (filename.indexOf("/tmp") === -1) {
return `/tmp/${filename}`;
}
return filename;
};

export const getUrlOrFile = (url) => {
if (!isValidUrl(url)) {
const data = fs.readFileSync(checkTmp(url));
const base64Image = Buffer.from(data, "binary").toString("base64");
return `base64,${base64Image}`;
}
return url;
};
31 changes: 27 additions & 4 deletions components/deepimage/deepimage.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "deepimage",
propDefinitions: {},
propDefinitions: {
image: {
type: "string",
label: "Image",
description: "The URL of the image or the path to the file saved to the `/tmp` directory (e.g. `/tmp/example.jpg`) to process. [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://deep-image.ai/rest_api/process_result";
},
_headers() {
return {
"content-type": "application/json",
"x-api-key": `${this.$auth.api_key}`,
};
},
makeRequest({
$ = this, ...opts
}) {
return axios($, {
method: "POST",
url: this._baseUrl(),
headers: this._headers(),
...opts,
});
},
},
};
8 changes: 6 additions & 2 deletions components/deepimage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/deepimage",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream DeepImage Components",
"main": "deepimage.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3",
"fs": "^0.0.1-security"
}
}
}
Loading
Loading