-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (107 loc) · 4.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Author: Lasith Manujitha
# Github: @z1nc0r3
# Description: Remove background from images using remove.bg API
# Date: 2024-03-28
import sys, os
parent_folder_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(parent_folder_path)
sys.path.append(os.path.join(parent_folder_path, "lib"))
sys.path.append(os.path.join(parent_folder_path, "plugin"))
from flowlauncher import FlowLauncher
import requests
from pathlib import Path
import re
class Remover(FlowLauncher):
def is_valid_file(self, file):
supported = [".jpg", ".jpeg", ".png", ".pjp", ".pjpeg"]
if Path(file).suffix.lower() not in supported:
return False
return os.path.isfile(file)
def is_valid_url(self, str):
regex = (
"((http|https)://)(www.)?"
+ "[a-zA-Z0-9@:%._\\+~#?&//=]"
+ "{1,256}\\.[a-z]"
+ "{2,6}\\b([-a-zA-Z0-9@:%"
+ "._\\+~#?&//=]*)"
)
p = re.compile(regex)
if re.search(p, str):
return True
else:
return False
def query(self, query):
output = []
if len(query.strip()) == 0:
output.append(
{"Title": "Enter a file path or URL", "IcoPath": "Images/app.png"}
)
else:
file = query.strip()
file = file.replace('"', "")
save_path = os.path.expanduser("~/Downloads")
if self.is_valid_file(file):
file_name = Path(file).stem
files = {"image_file": open(file, "rb")}
data = {"size": "auto"}
elif self.is_valid_url(file):
file_name = os.path.basename(file).split("?")[0]
files = None
if requests.head(file).status_code != 200:
file = file.split("?")[0]
data = {"image_url": file}
else:
output.append(
{
"Title": "Invalid file path or URL",
"IcoPath": "Images/error.png",
}
)
return output
response = requests.post(
"https://api.remove.bg/v1.0/removebg",
files=files,
data=data,
headers={"X-Api-Key": "jNAnhZ5d5DFU4bRQZD7YhaYf"},
)
if response.status_code == 200:
file_name = file_name.replace(" ", "_")
with open(f"{save_path}/{file_name}.png", "wb") as out:
out.write(response.content)
output.append(
{
"Title": "Click to open the image location",
"SubTitle": "Background removed successfully",
"IcoPath": "Images/open_dir.png",
"JsonRPCAction": {
"method": "open_dir",
"parameters": [save_path],
},
}
)
output.append(
{
"Title": "Click to open the image",
"SubTitle": "Background removed successfully",
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "open",
"parameters": [f"{save_path}/{file_name}.png"],
},
}
)
else:
output.append(
{
"Title": "Failed to remove background",
"SubTitle": "Please check the file or URL and try again",
"IcoPath": "Images/error.png",
}
)
return output
def open(self, file_name):
os.system(f"start {file_name}")
def open_dir(self, save_path):
os.system(f"start {save_path}")
if __name__ == "__main__":
Remover()