mstutils 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mstutils-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mstutils
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: This package contains some useful functions for image processing and torch gpu releasing. To run in Google Colab
|
|
5
|
+
Author: mstytn
|
|
6
|
+
Author-email: mstytn <mustafayatagan@gmail.com>
|
|
7
|
+
Requires-Dist: pillow>=10.0.0
|
|
8
|
+
Requires-Dist: requests>=2.31.0
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
mstutils-0.1.0/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mstutils"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "This package contains some useful functions for image processing and torch gpu releasing. To run in Google Colab"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "mstytn", email = "mustafayatagan@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"pillow>=10.0.0",
|
|
12
|
+
"requests>=2.31.0",
|
|
13
|
+
]
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["uv_build>=0.10.12,<0.11.0"]
|
|
16
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ImageHelpers:
|
|
10
|
+
def __init__(self) -> None:
|
|
11
|
+
# Lazy imports kept inside methods so module import never fails without dependencies
|
|
12
|
+
from PIL import Image
|
|
13
|
+
self._Image = Image
|
|
14
|
+
self.images: list[Image.Image] = []
|
|
15
|
+
|
|
16
|
+
def add_image(self, image: Image.Image) -> None:
|
|
17
|
+
"""Appends an existing PIL Image object to the internal list."""
|
|
18
|
+
self.images.append(image)
|
|
19
|
+
|
|
20
|
+
def load_images_from_path(self, paths: list[str]) -> dict[str, bool | int]:
|
|
21
|
+
"""Loads multiple images from a list of file paths and returns load statistics."""
|
|
22
|
+
stat: dict[str, bool | int] = {
|
|
23
|
+
"all": False,
|
|
24
|
+
"total": len(paths),
|
|
25
|
+
"loaded": 0,
|
|
26
|
+
"failed": 0,
|
|
27
|
+
}
|
|
28
|
+
for p in paths:
|
|
29
|
+
try:
|
|
30
|
+
# Copy image in memory so file handles do not stay locked
|
|
31
|
+
with self._Image.open(p) as img:
|
|
32
|
+
self.images.append(img.copy())
|
|
33
|
+
stat["loaded"] += 1
|
|
34
|
+
except Exception as e:
|
|
35
|
+
print(f"Failed to load {p}: {e}")
|
|
36
|
+
stat["failed"] += 1
|
|
37
|
+
|
|
38
|
+
stat["all"] = stat["loaded"] == stat["total"]
|
|
39
|
+
return stat
|
|
40
|
+
|
|
41
|
+
def load_image_from_url(self, url: str) -> Image.Image:
|
|
42
|
+
"""Downloads an image from a URL, stores it, and returns the PIL Image."""
|
|
43
|
+
import requests
|
|
44
|
+
|
|
45
|
+
response = requests.get(url, stream=True, timeout=15)
|
|
46
|
+
response.raise_for_status()
|
|
47
|
+
img = self._Image.open(response.raw)
|
|
48
|
+
img.load() # Force download completion before stream closes
|
|
49
|
+
self.images.append(img)
|
|
50
|
+
return img
|
|
51
|
+
|
|
52
|
+
def save_images(self, path: str = "./") -> None:
|
|
53
|
+
"""Saves all stored images sequentially as PNG files."""
|
|
54
|
+
target_dir = Path(path)
|
|
55
|
+
target_dir.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
for i, image in enumerate(self.images):
|
|
57
|
+
image.save(target_dir / f"{i}.png")
|
|
58
|
+
|
|
59
|
+
def load_images_from_disk(
|
|
60
|
+
self, path: str = "./", extensions: list[str] | None = None
|
|
61
|
+
) -> None:
|
|
62
|
+
"""Loads all matching images from a directory."""
|
|
63
|
+
if extensions is None:
|
|
64
|
+
extensions = ["jpg", "png", "jpeg"]
|
|
65
|
+
|
|
66
|
+
target_dir = Path(path)
|
|
67
|
+
for ext in extensions:
|
|
68
|
+
for file_path in target_dir.glob(f"*.{ext}"):
|
|
69
|
+
with self._Image.open(file_path) as img:
|
|
70
|
+
self.images.append(img.copy())
|
|
71
|
+
|
|
72
|
+
def load_image_from_disk(self, path: str) -> None:
|
|
73
|
+
"""Loads a single image from disk."""
|
|
74
|
+
with self._Image.open(path) as img:
|
|
75
|
+
self.images.append(img.copy())
|
|
File without changes
|