Skip to content

Commit

Permalink
Merge pull request #172 from xbnfz01/master
Browse files Browse the repository at this point in the history
feat: add image enhancement feature
  • Loading branch information
TobyG74 authored Feb 5, 2024
2 parents 5d2e455 + 55e71d0 commit 9f7914a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/commands/converter/enhance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { ConfigCommands } from "../../types/structure/commands";
import { enhanceImage } from "../../utils/scrapers/converter/enhance";

export default <ConfigCommands>{
name: "enhance",
alias: ["remini", "tohd"],
usage: "<reply>",
category: "converter",
description: "Convert image to HD quality",
limit: 4,
cooldown: 10,
isPremium: true,
isProcess: true,
async run({ Chisato, from, message }) {
try {
const { quoted } = message;
const buffer = quoted ? await quoted.download() : await message.download();

if (buffer && (message.type === "imageMessage" || quoted?.type === "imageMessage")) {
const remini = await enhanceImage(buffer, "enhance");
await Chisato.sendImage(from, remini, null, message);
} else {
await Chisato.sendText(
from,
"*「 ! 」* Please reply to the image you want to enhance",
message
);
}
} catch (error) {
console.error(error); // Log the error for debugging
await Chisato.sendText(
from,
"*「 ! 」* An error occurred. Please report it to the bot creator immediately!",
message
);
}
},
};
34 changes: 34 additions & 0 deletions src/utils/scrapers/converter/enhance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import FormData from "form-data";
import axios, { AxiosResponse } from "axios";

export async function enhanceImage(store: Buffer, name: string): Promise<Buffer> {
return new Promise(async (resolve, reject) => {
const validExports = ["enhance", "recolor", "dehaze"];
name = validExports.includes(name) ? name : validExports[0];

const data = new FormData();
const apiUrl = `https://inferenceengine.vyro.ai/${name}`;

data.append("model_version", "1");
data.append("image", Buffer.from(store), {
filename: "enhance_image_body.jpg",
contentType: "image/jpeg",
});

try {
const response: AxiosResponse = await axios.post(apiUrl, data, {
headers: {
"User-Agent": "okhttp/4.9.3",
Connection: "Keep-Alive",
"Accept-Encoding": "gzip",
...data.getHeaders(),
},
responseType: 'arraybuffer',
});

resolve(Buffer.from(response.data));
} catch (error) {
reject(error);
}
});
}

0 comments on commit 9f7914a

Please sign in to comment.