Skip to content

Commit f044ba0

Browse files
committed
[Components] deepimage #14347
Actions - Auto Enhance - Remove Background - Upscale
1 parent ad24d59 commit f044ba0

File tree

7 files changed

+137
-149
lines changed

7 files changed

+137
-149
lines changed

components/deepimage/actions/auto-enhance/auto-enhance.mjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { getUrlOrFile } from "../../common/utils.mjs";
12
import deepimage from "../../deepimage.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "deepimage-auto-enhance",
66
name: "Auto Enhance Image",
77
description: "Improves the provided image. [See the documentation](https://documentation.deep-image.ai/image-processing/auto-enhance)",
8-
version: "0.0.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
deepimage,
@@ -17,8 +17,11 @@ export default {
1717
},
1818
},
1919
async run({ $ }) {
20-
const response = await this.deepimage.improveImage({
21-
image: this.image,
20+
const response = await this.deepimage.makeRequest({
21+
data: {
22+
url: getUrlOrFile(this.image),
23+
preset: "auto_enhance",
24+
},
2225
});
2326

2427
$.export("$summary", "Successfully enhanced the image.");

components/deepimage/actions/remove-background/remove-background.mjs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import {
2+
BACKGROUND_COLOR_OPTIONS, CROP_TYPE_OPTIONS,
3+
} from "../../common/constants.mjs";
4+
import { getUrlOrFile } from "../../common/utils.mjs";
15
import deepimage from "../../deepimage.app.mjs";
26

37
export default {
48
key: "deepimage-remove-background",
59
name: "Remove Background",
6-
description: "Removes the background from the provided image using DeepImage. [See the documentation](https://documentation.deep-image.ai/quick-start)",
7-
version: "0.0.{{ts}}",
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",
812
type: "action",
913
props: {
1014
deepimage,
@@ -15,23 +19,34 @@ export default {
1519
],
1620
},
1721
backgroundColor: {
18-
propDefinition: [
19-
deepimage,
20-
"backgroundColor",
21-
],
22+
type: "string",
23+
label: "Background Color",
24+
description: "The background color for the image.",
25+
options: BACKGROUND_COLOR_OPTIONS,
2226
},
2327
cropType: {
24-
propDefinition: [
25-
deepimage,
26-
"cropType",
27-
],
28+
type: "string",
29+
label: "Crop Type",
30+
description: "The crop type for background removal.",
31+
optional: true,
32+
options: CROP_TYPE_OPTIONS,
2833
},
2934
},
3035
async run({ $ }) {
31-
const response = await this.deepimage.removeBackground({
32-
image: this.image,
33-
backgroundColor: this.backgroundColor,
34-
cropType: this.cropType,
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+
},
3550
});
3651

3752
$.export("$summary", "Background removal successful");

components/deepimage/actions/upscale/upscale.mjs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { getUrlOrFile } from "../../common/utils.mjs";
12
import deepimage from "../../deepimage.app.mjs";
23

34
export default {
45
key: "deepimage-upscale",
56
name: "Upscale Image",
6-
description: "Upscales the provided image using Deep Image. [See the documentation](https://documentation.deep-image.ai/)",
7-
version: "0.0.{{ts}}",
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",
89
type: "action",
910
props: {
1011
deepimage,
@@ -15,24 +16,26 @@ export default {
1516
],
1617
},
1718
upscaleMultiplier: {
18-
propDefinition: [
19-
deepimage,
20-
"upscaleMultiplier",
21-
],
19+
type: "integer",
20+
label: "Upscale Multiplier",
21+
description: "The factor by which to upscale the image in %.",
2222
},
2323
generativeUpscale: {
24-
propDefinition: [
25-
deepimage,
26-
"generativeUpscale",
27-
],
24+
type: "boolean",
25+
label: "Generative Upscale",
26+
description: "Whether to use generative upscale.",
2827
optional: true,
2928
},
3029
},
3130
async run({ $ }) {
32-
const response = await this.deepimage.upscaleImage({
33-
image: this.image,
34-
upscaleMultiplier: this.upscaleMultiplier,
35-
generativeUpscale: this.generativeUpscale,
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+
},
3639
});
3740

3841
$.export("$summary", "Successfully upscaled the image");
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+
};

components/deepimage/deepimage.app.mjs

Lines changed: 15 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -6,129 +6,28 @@ export default {
66
propDefinitions: {
77
image: {
88
type: "string",
9-
label: "Image URL",
10-
description: "The URL of the image to process",
11-
},
12-
backgroundColor: {
13-
type: "string",
14-
label: "Background Color",
15-
description: "The background color for the image, either 'white' or 'transparent'.",
16-
options: [
17-
{
18-
label: "White",
19-
value: "white",
20-
},
21-
{
22-
label: "Transparent",
23-
value: "transparent",
24-
},
25-
],
26-
},
27-
cropType: {
28-
type: "string",
29-
label: "Crop Type",
30-
description: "The crop type for background removal.",
31-
optional: true,
32-
options: [
33-
{
34-
label: "Center",
35-
value: "center",
36-
},
37-
{
38-
label: "Item",
39-
value: "item",
40-
},
41-
{
42-
label: "Content",
43-
value: "content",
44-
},
45-
{
46-
label: "Cover",
47-
value: "cover",
48-
},
49-
{
50-
label: "Canvas",
51-
value: "canvas",
52-
},
53-
{
54-
label: "Bounds",
55-
value: "bounds",
56-
},
57-
],
58-
},
59-
upscaleMultiplier: {
60-
type: "integer",
61-
label: "Upscale Multiplier",
62-
description: "The factor by which to upscale the image.",
63-
},
64-
generativeUpscale: {
65-
type: "boolean",
66-
label: "Generative Upscale",
67-
description: "Whether to use generative upscale.",
68-
optional: true,
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).",
6911
},
7012
},
7113
methods: {
7214
_baseUrl() {
73-
return "https://deep-image.ai/rest_api";
15+
return "https://deep-image.ai/rest_api/process_result";
7416
},
75-
async _makeRequest(opts = {}) {
76-
const {
77-
$ = this, method = "POST", path = "/", headers, ...otherOpts
78-
} = opts;
79-
return axios($, {
80-
...otherOpts,
81-
method,
82-
url: this._baseUrl() + path,
83-
headers: {
84-
...headers,
85-
"X-API-Key": this.$auth.api_key,
86-
"Content-Type": "application/json",
87-
},
88-
});
17+
_headers() {
18+
return {
19+
"content-type": "application/json",
20+
"x-api-key": `${this.$auth.api_key}`,
21+
};
8922
},
90-
async improveImage({
91-
image, ...opts
23+
makeRequest({
24+
$ = this, ...opts
9225
}) {
93-
return this._makeRequest({
94-
data: {
95-
url: image,
96-
preset: "auto_enhance",
97-
...opts,
98-
},
99-
});
100-
},
101-
async removeBackground({
102-
image, backgroundColor, cropType, ...opts
103-
}) {
104-
return this._makeRequest({
105-
data: {
106-
url: image,
107-
background: {
108-
remove: "auto",
109-
color: backgroundColor === "white"
110-
? "#FFFFFF"
111-
: "transparent",
112-
crop: cropType || "center",
113-
},
114-
...opts,
115-
},
116-
});
117-
},
118-
async upscaleImage({
119-
image, upscaleMultiplier, generativeUpscale, ...opts
120-
}) {
121-
return this._makeRequest({
122-
data: {
123-
url: image,
124-
upscale_parameters: {
125-
type: generativeUpscale
126-
? "text_x4"
127-
: "v1",
128-
},
129-
width: upscaleMultiplier * 1000,
130-
...opts,
131-
},
26+
return axios($, {
27+
method: "POST",
28+
url: this._baseUrl(),
29+
headers: this._headers(),
30+
...opts,
13231
});
13332
},
13433
},

components/deepimage/package.json

Lines changed: 5 additions & 1 deletion
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 <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"fs": "^0.0.1-security"
1418
}
1519
}

0 commit comments

Comments
 (0)