EmoCube commited on
Commit
103daba
·
verified ·
1 Parent(s): 67660e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -36,6 +36,44 @@ def add_noise(image, intensity=25):
36
  noisy_image = Image.fromarray(noisy_array)
37
  return noisy_image
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def query(prompt):
40
  if prompt == "" or prompt == None:
41
  return None
@@ -61,14 +99,7 @@ def query(prompt):
61
  image_bytes = response.content
62
  image = Image.open(io.BytesIO(image_bytes))
63
 
64
- width, height = image.size
65
- new_width = 480
66
- new_height = 640
67
- left = (width - new_width) / 2
68
- top = (height - new_height) / 2
69
- right = (width + new_width) / 2
70
- bottom = (height + new_height) / 2
71
- image = image.crop((left, top, right, bottom))
72
 
73
  # Изменение насыщенности
74
  enhancer = ImageEnhance.Color(image)
 
36
  noisy_image = Image.fromarray(noisy_array)
37
  return noisy_image
38
 
39
+ def resize_and_crop(image, target_width=768, target_height=1024):
40
+ """
41
+ Подгоняет изображение под размер target_width x target_height с сохранением пропорций.
42
+ Если изображение не соответствует соотношению сторон, обрезает его по бокам.
43
+ :param image: Изображение (PIL.Image)
44
+ :param target_width: Целевая ширина
45
+ :param target_height: Целевая высота
46
+ :return: Изображение с измененным размером (PIL.Image)
47
+ """
48
+ # Сохраняем исходные пропорции
49
+ original_width, original_height = image.size
50
+ target_ratio = target_width / target_height
51
+ original_ratio = original_width / original_height
52
+
53
+ # Масштабируем изображение с сохранением пропорций
54
+ if original_ratio > target_ratio:
55
+ # Если изображение шире, чем нужно, обрезаем по бокам
56
+ new_height = target_height
57
+ new_width = int(original_width * (target_height / original_height))
58
+ resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
59
+ left = (new_width - target_width) / 2
60
+ top = 0
61
+ right = (new_width + target_width) / 2
62
+ bottom = target_height
63
+ else:
64
+ # Если изображение уже, чем нужно, обрезаем сверху и снизу
65
+ new_width = target_width
66
+ new_height = int(original_height * (target_width / original_width))
67
+ resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
68
+ left = 0
69
+ top = (new_height - target_height) / 2
70
+ right = target_width
71
+ bottom = (new_height + target_height) / 2
72
+
73
+ # Обрезаем изображение до целевого размера
74
+ cropped_image = resized_image.crop((left, top, right, bottom))
75
+ return cropped_image
76
+
77
  def query(prompt):
78
  if prompt == "" or prompt == None:
79
  return None
 
99
  image_bytes = response.content
100
  image = Image.open(io.BytesIO(image_bytes))
101
 
102
+ image = resize_and_crop(image, 480, 640)
 
 
 
 
 
 
 
103
 
104
  # Изменение насыщенности
105
  enhancer = ImageEnhance.Color(image)