RE_UPLOAD-REBUILD-RESTART
Browse files- utils/get_RGB_image.py +19 -0
utils/get_RGB_image.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
from PIL import Image
|
3 |
+
from urllib.parse import urlparse
|
4 |
+
import requests
|
5 |
+
|
6 |
+
def is_online_file(url: str) -> bool:
|
7 |
+
return urlparse(url).scheme in ["http", "https"]
|
8 |
+
|
9 |
+
def steam_online_file(url: str) -> bytes:
|
10 |
+
return io.BytesIO(requests.get(url, stream=True).content)
|
11 |
+
|
12 |
+
def get_RGB_image(image_or_path: str | Image.Image) -> bytes:
|
13 |
+
if isinstance(image_or_path, str):
|
14 |
+
if is_online_file(image_or_path): # Online
|
15 |
+
content = steam_online_file(image_or_path)
|
16 |
+
image_or_path = Image.open(content)
|
17 |
+
else: # Local
|
18 |
+
image_or_path = Image.open(image_or_path)
|
19 |
+
return image_or_path.convert("RGB")
|