USER
System: Write a script in python with error checking (try except) and use logger (logger.debug and logger.info). You can use hacker tricks. Try to write code simply. Choose data structures and algorithms that are most efficient. If something is unclear, ask questions. Write a script that will
User: openpyxl сделать в ячейке жирный шрифт
Assistant: To achieve the task of formatting a cell with bold font using the `openpyxl` library in Python, I'll provide a script that includes error checking using `try-except` blocks and logging using the `logging` module. This script will demonstrate creating a new Excel file, writing some data to a cell, and applying bold formatting to that cell.
Make sure to install `openpyxl` if you haven't already:
```bash
pip install openpyxl
```
Here’s the script:
```python
import logging
from openpyxl import Workbook
from openpyxl.styles import Font
# Setting up the logger
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def create_excel_with_bold_cell(file_name, sheet_name, cell, value):
try:
# Create a new workbook and select the active worksheet
logger.info("Creating a new Excel workbook.")
workbook = Workbook()
sheet = workbook.active
sheet.title = sheet_name
# Set the value of the specified cell
sheet[cell] = value
logger.debug(f"Set cell {cell} with value '{value}'.")
# Apply bold formatting to the specified cell
bold_font = Font(bold=True)
sheet[cell].font = bold_font
logger.debug(f"Applied bold formatting to cell {cell}.")
# Save the workbook to the specified file
workbook.save(file_name)
logger.info(f"Workbook saved successfully as '{file_name}'.")
except Exception as e:
logger.error(f"An error occurred: {e}")
if __name__ == "__main__":
# Example usage
create_excel_with_bold_cell('example.xlsx', 'Sheet1', 'A1', 'Hello, World!')
```
### Explanation of the Script:
1. **Logging Setup**: We set up a logger that logs messages to the console. The logging level is set to `DEBUG` to capture all events.
2. **Function Definition**: The function `create_excel_with_bold_cell` takes parameters for the file name, sheet name, cell address, and cell value.
3. **Creating a Workbook**: A new workbook is created, and the active sheet is selected. The sheet's title is set.
4. **Setting Cell Value**: The specified cell is set to the provided value, and a debug message logs this action.
5. **Bold Font Styling**: The `Font` class from `openpyxl.styles` is used to create a bold font, which is then applied to the cell.
6. **Saving the Workbook**: Finally, the workbook is saved with a corresponding message logged.
7. **Error Handling**: All operations are wrapped in a `try-except` block to capture and log any errors.
8. **Main Execution Block**: The script will run the function with sample parameters if executed as the main module.
You can adjust the parameters in the `if __name__ == "__main__":` block to customize the output as needed.
User: openpyxl сделать в ячейке жирный шрифт и на 2 пункта больше, чем был
Assistant: To modify the previous script to not only set a cell to bold but also increase its font size by 2 points compared to its original size, we can adjust the `Font` object accordingly. Below is the updated script that implements this functionality:
```python
import logging
from openpyxl import Workbook
from openpyxl.styles import Font
# Setting up the logger
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def create_excel_with_bold_and_larger_font(file_name, sheet_name, cell, value, original_font_size):
try:
# Create a new workbook and select the active worksheet
logger.info("Creating a new Excel workbook.")
workbook = Workbook()
sheet = workbook.active
sheet.title = sheet_name
# Set the value of the specified cell
sheet[cell] = value
logger.debug(f"Set cell {cell} with value '{value}'.")
# Apply bold formatting and increase font size
new_font_size = original_font_size + 2
bold_font = Font(bold=True, size=new_font_size)
sheet[cell].font = bold_font
logger.debug(f"Applied bold formatting and increased font size to {new_font_size} for cell {cell}.")
# Save the workbook to the specified file
workbook.save(file_name)
logger.info(f"Workbook saved successfully as '{file_name}'.")
except Exception as e:
logger.error(f"An error occurred: {e}")
if __name__ == "__main__":
# Example usage
create_excel_with_bold_and_larger_font('example.xlsx', 'Sheet1', 'A1', 'Hello, World!', original_font_size=11)
```
### Explanation of the Changes:
1. **Function Name**: The function is renamed to `create_excel_with_bold_and_larger_font` to reflect its new functionality.
2. **Original Font Size Parameter**: An additional parameter `original_font_size` is added to the function to specify the starting font size.
3. **Font Size Calculation**: The new font size is calculated by adding 2 to the `original_font_size`.
4. **Font Object Update**: The `Font` object is updated to include the new font size along with the bold attribute.
### Usage:
- When calling the function, you can specify the original font size. In the example provided, it is set to 11, so the resulting font size in cell A1 will be 13 (11 + 2). Adjust this value as needed for your specific use case.
User: openpyxl, открыть test.xlsx, получить размер шрифта ячейки A1 и сделать в ячейке жирный шрифт и на 2 пункта больше, чем был
Assistant: To open an existing Excel file (e.g., `test.xlsx`), retrieve the font size of cell A1, and then update that cell to have bold formatting and an increased font size by 2 points, you can use the following script:
Make sure to have the `openpyxl` library installed and the file `test.xlsx` in the same directory as the script. Here is how you can perform the specified task:
```python
import logging
from openpyxl import load_workbook
from openpyxl.styles import Font
# Setting up the logger
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def update_cell_font_in_excel(file_name, sheet_name, cell):
try:
# Load the existing workbook and select the specified sheet
logger.info(f"Opening the workbook '{file_name}'.")
workbook = load_workbook(file_name)
sheet = workbook[sheet_name]
# Obtain the original font size from the specified cell
original_cell = sheet[cell]
original_font_size = original_cell.font.size if original_cell.font else 11 # Default to 11 if not set
logger.debug(f"Original font size of cell {cell} is {original_font_size}.")
# Increase the font size by 2
new_font_size = original_font_size + 2
logger.info(f"Increasing font size for cell {cell} to {new_font_size} and setting bold.")
# Apply bold formatting and updated font size
bold_font = Font(bold=True, size=new_font_size)
sheet[cell].font = bold_font
# Save the workbook
workbook.save(file_name)
logger.info(f"Workbook '{file_name}' saved successfully.")
except Exception as e:
logger.error(f"An error occurred: {e}")
if __name__ == "__main__":
# Example usage
update_cell_font_in_excel('test.xlsx', 'Sheet1', 'A1')
```
### Explanation of the Script:
1. **Imports**: We import necessary modules from `openpyxl` and set up logging.
2. **Function Definition**: The function `update_cell_font_in_excel` takes the file name, sheet name, and cell address as parameters.
3. **Loading Workbook**: The function uses `load_workbook` to open the specified Excel file and selects the required sheet.
4. **Retrieve Original Font Size**: It retrieves the current font size of the specified cell. If the font is not set, it defaults to a size of 11.
5. **Adjust Font Size**: The new font size is computed by adding 2 to the original font size.
6. **Updating the Cell**: A new `Font` object is created with bold formatting and the increased font size, then applied to the specified cell.
7. **Saving Changes**: The workbook is saved, and the action is logged.
8. **Error Handling**: The code is wrapped in a `try-except` block to handle any errors that may arise during execution.
### Usage:
- Replace `'test.xlsx'` and `'Sheet1'` as needed to point to your actual Excel file and sheet name.
- Run the script, and it will modify the cell `A1` as specified. Ensure that `test.xlsx` exists before running the script to avoid a file not found error.
User: получить прогноз погоды на завтра: давление, температура и осадки. На вход название города, широта и долгота
Assistant: Привет! Чтобы получить прогноз погоды на завтра, мне нужно знать название города, а также его широту и долготу. Пожалуйста, предоставь эту информацию, и я постараюсь помочь тебе с прогнозом!
User: сделай функцию получить прогноз погоды на завтра: давление, температура и осадки. На вход название города, широта и долгота, ответ в виде словаря
Assistant: Конечно! Вот пример функции на Python, которая может получить прогноз погоды на завтра, включая давление, температуру и осадки. Для этого можно использовать API, например, OpenWeatherMap. Вот как это может выглядеть:
```python
import requests
def get_weather_forecast(city_name, latitude, longitude):
api_key = 'YOUR_API_KEY' # Замените на ваш API ключ
url = f'http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
forecast = {
'city': city_name,
'temperature': data['main']['temp'],
'pressure': data['main']['pressure'],
'precipitation': data.get('rain', {}).get('1h', 0) # Осадки за последний час
}
return forecast
else:
return {'error': 'Не удалось получить данные о погоде'}
# Пример использования
city = 'Москва'
lat = 55.7558
lon = 37.6173
weather_forecast = get_weather_forecast(city, lat, lon)
print(weather_forecast)
```
Не забудьте заменить `'YOUR_API_KEY'` на ваш собственный ключ API, который можно получить, зарегистрировавшись на сайте OpenWeatherMap. Эта функция вернет словарь с прогнозом погоды на основе введенных данных. Если возникнут проблемы с запросом, она вернет сообщение об ошибке. Надеюсь, это поможет!
User: open-meteo.com сделай функцию получить прогноз погоды на завтра: давление, температура и осадки. На вход название города, широта и долгота, ответ в виде словаря
Assistant: Для получения прогноза погоды на завтра с использованием API Open-Meteo, вы можете использовать следующую функцию на Python. В этом примере мы будем передавать название города, широту и долготу, а функция вернет прогноз погоды в виде словаря.
Вот как это можно реализовать:
```python
import requests
def get_weather_forecast(city_name, latitude, longitude):
# URL для API Open-Meteo
url = f'https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,pressure_msl&timezone=auto'
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
tomorrow_forecast = data['daily'][0] # Берем прогноз на завтрашний день
forecast = {
'city': city_name,
'temperature_max': tomorrow_forecast['temperature_2m_max'],
'temperature_min': tomorrow_forecast['temperature_2m_min'],
'precipitation': tomorrow_forecast['precipitation_sum'],
'pressure': tomorrow_forecast['pressure_msl']
}
return forecast
else:
return {'error': 'Не удалось получить данные о погоде', 'status_code': response.status_code}
except Exception as e:
return {'error': str(e)}
# Пример использования
city = 'Москва'
lat = 55.7558
lon = 37.6173
weather_forecast = get_weather_forecast(city, lat, lon)
print(weather_forecast)
```
### Объяснение работы функции:
1. **API URL**: Формируется URL запроса к API Open-Meteo с указанием широты и долготы. Запрашиваются максимальная и минимальная температура, сумма осадков и давление.
2. **Отправка запроса**: Используется функция `requests.get` для отправки запроса к API.
3. **Проверка статуса ответа**: Если статус ответа равен 200, то данные обрабатываются.
4. **Получение данных**: Из ответа извлекается прогноз на следующий день, и формируется словарь, который содержит:
- `city`: название города
- `temperature_max`: максимальная температура
- `temperature_min`: минимальная температура
- `precipitation`: сумма осадков
- `pressure`: атмосферное давление
5. **Обработка ошибок**: Если запрос не удался или возникла ошибка, функция возвращает сообщение об ошибке в виде словаря.
### Пример использования:
Данная функция может быть вызвана с любым названием города, широтой и долготой. В приведенном примере используется Москва. Результат будет выведен на экран в виде словаря с погодными данными.
Убедитесь, что у вас установлен модуль `requests`. Вы можете установить его с помощью команды:
```bash
pip install requests
```
Теперь вы можете использовать эту функцию для получения прогноза погоды с Open-Meteo!
User: open-meteo.com сделай функцию получить прогноз погоды на день (дату) 31/12/2023: давление, температура и осадки. На вход название города, широта и долгота, ответ в виде словаря
Assistant: Для получения прогноза погоды на конкретную дату (например, 31 декабря 2023 года) с использованием API Open-Meteo, вы можете использовать следующий код. В этом примере функция будет принимать название города, широту, долготу и дату, а затем возвращать прогноз погоды в виде словаря.
Вот как это можно реализовать:
```python
import requests
from datetime import datetime
def get_weather_forecast_on_date(city_name, latitude, longitude, date):
# Форматируем дату в нужный формат (YYYY-MM-DD)
formatted_date = date.strftime('%Y-%m-%d')
# URL для API Open-Meteo
url = f'https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,pressure_msl&timezone=auto'
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
daily_data = data['daily']
# Ищем данные для нужной даты
for i, day in enumerate(daily_data):
if day['time'] == formatted_date:
forecast = {
'city': city_name,
'date': formatted_date,
'temperature_max': day['temperature_2m_max'],
'temperature_min': day['temperature_2m_min'],
'precipitation': day['precipitation_sum'],
'pressure': day['pressure_msl']
}
return forecast
return {'error': 'Данные для указанной даты не найдены'}
else:
return {'error': 'Не удалось получить данные о погоде', 'status_code': response.status_code}
except Exception as e:
return {'error': str(e)}
# Пример использования
city = 'Москва'
lat = 55.7558
lon = 37.6173
date = datetime(2023, 12, 31) # 31 декабря 2023 года
weather_forecast = get_weather_forecast_on_date(city, lat, lon, date)
print(weather_forecast)
```
### Объяснение работы функции:
1. **Форматирование даты**: Дата передается в формате `datetime`, и мы форматируем ее в строку формата `YYYY-MM-DD`, чтобы использовать в запросе.
2. **API URL**: Формируется URL запроса к API Open-Meteo с указанием широты и долготы. Запрашиваются максимальная и минимальная температура, сумма осадков и давление.
3. **Отправка запроса**: Используется функция `requests.get` для отправки запроса к API.
4. **Проверка статуса ответа**: Если статус ответа равен 200, то данные обрабатываются.
5. **Поиск данных для нужной даты**: Мы перебираем данные о погоде и ищем информацию для указанной даты. Если данные найдены, формируется словарь с прогнозом.
6. **Обработка ошибок**: Если запрос не удался или возникла ошибка, функция возвращает сообщение об ошибке в виде словаря.
### Пример использования:
В приведенном примере используется Москва и дата 31 декабря 2023 года. Результат будет выведен на экран в виде словаря с погодными данными.
Убедитесь, что у вас установлен модуль `requests`. Вы можете установить его с помощью команды:
```bash
pip install requests
```
Теперь вы можете использовать эту функцию для получения прогноза погоды на конкретную дату с Open-Meteo!
User: прокомментируй код, найди возможные проблемы
```py
import base64
import os
from io import BytesIO
import PyPDF2
import cv2
import numpy as np
import tqdm
from loguru import logger
from wand.image import Image
import json
import re
from fractions import Fraction
import math
def foo_campare(a1,a2:float)->bool:
return math.isclose(a1,a2,rel_tol=1e-08, abs_tol=1e-08)
def mm_to_inches(mm:float, precision=0.25)->float:
if isinstance(mm, str):
mm1=float(mm)
else:
mm1=mm
inches = mm1 / 25.4 # Перевод миллиметров в дюймы
# Округление до ближайшего кратного precision
rounded_inches = round(inches / precision) * precision
if abs(inches)>0.0001 and abs(rounded_inches-inches)/inches>0.018:
logger.warning(f'mm={mm}; inches={inches} <> rounded_inches={rounded_inches}')
return rounded_inches
def format_float_str2(num:float)->str:
# Форматируем число до двух знаков после запятой
formatted = f"{num:.3f}"
# Убираем нули и точку, если это целое число
result=formatted
while '.' in result and result.endswith("0"):
result = result[:-1]
if result.endswith("."):
return result[:-1]
return result
assert format_float_str2(0.250001)=="0.25"
assert format_float_str2(0.200001)=="0.2"
assert format_float_str2(-0.200001)=="-0.2"
assert format_float_str2(10.00001)=="10"
assert format_float_str2(0.00001)=="0"
assert format_float_str2(0)=="0"
assert format_float_str2(mm_to_inches(100, precision=1)) == "4"
def deprecated_normalize_inches(input_str):
"""
Функция для проверки и нормализации записи в дюймах.
Преобразует записи вида '3.3/4"' в нормализованный вид с десятичными долями: '3.75"'.
Параметры:
- input_str (str): Строка с записью дюймов в формате 'X.Y/Z"' или 'X"' или 'X.Y"'.
Возвращает:
- str: Нормализованная строка в формате 'X.Y"', если вход корректен.
Исключения:
- ValueError: Если формат строки неверен или дробь не нормализуется до 0.25, 0.5 или 0.75.
"""
# Регулярное выражение для формата с целым, дробной частью и необязательной дробью
pattern = r'^(\d+)(?:\.(\d+)/(\d+))?"$'
match = re.match(pattern, input_str)
if not match:
raise ValueError("Неверный формат записи. Используйте формат 'X.Y/Z\"'.")
# Парсинг значений из строки
whole_part = int(match.group(1))
numerator = int(match.group(2)) if match.group(2) else 0
denominator = int(match.group(3)) if match.group(3) else 1 # Если нет дробной части, делитель = 1
# Вычисление дробного значения
fraction_value = whole_part + Fraction(numerator, denominator)
# Приведение к десятичному виду
decimal_value = float(fraction_value)
# Округление до допустимых значений
allowed_fractions = [0.0, 0.25, 0.5, 0.75]
rounded_fraction = round(decimal_value - whole_part, 2) # Оставляем только дробную часть и округляем до 2 знаков
if rounded_fraction not in allowed_fractions:
raise ValueError("Дробная часть должна быть одной из 0.25, 0.5 или 0.75.")
# Формируем нормализованное значение
normalized_value = format_float_str2(whole_part + rounded_fraction)
return f'{normalized_value}"'
assert deprecated_normalize_inches('3"') == '3"'
assert deprecated_normalize_inches('3.3/4"') == '3.75"'
assert deprecated_normalize_inches('3.1/2"') == '3.5"'
# assert deprecated_normalize_inches('2.9/16"') == '2.5"'
def feet_inches_to_mm(feet_inches: str) -> float:
"""
Converts a measurement in feet and inches to millimeters.
Args:
feet_inches (str): A string representing measurement in the format "X'Y.Z/W\"" where:
- X is the feet part (optional),
- Y is the inches part (optional),
- Z/W is the fractional inches part (optional).
Returns:
float: The measurement converted to millimeters.
"""
feet = 0 # Default feet to 0 if not provided
whole_inches = 0
fractional_inches = 0.0
# Check if the input contains feet (indicated by the presence of `'`)
if "'" in feet_inches:
feet_part, inches_part = feet_inches.split("'")
feet = int(feet_part.strip())
else:
inches_part = feet_inches # If no feet part, consider the entire input as inches part
# Process the inches part, handling whole and fractional inches
if inches_part:
inches_part = inches_part.replace('"', '').strip() # Remove the trailing quote
if '.' in inches_part and '/' not in inches_part: # Check for mixed decimal/fractional inches
whole_inches, fractional_part = inches_part.split('.')
whole_inches = int(whole_inches.strip())
fractional_inches = float('0.'+fractional_part)
elif '/' in inches_part: # Check for pure fractional inches (e.g., "1/4")
try:
whole_inches, fractional_part = inches_part.split('.')
except:
whole_inches='0'
fractional_part= inches_part
whole_inches = int(whole_inches.strip())
fractional_inches = float(Fraction(fractional_part))
else: # Pure integer inches
whole_inches = int(inches_part.strip())
# Convert feet and inches to millimeters
feet_mm = feet * 304.8
inches_mm = (whole_inches + fractional_inches) * 25.4
total_mm = feet_mm + inches_mm
return total_mm
# Test cases based on the examples provided
assert foo_campare( feet_inches_to_mm('2.5"') , 2.5*25.4)
assert foo_campare(feet_inches_to_mm('1\'') , 304.8)
assert foo_campare(feet_inches_to_mm('1\'2"') , 304.8 + 2*25.4)
assert foo_campare(feet_inches_to_mm('1\'2.1/2"') , 304.8 + (2+1/2)*25.4)
assert foo_campare(feet_inches_to_mm('2"') , 2*25.4)
assert foo_campare(feet_inches_to_mm('2.9/16"') , (2+9/16)*25.4)
assert foo_campare(feet_inches_to_mm('0.1/8"') , (0+1/8)*25.4)
assert foo_campare(feet_inches_to_mm('1/8"') , (1/8)*25.4)
def convert_raw_size_to_inch(raw_size0, precision=0.25):
if isinstance(raw_size0, (int, float)):
raw_size=str(raw_size0)
elif isinstance(raw_size0, str):
raw_size = raw_size0
else:
raw_size = str(raw_size0)
if ' ' in raw_size:
k1,k2 = raw_size.split(' ')
return convert_raw_size_to_inch(k1,precision)+'x'+convert_raw_size_to_inch(k2,precision)
elif '"' in raw_size or "'" in raw_size:
mm=feet_inches_to_mm(raw_size)
return format_float_str2(mm_to_inches(mm, precision))+ '"'
else:
return format_float_str2(mm_to_inches(raw_size, precision))+ '"'
assert convert_raw_size_to_inch('0')=='0"'
assert convert_raw_size_to_inch('100')=='4"'
assert convert_raw_size_to_inch('80', precision=0.5) == '3"'
assert convert_raw_size_to_inch('3.3/4"', precision=0.05) == '3.75"'
assert convert_raw_size_to_inch('3/8" 3.3/4"', precision=0.125) == '0.375"x3.75"'
assert convert_raw_size_to_inch('100 80', precision=0.5) == '4"x3"'
def load_json(file_path):
"""
Загрузить объект из JSON файла.
:param file_path: Путь к JSON файлу
:return: Объект Python, содержащий данные из JSON файла
"""
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return data
def png_to_jpeg_base64(png_filename: str, quality: int = 95) -> str:
# Открываем изображение в формате PNG
with Image(filename=png_filename) as img:
# Преобразуем изображение в формат JPEG с заданным качеством
img.format = 'jpeg'
img.compression_quality = quality
# Сохраняем изображение в буфер памяти
with BytesIO() as buffer:
img.save(file=buffer)
# Возвращаем указатель буфера на начало
buffer.seek(0)
# Получаем байты изображения
jpeg_bytes = buffer.read()
# Кодируем байты изображения в строку Base64
jpeg_base64 = base64.b64encode(jpeg_bytes).decode('utf-8')
return jpeg_base64
def compare_numbers(a, b, relative_tolerance=1e-9):
"""
Сравнивает два числа с относительной точностью.
:param a: Первое число (может быть целым или дробным).
:param b: Второе число (может быть целым или дробным).
:param relative_tolerance: Допустимая относительная погрешность.
:return: True, если числа равны с заданной точностью, иначе False.
"""
# Если оба числа равны (сразу вернуть True)
if a == b:
return True
# Проверяем относительную разницу
relative_difference = abs(a - b) / max(abs(a), abs(b))
return relative_difference <= relative_tolerance
def image_red_expand(filename_png, output_png, radius=5):
# Загрузка изображения
image = cv2.imread(filename_png)
# Определение диапазона красного цвета в RGB
# Красный цвет: высокий Red, низкий Green и Blue
lower_red = np.array([50, 0, 0]) # Нижний порог для красного
upper_red = np.array([255, 100, 100]) # Верхний порог для красного
# Создание маски на основе красных областей в RGB
red_mask = cv2.inRange(image, lower_red, upper_red)
# Применение дилатации (dilation) к красным областям для их расширения
kernel = np.ones((radius, radius), np.uint8) # Ядро 5x5 по умолчанию
dilated_mask = cv2.dilate(red_mask, kernel, iterations=1)
# Извлечение расширенных красных областей
red_areas = cv2.bitwise_and(image, image, mask=dilated_mask)
# Извлечение всех остальных областей, которые не являются красными
inverse_mask = cv2.bitwise_not(red_mask) # Инвертируем оригинальную маску
background = cv2.bitwise_and(image, image, mask=inverse_mask)
# Комбинируем увеличенные красные области с фоном
result_image = cv2.add(background, red_areas)
# Сохраняем результат
cv2.imwrite(output_png, result_image)
def image_colors_expand(filename_png, output_png, radius=5):
if radius<2:
logger.warning(f'image_colors_expand radius={radius}')
return -1
# Загрузка изображения
image = cv2.imread(filename_png)
# Преобразование изображения в цветовое пространство Lab
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
# Разделяем каналы Lab
L, A, B = cv2.split(lab_image)
# Преобразуем A и B каналы в float32 для корректных математических операций
A = A.astype(np.float32)
B = B.astype(np.float32)
# Вычисляем интенсивность цвета
color_magnitude = np.sqrt(A ** 2 + B ** 2)
# Создаем маску для областей с высокой интенсивностью цвета
color_intensity_mask = cv2.inRange(color_magnitude, 50, 255)
# Инвертируем маску для выделения только цветных областей (исключаем черное и белое)
non_black_white_mask = cv2.bitwise_not(color_intensity_mask)
# Подсчитаем количество ненулевых (не черных) пикселей
non_black_pixels = cv2.countNonZero(non_black_white_mask)
if non_black_pixels>10:
logger.debug(f'Non black pixels {non_black_pixels} ({filename_png})')
# Первое dilate для создания dilated_mask
kernel = np.ones((5, 5), np.uint8)
dilated_mask = cv2.dilate(non_black_white_mask, kernel, iterations=1)
# Применяем non_black_white_mask к изображению и делаем второе dilate
selected_areas = cv2.bitwise_and(image, image, mask=non_black_white_mask)
dilated_selected_areas = cv2.dilate(selected_areas, kernel, iterations=1)
# Комбинируем результат с исходным изображением по маске dilated_mask
result_image = np.where(dilated_mask[:, :, np.newaxis] != 0, dilated_selected_areas, image)
# cv2.imwrite('color_intensity_mask.png', color_intensity_mask)
# cv2.imwrite('selected_areas.png', selected_areas)
# cv2.imwrite('dilated_mask.png', dilated_mask)
# cv2.imwrite('dilated_selected_areas.png', dilated_selected_areas)
# Сохраняем результат
cv2.imwrite(output_png, result_image)
return non_black_pixels
def split_pdf(input_pdf, output_folder):
# Открываем исходный PDF файл
with open(input_pdf, 'rb') as pdf_file:
reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(reader.pages)
# Проходим по каждой странице
for page_num in tqdm.tqdm(range(num_pages), desc=f'Split {input_pdf} pages'):
writer = PyPDF2.PdfWriter()
page = reader.pages[page_num]
writer.add_page(page)
output_filename = os.path.join(output_folder, f"tmp_page_{page_num + 1}.pdf")
with open(output_filename, 'wb') as output_pdf:
writer.write(output_pdf)
logger.trace(f"Saved {page_num + 1} as {output_filename}")
return num_pages
```
User: прокомментируй
```
def mm_to_inches(mm:float, precision=0.25)->float:
if isinstance(mm, str):
mm1=float(mm)
else:
mm1=mm
inches = mm1 / 25.4 # Перевод миллиметров в дюймы
# Округление до ближайшего кратного precision
rounded_inches = round(inches / precision) * precision
if abs(inches)>0.0001 and abs(rounded_inches-inches)/inches>0.018:
logger.warning(f'mm={mm}; inches={inches} <> rounded_inches={rounded_inches}')
return rounded_inches
```
```py
import base64
import os
from io import BytesIO
import PyPDF2
import cv2
import numpy as np
import tqdm
from loguru import logger
from wand.image import Image
import json
import re
from fractions import Fraction
import math
def foo_campare(a1,a2:float)->bool:
return math.isclose(a1,a2,rel_tol=1e-08, abs_tol=1e-08)
def mm_to_inches(mm:float, precision=0.25)->float:
if isinstance(mm, str):
mm1=float(mm)
else:
mm1=mm
inches = mm1 / 25.4 # Перевод миллиметров в дюймы
# Округление до ближайшего кратного precision
rounded_inches = round(inches / precision) * precision
if abs(inches)>0.0001 and abs(rounded_inches-inches)/inches>0.018:
logger.warning(f'mm={mm}; inches={inches} <> rounded_inches={rounded_inches}')
return rounded_inches
def format_float_str2(num:float)->str:
# Форматируем число до двух знаков после запятой
formatted = f"{num:.3f}"
# Убираем нули и точку, если это целое число
result=formatted
while '.' in result and result.endswith("0"):
result = result[:-1]
if result.endswith("."):
return result[:-1]
return result
assert format_float_str2(0.250001)=="0.25"
assert format_float_str2(0.200001)=="0.2"
assert format_float_str2(-0.200001)=="-0.2"
assert format_float_str2(10.00001)=="10"
assert format_float_str2(0.00001)=="0"
assert format_float_str2(0)=="0"
assert format_float_str2(mm_to_inches(100, precision=1)) == "4"
def deprecated_normalize_inches(input_str):
"""
Функция для проверки и нормализации записи в дюймах.
Преобразует записи вида '3.3/4"' в нормализованный вид с десятичными долями: '3.75"'.
Параметры:
- input_str (str): Строка с записью дюймов в формате 'X.Y/Z"' или 'X"' или 'X.Y"'.
Возвращает:
- str: Нормализованная строка в формате 'X.Y"', если вход корректен.
Исключения:
- ValueError: Если формат строки неверен или дробь не нормализуется до 0.25, 0.5 или 0.75.
"""
# Регулярное выражение для формата с целым, дробной частью и необязательной дробью
pattern = r'^(\d+)(?:\.(\d+)/(\d+))?"$'
match = re.match(pattern, input_str)
if not match:
raise ValueError("Неверный формат записи. Используйте формат 'X.Y/Z\"'.")
# Парсинг значений из строки
whole_part = int(match.group(1))
numerator = int(match.group(2)) if match.group(2) else 0
denominator = int(match.group(3)) if match.group(3) else 1 # Если нет дробной части, делитель = 1
# Вычисление дробного значения
fraction_value = whole_part + Fraction(numerator, denominator)
# Приведение к десятичному виду
decimal_value = float(fraction_value)
# Округление до допустимых значений
allowed_fractions = [0.0, 0.25, 0.5, 0.75]
rounded_fraction = round(decimal_value - whole_part, 2) # Оставляем только дробную часть и округляем до 2 знаков
if rounded_fraction not in allowed_fractions:
raise ValueError("Дробная часть должна быть одной из 0.25, 0.5 или 0.75.")
# Формируем нормализованное значение
normalized_value = format_float_str2(whole_part + rounded_fraction)
return f'{normalized_value}"'
assert deprecated_normalize_inches('3"') == '3"'
assert deprecated_normalize_inches('3.3/4"') == '3.75"'
assert deprecated_normalize_inches('3.1/2"') == '3.5"'
# assert deprecated_normalize_inches('2.9/16"') == '2.5"'
def feet_inches_to_mm(feet_inches: str) -> float:
"""
Converts a measurement in feet and inches to millimeters.
Args:
feet_inches (str): A string representing measurement in the format "X'Y.Z/W\"" where:
- X is the feet part (optional),
- Y is the inches part (optional),
- Z/W is the fractional inches part (optional).
Returns:
float: The measurement converted to millimeters.
"""
feet = 0 # Default feet to 0 if not provided
whole_inches = 0
fractional_inches = 0.0
# Check if the input contains feet (indicated by the presence of `'`)
if "'" in feet_inches:
feet_part, inches_part = feet_inches.split("'")
feet = int(feet_part.strip())
else:
inches_part = feet_inches # If no feet part, consider the entire input as inches part
# Process the inches part, handling whole and fractional inches
if inches_part:
inches_part = inches_part.replace('"', '').strip() # Remove the trailing quote
if '.' in inches_part and '/' not in inches_part: # Check for mixed decimal/fractional inches
whole_inches, fractional_part = inches_part.split('.')
whole_inches = int(whole_inches.strip())
fractional_inches = float('0.'+fractional_part)
elif '/' in inches_part: # Check for pure fractional inches (e.g., "1/4")
try:
whole_inches, fractional_part = inches_part.split('.')
except:
whole_inches='0'
fractional_part= inches_part
whole_inches = int(whole_inches.strip())
fractional_inches = float(Fraction(fractional_part))
else: # Pure integer inches
whole_inches = int(inches_part.strip())
# Convert feet and inches to millimeters
feet_mm = feet * 304.8
inches_mm = (whole_inches + fractional_inches) * 25.4
total_mm = feet_mm + inches_mm
return total_mm
# Test cases based on the examples provided
assert foo_campare( feet_inches_to_mm('2.5"') , 2.5*25.4)
assert foo_campare(feet_inches_to_mm('1\'') , 304.8)
assert foo_campare(feet_inches_to_mm('1\'2"') , 304.8 + 2*25.4)
assert foo_campare(feet_inches_to_mm('1\'2.1/2"') , 304.8 + (2+1/2)*25.4)
assert foo_campare(feet_inches_to_mm('2"') , 2*25.4)
assert foo_campare(feet_inches_to_mm('2.9/16"') , (2+9/16)*25.4)
assert foo_campare(feet_inches_to_mm('0.1/8"') , (0+1/8)*25.4)
assert foo_campare(feet_inches_to_mm('1/8"') , (1/8)*25.4)
def convert_raw_size_to_inch(raw_size0, precision=0.25):
if isinstance(raw_size0, (int, float)):
raw_size=str(raw_size0)
elif isinstance(raw_size0, str):
raw_size = raw_size0
else:
raw_size = str(raw_size0)
if ' ' in raw_size:
k1,k2 = raw_size.split(' ')
return convert_raw_size_to_inch(k1,precision)+'x'+convert_raw_size_to_inch(k2,precision)
elif '"' in raw_size or "'" in raw_size:
mm=feet_inches_to_mm(raw_size)
return format_float_str2(mm_to_inches(mm, precision))+ '"'
else:
return format_float_str2(mm_to_inches(raw_size, precision))+ '"'
assert convert_raw_size_to_inch('0')=='0"'
assert convert_raw_size_to_inch('100')=='4"'
assert convert_raw_size_to_inch('80', precision=0.5) == '3"'
assert convert_raw_size_to_inch('3.3/4"', precision=0.05) == '3.75"'
assert convert_raw_size_to_inch('3/8" 3.3/4"', precision=0.125) == '0.375"x3.75"'
assert convert_raw_size_to_inch('100 80', precision=0.5) == '4"x3"'
def load_json(file_path):
"""
Загрузить объект из JSON файла.
:param file_path: Путь к JSON файлу
:return: Объект Python, содержащий данные из JSON файла
"""
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return data
def png_to_jpeg_base64(png_filename: str, quality: int = 95) -> str:
# Открываем изображение в формате PNG
with Image(filename=png_filename) as img:
# Преобразуем изображение в формат JPEG с заданным качеством
img.format = 'jpeg'
img.compression_quality = quality
# Сохраняем изображение в буфер памяти
with BytesIO() as buffer:
img.save(file=buffer)
# Возвращаем указатель буфера на начало
buffer.seek(0)
# Получаем байты изображения
jpeg_bytes = buffer.read()
# Кодируем байты изображения в строку Base64
jpeg_base64 = base64.b64encode(jpeg_bytes).decode('utf-8')
return jpeg_base64
def compare_numbers(a, b, relative_tolerance=1e-9):
"""
Сравнивает два числа с относительной точностью.
:param a: Первое число (может быть целым или дробным).
:param b: Второе число (может быть целым или дробным).
:param relative_tolerance: Допустимая относительная погрешность.
:return: True, если числа равны с заданной точностью, иначе False.
"""
# Если оба числа равны (сразу вернуть True)
if a == b:
return True
# Проверяем относительную разницу
relative_difference = abs(a - b) / max(abs(a), abs(b))
return relative_difference <= relative_tolerance
def image_red_expand(filename_png, output_png, radius=5):
# Загрузка изображения
image = cv2.imread(filename_png)
# Определение диапазона красного цвета в RGB
# Красный цвет: высокий Red, низкий Green и Blue
lower_red = np.array([50, 0, 0]) # Нижний порог для красного
upper_red = np.array([255, 100, 100]) # Верхний порог для красного
# Создание маски на основе красных областей в RGB
red_mask = cv2.inRange(image, lower_red, upper_red)
# Применение дилатации (dilation) к красным областям для их расширения
kernel = np.ones((radius, radius), np.uint8) # Ядро 5x5 по умолчанию
dilated_mask = cv2.dilate(red_mask, kernel, iterations=1)
# Извлечение расширенных красных областей
red_areas = cv2.bitwise_and(image, image, mask=dilated_mask)
# Извлечение всех остальных областей, которые не являются красными
inverse_mask = cv2.bitwise_not(red_mask) # Инвертируем оригинальную маску
background = cv2.bitwise_and(image, image, mask=inverse_mask)
# Комбинируем увеличенные красные области с фоном
result_image = cv2.add(background, red_areas)
# Сохраняем результат
cv2.imwrite(output_png, result_image)
def image_colors_expand(filename_png, output_png, radius=5):
if radius<2:
logger.warning(f'image_colors_expand radius={radius}')
return -1
# Загрузка изображения
image = cv2.imread(filename_png)
# Преобразование изображения в цветовое пространство Lab
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
# Разделяем каналы Lab
L, A, B = cv2.split(lab_image)
# Преобразуем A и B каналы в float32 для корректных математических операций
A = A.astype(np.float32)
B = B.astype(np.float32)
# Вычисляем интенсивность цвета
color_magnitude = np.sqrt(A ** 2 + B ** 2)
# Создаем маску для областей с высокой интенсивностью цвета
color_intensity_mask = cv2.inRange(color_magnitude, 50, 255)
# Инвертируем маску для выделения только цветных областей (исключаем черное и белое)
non_black_white_mask = cv2.bitwise_not(color_intensity_mask)
# Подсчитаем количество ненулевых (не черных) пикселей
non_black_pixels = cv2.countNonZero(non_black_white_mask)
if non_black_pixels>10:
logger.debug(f'Non black pixels {non_black_pixels} ({filename_png})')
# Первое dilate для создания dilated_mask
kernel = np.ones((5, 5), np.uint8)
dilated_mask = cv2.dilate(non_black_white_mask, kernel, iterations=1)
# Применяем non_black_white_mask к изображению и делаем второе dilate
selected_areas = cv2.bitwise_and(image, image, mask=non_black_white_mask)
dilated_selected_areas = cv2.dilate(selected_areas, kernel, iterations=1)
# Комбинируем результат с исходным изображением по маске dilated_mask
result_image = np.where(dilated_mask[:, :, np.newaxis] != 0, dilated_selected_areas, image)
# cv2.imwrite('color_intensity_mask.png', color_intensity_mask)
# cv2.imwrite('selected_areas.png', selected_areas)
# cv2.imwrite('dilated_mask.png', dilated_mask)
# cv2.imwrite('dilated_selected_areas.png', dilated_selected_areas)
# Сохраняем результат
cv2.imwrite(output_png, result_image)
return non_black_pixels
def split_pdf(input_pdf, output_folder):
# Открываем исходный PDF файл
with open(input_pdf, 'rb') as pdf_file:
reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(reader.pages)
# Проходим по каждой странице
for page_num in tqdm.tqdm(range(num_pages), desc=f'Split {input_pdf} pages'):
writer = PyPDF2.PdfWriter()
page = reader.pages[page_num]
writer.add_page(page)
output_filename = os.path.join(output_folder, f"tmp_page_{page_num + 1}.pdf")
with open(output_filename, 'wb') as output_pdf:
writer.write(output_pdf)
logger.trace(f"Saved {page_num + 1} as {output_filename}")
return num_pages
```
Assistant: