import os import shutil import numpy as np import uuid from PIL import Image from…
- User
import os import shutil import numpy as np import uuid from PIL import Image from pathlib import Path import telebot from utils import ImageDataset from run import run from stereoimage_generation import create_stereoimages
Ваш токен бота, замените 'YOUR_TOKEN' на фактический токен
TOKEN = '5433689596:AAEM8vxo850RIVqaTLvKgcNp6U7fTWgt8oQ' bot = telebot.TeleBot(TOKEN)
def invert_depth_map(depth_map): # Инвертирование карты глубины return depth_map * -1
def create_stereo_images(input_image, depth_map, output_path, divergence, separation): inverted_depth_map = invert_depth_map(depth_map) stereo_images = create_stereoimages( input_image, inverted_depth_map, divergence=divergence, separation=separation, modes=['left-right'], fill_technique='polylines_sharp')
output_path_jpg = Path(output_path).with_suffix('.jpg') stereo_images[0].save(output_path_jpg, format='JPEG', quality=100) return output_path_jpgdef square_image(img): width, height = img.size if width != height: size = max(width, height) new_img = Image.new('RGB', (size, size), color=(0, 0, 0)) new_img.paste(img, ((size - width) // 2, (size - height) // 2)) return new_img else: return img
def split_and_square_image(input_image_path): img = Image.open(input_image_path) width, height = img.size half_width = width // 2
# Разбить изображение на две половины left_half = img.crop((0, 0, half_width, height)) right_half = img.crop((half_width, 0, width, height)) # Сделать каждую половину квадратной left_half_square = square_image(left_half) right_half_square = square_image(right_half) # Объединить квадратные половины в одно изображение combined_width = left_half_square.width + right_half_square.width combined = Image.new('RGB', (combined_width, left_half_square.height)) combined.paste(left_half_square, (0, 0)) combined.paste(right_half_square, (left_half_square.width, 0)) return combineddef process_image(input_file_path, output_path, divergence, separation): tmp_dir = os.path.join(output_path, 'temp') os.makedirs(tmp_dir, exist_ok=True)
tmp_file_path = os.path.join(tmp_dir, os.path.basename(input_file_path)) shutil.copy2(input_file_path, tmp_file_path) class Options: def __init__(self): self.data_dir = tmp_dir self.output_dir = output_path self.savepatchs = 0 self.savewholeest = 0 self.output_resolution = 1 self.pix2pixsize = 1024 self.depthNet = 2 # Использовать LeRes self.colorize_results = False self.R0 = False self.R20 = False self.Final = True # Использовать параметр Final self.max_res = np.inf self.net_receptive_field_size = 448 self.patch_netsize = 2 * self.net_receptive_field_size option = Options() dataset = ImageDataset(tmp_dir, 'test') run(dataset, option) # Генерация карты глубины и создание стереоизображения depth_map_path = os.path.join(output_path, os.path.basename(input_file_path).split('.')[0] + '.png') input_image = Image.open(input_file_path) depth_map = np.array(Image.open(depth_map_path)) stereo_image_path = create_stereo_images(input_image, depth_map, depth_map_path, divergence, separation) # Изменение размера стереоизображения final_image = split_and_square_image(stereo_image_path) final_image_path = os.path.join(output_path, "final_" + os.path.basename(stereo_image_path)) final_image.save(final_image_path, format='JPEG', quality=100) shutil.rmtree(tmp_dir) return final_image_path - Assistant
- User
можно какие нибудь смайлики добавить и как то сделать бот более креативным
- Assistant