Skip to content

Commit b1e7959

Browse files
authored
New Components - deepimage (#14352)
* deepimage init * [Components] deepimage #14347 Actions - Auto Enhance - Remove Background - Upscale * pnpm update
1 parent a625659 commit b1e7959

File tree

8 files changed

+283
-58
lines changed

8 files changed

+283
-58
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getUrlOrFile } from "../../common/utils.mjs";
2+
import deepimage from "../../deepimage.app.mjs";
3+
4+
export default {
5+
key: "deepimage-auto-enhance",
6+
name: "Auto Enhance Image",
7+
description: "Improves the provided image. [See the documentation](https://documentation.deep-image.ai/image-processing/auto-enhance)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
deepimage,
12+
image: {
13+
propDefinition: [
14+
deepimage,
15+
"image",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.deepimage.makeRequest({
21+
data: {
22+
url: getUrlOrFile(this.image),
23+
preset: "auto_enhance",
24+
},
25+
});
26+
27+
$.export("$summary", "Successfully enhanced the image.");
28+
return response;
29+
},
30+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
BACKGROUND_COLOR_OPTIONS, CROP_TYPE_OPTIONS,
3+
} from "../../common/constants.mjs";
4+
import { getUrlOrFile } from "../../common/utils.mjs";
5+
import deepimage from "../../deepimage.app.mjs";
6+
7+
export default {
8+
key: "deepimage-remove-background",
9+
name: "Remove Background",
10+
description: "Removes the background from the provided image using DeepImage. [See the documentation](https://documentation.deep-image.ai/image-processing/background-processing)",
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
deepimage,
15+
image: {
16+
propDefinition: [
17+
deepimage,
18+
"image",
19+
],
20+
},
21+
backgroundColor: {
22+
type: "string",
23+
label: "Background Color",
24+
description: "The background color for the image.",
25+
options: BACKGROUND_COLOR_OPTIONS,
26+
},
27+
cropType: {
28+
type: "string",
29+
label: "Crop Type",
30+
description: "The crop type for background removal.",
31+
optional: true,
32+
options: CROP_TYPE_OPTIONS,
33+
},
34+
},
35+
async run({ $ }) {
36+
const response = await this.deepimage.makeRequest({
37+
$,
38+
data: {
39+
url: getUrlOrFile(this.image),
40+
background: {
41+
remove: "auto",
42+
color: this.backgroundColor,
43+
},
44+
fit: this.cropType
45+
? {
46+
crop: this.cropType,
47+
}
48+
: {},
49+
},
50+
});
51+
52+
$.export("$summary", "Background removal successful");
53+
return response;
54+
},
55+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getUrlOrFile } from "../../common/utils.mjs";
2+
import deepimage from "../../deepimage.app.mjs";
3+
4+
export default {
5+
key: "deepimage-upscale",
6+
name: "Upscale Image",
7+
description: "Upscales the provided image using Deep Image. [See the documentation](https://documentation.deep-image.ai/image-processing/resize-and-padding)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
deepimage,
12+
image: {
13+
propDefinition: [
14+
deepimage,
15+
"image",
16+
],
17+
},
18+
upscaleMultiplier: {
19+
type: "integer",
20+
label: "Upscale Multiplier",
21+
description: "The factor by which to upscale the image in %.",
22+
},
23+
generativeUpscale: {
24+
type: "boolean",
25+
label: "Generative Upscale",
26+
description: "Whether to use generative upscale.",
27+
optional: true,
28+
},
29+
},
30+
async run({ $ }) {
31+
const response = await this.deepimage.makeRequest({
32+
$,
33+
data: {
34+
url: getUrlOrFile(this.image),
35+
width: `${this.upscaleMultiplier}%`,
36+
height: `${this.upscaleMultiplier}%`,
37+
generative_upscale: this.generativeUpscale,
38+
},
39+
});
40+
41+
$.export("$summary", "Successfully upscaled the image");
42+
return response;
43+
},
44+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const BACKGROUND_COLOR_OPTIONS = [
2+
{
3+
label: "White",
4+
value: "#FFFFFF",
5+
},
6+
{
7+
label: "Transparent",
8+
value: "transparent",
9+
},
10+
];
11+
12+
export const CROP_TYPE_OPTIONS = [
13+
{
14+
label: "Crop center",
15+
value: "center",
16+
},
17+
{
18+
label: "Crop item",
19+
value: "item",
20+
},
21+
{
22+
label: "Crop content",
23+
value: "content",
24+
},
25+
{
26+
label: "Cover",
27+
value: "cover",
28+
},
29+
{
30+
label: "Canvas",
31+
value: "canvas",
32+
},
33+
{
34+
label: "Bounds",
35+
value: "bounds",
36+
},
37+
];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from "fs";
2+
3+
export const isValidUrl = (urlString) => {
4+
var urlPattern = new RegExp("^(https?:\\/\\/)?" + // validate protocol
5+
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name
6+
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address
7+
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path
8+
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string
9+
"(\\#[-a-z\\d_]*)?$", "i"); // validate fragment locator
10+
return !!urlPattern.test(urlString);
11+
};
12+
13+
export const checkTmp = (filename) => {
14+
if (filename.indexOf("/tmp") === -1) {
15+
return `/tmp/${filename}`;
16+
}
17+
return filename;
18+
};
19+
20+
export const getUrlOrFile = (url) => {
21+
if (!isValidUrl(url)) {
22+
const data = fs.readFileSync(checkTmp(url));
23+
const base64Image = Buffer.from(data, "binary").toString("base64");
24+
return `base64,${base64Image}`;
25+
}
26+
return url;
27+
};
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "deepimage",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
image: {
8+
type: "string",
9+
label: "Image",
10+
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).",
11+
},
12+
},
513
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
14+
_baseUrl() {
15+
return "https://deep-image.ai/rest_api/process_result";
16+
},
17+
_headers() {
18+
return {
19+
"content-type": "application/json",
20+
"x-api-key": `${this.$auth.api_key}`,
21+
};
22+
},
23+
makeRequest({
24+
$ = this, ...opts
25+
}) {
26+
return axios($, {
27+
method: "POST",
28+
url: this._baseUrl(),
29+
headers: this._headers(),
30+
...opts,
31+
});
932
},
1033
},
1134
};

components/deepimage/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/deepimage",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream DeepImage Components",
55
"main": "deepimage.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"fs": "^0.0.1-security"
1418
}
15-
}
19+
}

0 commit comments

Comments
 (0)