-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcached_property_image_analyzer_github.py
More file actions
41 lines (33 loc) · 1.2 KB
/
cached_property_image_analyzer_github.py
File metadata and controls
41 lines (33 loc) · 1.2 KB
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
from functools import cached_property
from PIL import Image
from pathlib import Path
class ImageAnalyzer:
def __init__(self, image_path: str):
self.image_path = Path(image_path)
self.image = Image.open(self.image_path)
@cached_property
def total_pixels(self):
"""Expensive computation executed ONLY once."""
print("Calculating total pixels...")
width, height = self.image.size
return width * height
@cached_property
def aspect_ratio(self):
"""Another cached expensive calculation."""
print("Calculating aspect ratio...")
w, h = self.image.size
return round(w / h, 2)
def show_info(self):
print(f"- File: {self.image_path.name}")
print(f"- Resolution: {self.image.size}")
print(f"- Total Pixels: {self.total_pixels}")
print(f"- Aspect Ratio: {self.aspect_ratio}")
def main():
analyzer = ImageAnalyzer("sample.jpg")
analyzer.show_info()
# These two calls will NOT re-run the calculations (cached!)
print("\nCalling again...")
print(analyzer.total_pixels)
print(analyzer.aspect_ratio)
if __name__ == "__main__":
main()