{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "a4447a99", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from typing import Iterable, List\n", "\n", "import numpy as np\n", "from PIL import Image\n", "from IPython.display import display\n", "\n", "from vibe_blending import run_vibe_blend_not_safe\n", "from ipadapter_model import create_image_grid" ] }, { "cell_type": "code", "execution_count": null, "id": "2f9e85da", "metadata": {}, "outputs": [], "source": [ "CONFIG_PATH = Path('config.yaml')\n", "IMAGES_DIR = Path('images')\n", "\n", "POSITIVE_IMAGE_PATHS = [\n", " IMAGES_DIR / 'playviolin_hr.png',\n", " IMAGES_DIR / 'playguitar_hr.png',\n", "]\n", "\n", "EXTRA_IMAGE_PATHS = [\n", " # ...\n", "]\n", "\n", "NEGATIVE_IMAGE_PATHS = [\n", " # ...\n", "]\n", "\n", "def load_images(paths: Iterable[Path]) -> List[Image.Image]:\n", " return [Image.open(path).convert('RGB') for path in paths]\n", "\n", "positive_images = load_images(POSITIVE_IMAGE_PATHS)\n", "extra_images = load_images(EXTRA_IMAGE_PATHS)\n", "negative_images = load_images(NEGATIVE_IMAGE_PATHS)\n", "\n", "print(f'Loaded {len(positive_images)} positive, {len(extra_images)} extra, {len(negative_images)} negative images')" ] }, { "cell_type": "code", "execution_count": null, "id": "4b079a6a", "metadata": {}, "outputs": [], "source": [ "GRID_PREVIEW_SIZE = (256, 256)\n", "GRID_BLEND_SIZE = (512, 512)\n", "\n", "def resize_for_grid(images: List[Image.Image], size: tuple[int, int]) -> List[Image.Image]:\n", " return [img.resize(size, Image.Resampling.LANCZOS) for img in images]\n", "\n", "def show_row(title: str, images: List[Image.Image]):\n", " if not images:\n", " return\n", " print(f\"{title} ({len(images)} images)\")\n", " thumbs = resize_for_grid(images, GRID_PREVIEW_SIZE)\n", " grid = create_image_grid(thumbs, rows=1, cols=len(thumbs))\n", " display(grid)\n", "\n", "show_row('Positives', positive_images)\n", "show_row('Extra references', extra_images)\n", "show_row('Negatives (attributes to suppress)', negative_images)" ] }, { "cell_type": "code", "execution_count": null, "id": "9d396972", "metadata": {}, "outputs": [], "source": [ "alpha_weights = np.linspace(0, 1, 10).tolist()\n", "print(f'α values: {alpha_weights}')\n", "\n", "blended_with_negatives = run_vibe_blend_not_safe(\n", " image1=positive_images[0],\n", " image2=positive_images[1],\n", " extra_images=extra_images,\n", " negative_images=negative_images,\n", " config_path=str(CONFIG_PATH),\n", " interpolation_weights=alpha_weights,\n", " n_clusters=20,\n", ")\n", "\n", "sequence_with_neg = [positive_images[0], *blended_with_negatives, positive_images[1]]\n", "rows = int(np.ceil(len(sequence_with_neg) / 4))\n", "normalized_sequence = resize_for_grid(sequence_with_neg, GRID_BLEND_SIZE)\n", "neg_grid = create_image_grid(normalized_sequence, rows=rows, cols=4)\n", "display(neg_grid)" ] } ], "metadata": { "kernelspec": { "display_name": "mspace", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 5 }