mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-07-22 23:47:02 +08:00
248 lines
13 KiB
Plaintext
248 lines
13 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 34,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from PIL import Image, ImageDraw\n",
|
|
"import imageio\n",
|
|
"import numpy as np\n",
|
|
"from io import BytesIO\n",
|
|
"import cairosvg\n",
|
|
"from lxml import etree\n",
|
|
"\n",
|
|
"# Assuming WATERMARK and WATERMARK_SIZE are defined elsewhere, e.g.,\n",
|
|
"WATERMARK = \"\"\"\n",
|
|
"<svg width=\"256\" height=\"256\" viewBox=\"0 0 256 256\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n",
|
|
" <path d=\"M60.0859 196.8C65.9526 179.067 71.5526 161.667 76.8859 144.6C79.1526 137.4 81.4859 129.867 83.8859 122C86.2859 114.133 88.6859 106.333 91.0859 98.6C93.4859 90.8667 95.6859 83.4 97.6859 76.2C99.8193 69 101.686 62.3333 103.286 56.2C110.619 56.2 117.553 55.8 124.086 55C130.619 54.2 137.686 53.4667 145.286 52.8C144.886 55.7333 144.419 59.0667 143.886 62.8C143.486 66.4 142.953 70.2 142.286 74.2C141.753 78.2 141.153 82.3333 140.486 86.6C139.819 90.8667 139.019 96.3333 138.086 103C137.153 109.667 135.886 118 134.286 128H136.886C140.753 117.867 143.953 109.467 146.486 102.8C149.019 96 151.086 90.4667 152.686 86.2C154.286 81.9333 155.886 77.8 157.486 73.8C159.219 69.6667 160.819 65.8 162.286 62.2C163.886 58.4667 165.353 55.2 166.686 52.4C170.019 52.1333 173.153 51.8 176.086 51.4C179.019 51 181.953 50.6 184.886 50.2C187.819 49.6667 190.753 49.2 193.686 48.8C196.753 48.2667 200.086 47.6667 203.686 47C202.353 54.7333 201.086 62.6667 199.886 70.8C198.686 78.9333 197.619 87.0667 196.686 95.2C195.753 103.2 194.819 111.133 193.886 119C193.086 126.867 192.353 134.333 191.686 141.4C190.086 157.933 188.686 174.067 187.486 189.8L152.686 196C152.686 195.333 152.753 193.533 152.886 190.6C153.153 187.667 153.419 184.067 153.686 179.8C154.086 175.533 154.553 170.8 155.086 165.6C155.753 160.4 156.353 155.2 156.886 150C157.553 144.8 158.219 139.8 158.886 135C159.553 130.067 160.219 125.867 160.886 122.4H159.086C157.219 128 155.153 133.933 152.886 140.2C150.619 146.333 148.286 152.6 145.886 159C143.619 165.4 141.353 171.667 139.086 177.8C136.819 183.933 134.819 189.8 133.086 195.4C128.419 195.533 124.419 195.733 121.086 196C117.753 196.133 113.886 196.333 109.486 196.6L115.886 122.4H112.886C112.619 124.133 111.953 127.067 110.886 131.2C109.819 135.2 108.553 139.867 107.086 145.2C105.753 150.4 104.286 155.867 102.686 161.6C101.086 167.2 99.5526 172.467 98.0859 177.4C96.7526 182.2 95.6193 186.2 94.6859 189.4C93.7526 192.467 93.2193 194.2 93.0859 194.6L60.0859 196.8Z\" fill=\"white\"/>\n",
|
|
"</svg>\n",
|
|
"\"\"\"\n",
|
|
"WATERMARK_SIZE = 40\n",
|
|
"\n",
|
|
"# def add_watermark_to_image_sequence(pil_images, base_blob_name):\n",
|
|
"# \"\"\"\n",
|
|
"# Adds a watermark to a sequence of PIL images.\n",
|
|
"\n",
|
|
"# Args:\n",
|
|
"# pil_images: A list of PIL Image objects.\n",
|
|
"# base_blob_name: The base name of the blob (used for naming the output).\n",
|
|
"\n",
|
|
"# Returns:\n",
|
|
"# A list of bytes objects representing the watermarked images.\n",
|
|
"# \"\"\"\n",
|
|
"\n",
|
|
"# watermarked_images = []\n",
|
|
"# for img in pil_images:\n",
|
|
"# img = add_watermark_to_image(img)\n",
|
|
" \n",
|
|
"# # Save the image to a bytes buffer\n",
|
|
"# buffer = BytesIO()\n",
|
|
"# img.save(buffer, format=\"JPEG\")\n",
|
|
"# watermarked_images.append(buffer.getvalue())\n",
|
|
"\n",
|
|
"# return watermarked_images\n",
|
|
" \n",
|
|
" \n",
|
|
"def add_watermark_to_image_sequence(pil_images, base_blob_name):\n",
|
|
" \"\"\"\n",
|
|
" Adds a watermark to a sequence of PIL images and saves as a WEBP animation.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" pil_images: A list of PIL Image objects.\n",
|
|
" base_blob_name: The base name of the blob (used for naming the output).\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" The filename of the saved WEBP animation.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" watermarked_images = []\n",
|
|
" for img in pil_images:\n",
|
|
" img = add_watermark_to_image(img)\n",
|
|
" watermarked_images.append(img)\n",
|
|
"\n",
|
|
" # Save the images as a WEBP animation\n",
|
|
" output_filename = f\"{base_blob_name.split('.')[0]}_watermarked.webp\"\n",
|
|
" imageio.mimsave(output_filename, watermarked_images, fps=24) # Adjust fps as needed\n",
|
|
"\n",
|
|
" return output_filename\n",
|
|
"\n",
|
|
"def add_watermark_to_image(img):\n",
|
|
" \"\"\"\n",
|
|
" Adds a watermark to a single PIL Image.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" img: A PIL Image object.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" A PIL Image object with the watermark added.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" # Calculate position (bottom right corner)\n",
|
|
" padding = 12\n",
|
|
" x = img.width - WATERMARK_SIZE - padding\n",
|
|
" y = img.height - WATERMARK_SIZE - padding\n",
|
|
"\n",
|
|
" background_brightness = analyze_background_brightness(img, x, y, WATERMARK_SIZE)\n",
|
|
" print(f\"background_brightness: {background_brightness}\")\n",
|
|
"\n",
|
|
" # Generate watermark image (replace this with your actual watermark generation)\n",
|
|
" watermark = generate_watermark(WATERMARK_SIZE, background_brightness)\n",
|
|
"\n",
|
|
" # Overlay the watermark\n",
|
|
" img.paste(watermark, (x, y), watermark)\n",
|
|
"\n",
|
|
" return img\n",
|
|
"\n",
|
|
"\n",
|
|
"def analyze_background_brightness(img, x, y, size):\n",
|
|
" \"\"\"\n",
|
|
" Analyzes the average brightness of a region in the image.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" img: A PIL Image object.\n",
|
|
" x: The x-coordinate of the top-left corner of the region.\n",
|
|
" y: The y-coordinate of the top-left corner of the region.\n",
|
|
" size: The size of the region (square).\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" The average brightness of the region as an integer.\n",
|
|
" \"\"\"\n",
|
|
" region = img.crop((x, y, x + size, y + size))\n",
|
|
" pixels = np.array(region)\n",
|
|
" total_brightness = np.sum(\n",
|
|
" 0.299 * pixels[:, :, 0] + 0.587 * pixels[:, :, 1] + 0.114 * pixels[:, :, 2]\n",
|
|
" ) / 1000\n",
|
|
" print(f\"total_brightness: {total_brightness}\")\n",
|
|
" return max(0, min(255, total_brightness)) \n",
|
|
"\n",
|
|
"def generate_watermark(size, background_brightness):\n",
|
|
" \"\"\"\n",
|
|
" Generates a watermark image from an SVG string.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" size: The size of the watermark (square).\n",
|
|
" background_brightness: The background brightness at the watermark position.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" A PIL Image object representing the watermark.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" # Determine watermark color based on background brightness\n",
|
|
" watermark_color = (0, 0, 0, 165) if background_brightness > 128 else (255, 255, 255, 165)\n",
|
|
"\n",
|
|
" # Parse the SVG string\n",
|
|
" svg_tree = etree.fromstring(WATERMARK)\n",
|
|
"\n",
|
|
" # Find the path element and set its fill attribute\n",
|
|
" path_element = svg_tree.find(\".//{http://www.w3.org/2000/svg}path\")\n",
|
|
" if path_element is not None:\n",
|
|
" r, g, b, a = watermark_color\n",
|
|
" fill_color = f\"rgba({r},{g},{b},{a/255})\" # Convert to rgba string\n",
|
|
" path_element.set(\"fill\", fill_color)\n",
|
|
"\n",
|
|
" # Convert the modified SVG tree back to a string\n",
|
|
" modified_svg = etree.tostring(svg_tree, encoding=\"unicode\")\n",
|
|
"\n",
|
|
" # Render the modified SVG to a PNG image with a transparent background\n",
|
|
" png_data = cairosvg.svg2png(\n",
|
|
" bytestring=modified_svg,\n",
|
|
" output_width=size,\n",
|
|
" output_height=size,\n",
|
|
" background_color=\"transparent\"\n",
|
|
" )\n",
|
|
" watermark_img = Image.open(BytesIO(png_data))\n",
|
|
"\n",
|
|
" # Convert the watermark to RGBA to handle transparency\n",
|
|
" watermark_img = watermark_img.convert(\"RGBA\")\n",
|
|
"\n",
|
|
" return watermark_img"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 35,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "AttributeError",
|
|
"evalue": "'numpy.ndarray' object has no attribute 'width'",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
|
"Cell \u001b[0;32mIn[35], line 15\u001b[0m\n\u001b[1;32m 13\u001b[0m start_time \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# Add the watermark\u001b[39;00m\n\u001b[0;32m---> 15\u001b[0m watermarked_img \u001b[38;5;241m=\u001b[39m \u001b[43madd_watermark_to_image_sequence\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mvideo_72b0b56c_0c40_40ff_89d2_2dd1cb01a163_2fbca50b_3e1b_42e6_9391_c2b3efa091ad\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 16\u001b[0m end_time \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTime taken: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mend_time\u001b[38;5;250m \u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;250m \u001b[39mstart_time\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m seconds\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
|
|
"Cell \u001b[0;32mIn[34], line 54\u001b[0m, in \u001b[0;36madd_watermark_to_image_sequence\u001b[0;34m(pil_images, base_blob_name)\u001b[0m\n\u001b[1;32m 52\u001b[0m watermarked_images \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 53\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m img \u001b[38;5;129;01min\u001b[39;00m pil_images:\n\u001b[0;32m---> 54\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43madd_watermark_to_image\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 55\u001b[0m watermarked_images\u001b[38;5;241m.\u001b[39mappend(img)\n\u001b[1;32m 57\u001b[0m \u001b[38;5;66;03m# Save the images as a WEBP animation\u001b[39;00m\n",
|
|
"Cell \u001b[0;32mIn[34], line 77\u001b[0m, in \u001b[0;36madd_watermark_to_image\u001b[0;34m(img)\u001b[0m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;66;03m# Calculate position (bottom right corner)\u001b[39;00m\n\u001b[1;32m 76\u001b[0m padding \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m12\u001b[39m\n\u001b[0;32m---> 77\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[43mimg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwidth\u001b[49m \u001b[38;5;241m-\u001b[39m WATERMARK_SIZE \u001b[38;5;241m-\u001b[39m padding\n\u001b[1;32m 78\u001b[0m y \u001b[38;5;241m=\u001b[39m img\u001b[38;5;241m.\u001b[39mheight \u001b[38;5;241m-\u001b[39m WATERMARK_SIZE \u001b[38;5;241m-\u001b[39m padding\n\u001b[1;32m 80\u001b[0m background_brightness \u001b[38;5;241m=\u001b[39m analyze_background_brightness(img, x, y, WATERMARK_SIZE)\n",
|
|
"\u001b[0;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'width'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Load the webp image using the requests library\n",
|
|
"import time\n",
|
|
"import requests\n",
|
|
"\n",
|
|
"\n",
|
|
"image_url = \"https://media.memedeck.xyz/memes/user:d38ee417_a500_4cd4_a455_432c3cbb61fe/video_gen/video_72b0b56c_0c40_40ff_89d2_2dd1cb01a163_2fbca50b_3e1b_42e6_9391_c2b3efa091ad.webp\" # Example webp image URL\n",
|
|
"response = requests.get(image_url, stream=True)\n",
|
|
"response.raise_for_status() # Raise an exception for bad status codes\n",
|
|
"\n",
|
|
"# Open the image using PIL\n",
|
|
"img = Image.open(response.raw)\n",
|
|
"\n",
|
|
"# img_to_tensor = img_to_tensor(img)\n",
|
|
"\n",
|
|
"start_time = time.time()\n",
|
|
"# Add the watermark\n",
|
|
"\n",
|
|
"watermarked_img = add_watermark_to_image_sequence(img, 'video_72b0b56c_0c40_40ff_89d2_2dd1cb01a163_2fbca50b_3e1b_42e6_9391_c2b3efa091ad')\n",
|
|
"end_time = time.time()\n",
|
|
"print(f\"Time taken: {end_time - start_time} seconds\")\n",
|
|
"\n",
|
|
"# Save the watermarked image (optional)\n",
|
|
"# watermarked_img.save(\"watermarked_image.webp\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Requirement already satisfied: lxml in /home/holium/ComfyUI/comfy-venv/lib/python3.12/site-packages (5.3.0)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!pip install lxml"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "comfy-venv",
|
|
"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.12.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|