Commit 00e3e9c2 authored by Danila's avatar Danila
Browse files

add main.py

parent d50fe356
No related merge requests found
Showing with 100 additions and 0 deletions
+100 -0
main.py 0 → 100644
from PIL import Image
import os
from jinja2 import Template
import math
from filesystem import images_folder
def distance_coefficient(h, initial_h):
"""
Вычисляет коэффициент разницы в цветовом тоне между двумя углами в радианах.
h: Угол первого цвета.
initial_h: Угол самого тёплого цвета.
return: Коэффициент разницы в цветовом тоне.
"""
h=(h/255)*360
h_rad = math.radians(h)
initial_h_rad = math.radians(initial_h)
difference = abs(initial_h_rad - h_rad)
if difference > math.pi:
difference = 2 * math.pi - difference
coefficient = 1 - difference / math.pi
return coefficient
def is_warm_hsv2(image_path):
"""
Определяет, является ли изображение теплым в цветовом представлении HSV
image_path: Путь к изображению.
return: Количество теплых пикселей.
"""
img = Image.open(image_path)
img = img.convert('HSV')
warm_count = 0
img = img.resize((1, 1))
h, s, v = img.getpixel((0, 0))
warm_count += distance_coefficient(h, 30)
return warm_count
def is_warm_hsv(image_path):
"""
Определяет, является ли изображение теплым в цветовом представлении HSV.
param image_path: Путь к изображению.
return: Количество теплых пикселей.
"""
img = Image.open(image_path)
img = img.convert('HSV')
warm_count = 0
img = img.resize((1, 1))
h, s, v = img.getpixel((0, 0))
print(image_path)
print(h)
if (((h >= 0 and h <= 122) or (h >= 228 and h <= 255)) and s>50) :
warm_count += 1
return warm_count
def generate_html(images):
"""
Генерирует HTML-страницу на основе шаблона и списка изображений.
images: Список изображений.
return:Сгенерированный HTML-код.
"""
template = Template(open('template.html').read())
photos = images
html_output = template.render(photos=photos)
with open('output.html', 'w') as file:
file.write(html_output)
def analyze_images(images_folder):
"""
Анализирует изображения в указанной папке.
:param images_folder: Путь к папке с изображениями.
:return: Отсортированный список кортежей (имя_файла, коэффициент_тепла).
"""
warm_images = []
warm_images_sorted = {}
for filename in os.listdir(images_folder):
if filename.endswith(".jpg") or filename.endswith(".png"):
if is_warm_hsv(os.path.join(images_folder, filename)) != 0:
warm_images.append(filename)
print(warm_images)
for filename in warm_images:
if filename.endswith(".jpg") or filename.endswith(".png"):
if is_warm_hsv2(os.path.join(images_folder, filename)) > 0.4:
warm_images_sorted[filename] = is_warm_hsv2(os.path.join(images_folder, filename))
print(warm_images)
sorted_images = sorted(warm_images_sorted.items(), key=lambda item: item[1],reverse=True)
print(sorted_images)
return sorted_images
images = analyze_images(images_folder)
html = generate_html(images)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment