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"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
},
|
| 13 |
+
"language_info": {
|
| 14 |
+
"name": "python"
|
| 15 |
+
},
|
| 16 |
+
"accelerator": "GPU"
|
| 17 |
+
},
|
| 18 |
+
"cells": [
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "markdown",
|
| 21 |
+
"metadata": {
|
| 22 |
+
"id": "54eb8180"
|
| 23 |
+
},
|
| 24 |
+
"source": [
|
| 25 |
+
"### Hugging Face Login\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"To access gated models like `briaai/RMBG-2.0`, you need to log in to Hugging Face. Make sure you have your Hugging Face API token stored in Colab secrets as `HF_TOKEN`. You can create a token [here](https://huggingface.co/settings/tokens)."
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "code",
|
| 32 |
+
"metadata": {
|
| 33 |
+
"colab": {
|
| 34 |
+
"base_uri": "https://localhost:8080/"
|
| 35 |
+
},
|
| 36 |
+
"id": "69c32694",
|
| 37 |
+
"outputId": "4db31198-daed-450c-9fd4-8c84211935d3"
|
| 38 |
+
},
|
| 39 |
+
"source": [
|
| 40 |
+
"from google.colab import userdata\n",
|
| 41 |
+
"from huggingface_hub import login\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"# Retrieve HF_TOKEN from Colab secrets\n",
|
| 44 |
+
"HF_TOKEN = userdata.get('HF_TOKEN')\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"if HF_TOKEN:\n",
|
| 47 |
+
" login(token=HF_TOKEN)\n",
|
| 48 |
+
" print(\"Successfully logged into Hugging Face.\")\n",
|
| 49 |
+
"else:\n",
|
| 50 |
+
" print(\"HF_TOKEN not found in Colab secrets. Please add it to proceed with gated models.\")"
|
| 51 |
+
],
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"outputs": [
|
| 54 |
+
{
|
| 55 |
+
"output_type": "stream",
|
| 56 |
+
"name": "stdout",
|
| 57 |
+
"text": [
|
| 58 |
+
"Successfully logged into Hugging Face.\n"
|
| 59 |
+
]
|
| 60 |
+
}
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"cell_type": "code",
|
| 65 |
+
"source": [
|
| 66 |
+
"!pip install -q \\\n",
|
| 67 |
+
" transformers \\\n",
|
| 68 |
+
" accelerate \\\n",
|
| 69 |
+
" pillow \\\n",
|
| 70 |
+
" tqdm \\\n",
|
| 71 |
+
" safetensors \\\n",
|
| 72 |
+
" kornia"
|
| 73 |
+
],
|
| 74 |
+
"metadata": {
|
| 75 |
+
"id": "Y3HzjfqPsP45",
|
| 76 |
+
"colab": {
|
| 77 |
+
"base_uri": "https://localhost:8080/"
|
| 78 |
+
},
|
| 79 |
+
"outputId": "d631aca6-59d8-4fba-969b-8c96168f2131"
|
| 80 |
+
},
|
| 81 |
+
"execution_count": null,
|
| 82 |
+
"outputs": [
|
| 83 |
+
{
|
| 84 |
+
"output_type": "stream",
|
| 85 |
+
"name": "stdout",
|
| 86 |
+
"text": [
|
| 87 |
+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.2 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m35.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m23.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 88 |
+
"\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/3.7 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m3.7/3.7 MB\u001b[0m \u001b[31m130.8 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.7/3.7 MB\u001b[0m \u001b[31m66.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 89 |
+
"\u001b[?25h"
|
| 90 |
+
]
|
| 91 |
+
}
|
| 92 |
+
]
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"cell_type": "code",
|
| 96 |
+
"source": [
|
| 97 |
+
"from google.colab import drive\n",
|
| 98 |
+
"drive.mount('/content/drive')"
|
| 99 |
+
],
|
| 100 |
+
"metadata": {
|
| 101 |
+
"id": "WHDPLITy4Rh0"
|
| 102 |
+
},
|
| 103 |
+
"execution_count": null,
|
| 104 |
+
"outputs": []
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"cell_type": "markdown",
|
| 108 |
+
"source": [
|
| 109 |
+
"Note: Bria AI remove background tool is a gated access model , meaning you will need a huggingface account , and you\n",
|
| 110 |
+
"will need to fetch your huggingface api token , the agree to open source terms of usage on https://huggingface.co/briaai/RMBG-2.0 while logged into your huggingface account"
|
| 111 |
+
],
|
| 112 |
+
"metadata": {
|
| 113 |
+
"id": "TT87LNT9uAWR"
|
| 114 |
+
}
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"cell_type": "code",
|
| 118 |
+
"source": [
|
| 119 |
+
"import zipfile\n",
|
| 120 |
+
"import shutil\n",
|
| 121 |
+
"from pathlib import Path\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"import torch\n",
|
| 124 |
+
"import numpy as np\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"from PIL import Image\n",
|
| 127 |
+
"from tqdm import tqdm\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"from transformers import AutoModelForImageSegmentation\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"# =============================================================================\n",
|
| 132 |
+
"# SETTINGS: Find all 'animation' zip files in Google Drive\n",
|
| 133 |
+
"# =============================================================================\n",
|
| 134 |
+
"\n",
|
| 135 |
+
"drive_root = Path(\"/content/drive/MyDrive\")\n",
|
| 136 |
+
"animation_zip_files = []\n",
|
| 137 |
+
"print(f\"Searching for 'animation' zip files in {drive_root} recursively...\")\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"# Use .rglob to find all .zip files recursively in MyDrive\n",
|
| 140 |
+
"for p in drive_root.rglob(\"*.zip\"):\n",
|
| 141 |
+
" if \"animation\" in p.name.lower():\n",
|
| 142 |
+
" animation_zip_files.append(p)\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"if not animation_zip_files:\n",
|
| 145 |
+
" raise FileNotFoundError(\n",
|
| 146 |
+
" \"No ZIP files containing 'animation' found in Google Drive. \"\n",
|
| 147 |
+
" \"Please upload your animation zip files to your Google Drive.\"\n",
|
| 148 |
+
" )\n",
|
| 149 |
+
"else:\n",
|
| 150 |
+
" print(f\"Found {len(animation_zip_files)} animation ZIP file(s):\")\n",
|
| 151 |
+
" for i, zip_file in enumerate(animation_zip_files):\n",
|
| 152 |
+
" print(f\"{i+1}. {zip_file.name}\")\n",
|
| 153 |
+
"\n",
|
| 154 |
+
"# =============================================================================\n",
|
| 155 |
+
"# DEVICE & MODEL LOADING (Load once for all ZIPs)\n",
|
| 156 |
+
"# =============================================================================\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 159 |
+
"print(f\"Using device: {device}\")\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"print(\"Loading BRIA RMBG 2.0...\")\n",
|
| 162 |
+
"model = AutoModelForImageSegmentation.from_pretrained(\n",
|
| 163 |
+
" \"briaai/RMBG-2.0\",\n",
|
| 164 |
+
" trust_remote_code=True\n",
|
| 165 |
+
")\n",
|
| 166 |
+
"model.to(device)\n",
|
| 167 |
+
"model.eval()\n",
|
| 168 |
+
"print(\"Model loaded\")\n",
|
| 169 |
+
"\n",
|
| 170 |
+
"# =============================================================================\n",
|
| 171 |
+
"# IMAGE HELPERS\n",
|
| 172 |
+
"# =============================================================================\n",
|
| 173 |
+
"\n",
|
| 174 |
+
"MODEL_SIZE = 1024\n",
|
| 175 |
+
"\n",
|
| 176 |
+
"def preprocess_image(image):\n",
|
| 177 |
+
" image = image.convert(\"RGB\")\n",
|
| 178 |
+
" original_size = image.size\n",
|
| 179 |
+
" resized = image.resize((MODEL_SIZE, MODEL_SIZE), Image.Resampling.LANCZOS)\n",
|
| 180 |
+
" image_np = (np.array(resized).astype(np.float32) / 255.0)\n",
|
| 181 |
+
" image_np = image_np.transpose(2, 0, 1)\n",
|
| 182 |
+
" tensor = torch.from_numpy(image_np).unsqueeze(0)\n",
|
| 183 |
+
" return tensor, original_size\n",
|
| 184 |
+
"\n",
|
| 185 |
+
"def postprocess_mask(mask, original_size):\n",
|
| 186 |
+
" mask = mask.squeeze()\n",
|
| 187 |
+
" mask = mask.cpu().numpy()\n",
|
| 188 |
+
" mask = (\n",
|
| 189 |
+
" mask * 255\n",
|
| 190 |
+
" ).astype(np.uint8)\n",
|
| 191 |
+
" mask_img = Image.fromarray(mask)\n",
|
| 192 |
+
" mask_img = mask_img.resize(original_size, Image.Resampling.LANCZOS)\n",
|
| 193 |
+
" return mask_img\n",
|
| 194 |
+
"\n",
|
| 195 |
+
"# =============================================================================\n",
|
| 196 |
+
"# PROCESS EACH ANIMATION ZIP FILE\n",
|
| 197 |
+
"# =============================================================================\n",
|
| 198 |
+
"\n",
|
| 199 |
+
"total_processed_images_across_all_zips = 0\n",
|
| 200 |
+
"processed_output_zip_paths = []\n",
|
| 201 |
+
"\n",
|
| 202 |
+
"for idx, zip_filepath in enumerate(animation_zip_files):\n",
|
| 203 |
+
" print(f\"\\n{'='*10} Processing {zip_filepath.name} ({idx+1}/{len(animation_zip_files)}) {'='*10}\")\n",
|
| 204 |
+
"\n",
|
| 205 |
+
" # Determine output ZIP filename\n",
|
| 206 |
+
" output_zip_filename = f\"{zip_filepath.stem}_transparent.zip\"\n",
|
| 207 |
+
"\n",
|
| 208 |
+
" # Define unique working directories for this specific ZIP file\n",
|
| 209 |
+
" # Use the stem of the zip_filepath to create unique temporary directories\n",
|
| 210 |
+
" current_zip_base_dir = Path(\"/content/bria_rmbg_temp\") / zip_filepath.stem\n",
|
| 211 |
+
" input_dir = current_zip_base_dir / \"input\"\n",
|
| 212 |
+
" output_dir = current_zip_base_dir / \"output\"\n",
|
| 213 |
+
"\n",
|
| 214 |
+
" # Clean up previous run's temporary directories for this specific zip, if they exist\n",
|
| 215 |
+
" if current_zip_base_dir.exists():\n",
|
| 216 |
+
" shutil.rmtree(current_zip_base_dir)\n",
|
| 217 |
+
"\n",
|
| 218 |
+
" input_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 219 |
+
" output_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 220 |
+
"\n",
|
| 221 |
+
" # EXTRACT ZIP\n",
|
| 222 |
+
" print(f\"Extracting {zip_filepath.name} to {input_dir}...\")\n",
|
| 223 |
+
" try:\n",
|
| 224 |
+
" with zipfile.ZipFile(zip_filepath, \"r\") as z:\n",
|
| 225 |
+
" z.extractall(input_dir)\n",
|
| 226 |
+
" print(\"ZIP extracted successfully.\")\n",
|
| 227 |
+
" except Exception as e:\n",
|
| 228 |
+
" print(f\"Error extracting {zip_filepath.name}: {e}. Skipping this ZIP.\")\n",
|
| 229 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 230 |
+
" continue\n",
|
| 231 |
+
"\n",
|
| 232 |
+
" # REMOVE MACOS METADATA (e.g., __MACOSX, .DS_Store)\n",
|
| 233 |
+
" print(\"Cleaning up macOS metadata...\")\n",
|
| 234 |
+
" for macos_dir in input_dir.rglob(\"__MACOSX\"):\n",
|
| 235 |
+
" if macos_dir.is_dir():\n",
|
| 236 |
+
" try:\n",
|
| 237 |
+
" shutil.rmtree(macos_dir, ignore_errors=True)\n",
|
| 238 |
+
" except Exception as e:\n",
|
| 239 |
+
" print(f\"Warning: Could not remove {macos_dir}: {e}\")\n",
|
| 240 |
+
" for ds_store in input_dir.rglob(\".DS_Store\"):\n",
|
| 241 |
+
" try:\n",
|
| 242 |
+
" ds_store.unlink()\n",
|
| 243 |
+
" except Exception as e:\n",
|
| 244 |
+
" print(f\"Warning: Could not remove {ds_store}: {e}\")\n",
|
| 245 |
+
"\n",
|
| 246 |
+
" # FIND IMAGES\n",
|
| 247 |
+
" IMAGE_EXTENSIONS = {\".jpg\", \".jpeg\", \".png\", \".webp\", \".bmp\", \".tif\", \".tiff\"}\n",
|
| 248 |
+
" image_files = []\n",
|
| 249 |
+
" for p in input_dir.rglob(\"*\"):\n",
|
| 250 |
+
" if not p.is_file():\n",
|
| 251 |
+
" continue\n",
|
| 252 |
+
" # Skip AppleDouble files (._filename.jpg)\n",
|
| 253 |
+
" if p.name.startswith(\"._\"):\n",
|
| 254 |
+
" continue\n",
|
| 255 |
+
" if p.suffix.lower() in IMAGE_EXTENSIONS:\n",
|
| 256 |
+
" image_files.append(p)\n",
|
| 257 |
+
"\n",
|
| 258 |
+
" print(f\"Found {len(image_files)} image(s) in {zip_filepath.name} for processing.\")\n",
|
| 259 |
+
"\n",
|
| 260 |
+
" if len(image_files) == 0:\n",
|
| 261 |
+
" print(f\"No valid image files found in {zip_filepath.name}. Skipping background removal for this ZIP.\")\n",
|
| 262 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 263 |
+
" continue\n",
|
| 264 |
+
"\n",
|
| 265 |
+
" # PROCESS IMAGES (Remove Background)\n",
|
| 266 |
+
" processed_images_in_current_zip = 0\n",
|
| 267 |
+
" print(f\"Removing backgrounds for images in {zip_filepath.name}...\")\n",
|
| 268 |
+
" for image_path in tqdm(image_files, desc=f\"Processing images in {zip_filepath.name}\"):\n",
|
| 269 |
+
" try:\n",
|
| 270 |
+
" image = Image.open(image_path).convert(\"RGB\")\n",
|
| 271 |
+
" tensor, original_size = preprocess_image(image)\n",
|
| 272 |
+
" tensor = tensor.to(device)\n",
|
| 273 |
+
"\n",
|
| 274 |
+
" with torch.no_grad():\n",
|
| 275 |
+
" prediction = model(tensor)\n",
|
| 276 |
+
" if isinstance(prediction, (tuple, list)):\n",
|
| 277 |
+
" prediction = prediction[-1]\n",
|
| 278 |
+
" prediction = torch.sigmoid(prediction)\n",
|
| 279 |
+
"\n",
|
| 280 |
+
" mask = postprocess_mask(prediction[0][0], original_size)\n",
|
| 281 |
+
" rgba = image.convert(\"RGBA\")\n",
|
| 282 |
+
" rgba.putalpha(mask)\n",
|
| 283 |
+
"\n",
|
| 284 |
+
" # Preserve original folder structure and filename, convert to PNG\n",
|
| 285 |
+
" relative_path = image_path.relative_to(input_dir)\n",
|
| 286 |
+
" output_image_path = output_dir / relative_path.with_suffix(\".png\")\n",
|
| 287 |
+
"\n",
|
| 288 |
+
" output_image_path.parent.mkdir(parents=True, exist_ok=True)\n",
|
| 289 |
+
" rgba.save(output_image_path, \"PNG\", optimize=True)\n",
|
| 290 |
+
" processed_images_in_current_zip += 1\n",
|
| 291 |
+
"\n",
|
| 292 |
+
" except Exception as e:\n",
|
| 293 |
+
" print(f\"\\nFailed to process image: {image_path.name} from {zip_filepath.name}. Error: {e}\")\n",
|
| 294 |
+
" # Continue to the next image even if one fails\n",
|
| 295 |
+
"\n",
|
| 296 |
+
" total_processed_images_across_all_zips += processed_images_in_current_zip\n",
|
| 297 |
+
" print(f\"Successfully processed {processed_images_in_current_zip} images for {zip_filepath.name}.\")\n",
|
| 298 |
+
"\n",
|
| 299 |
+
" # CREATE OUTPUT ZIP for the current processed files\n",
|
| 300 |
+
" print(f\"Creating output ZIP '{output_zip_filename}' for processed images...\")\n",
|
| 301 |
+
" temp_output_zip_path = Path(\"/content\") / f\"{zip_filepath.stem}_transparent\" # Temporary zip without .zip suffix\n",
|
| 302 |
+
"\n",
|
| 303 |
+
" # Ensure no old temporary zip file exists\n",
|
| 304 |
+
" if temp_output_zip_path.with_suffix(\".zip\").exists():\n",
|
| 305 |
+
" temp_output_zip_path.with_suffix(\".zip\").unlink()\n",
|
| 306 |
+
"\n",
|
| 307 |
+
" shutil.make_archive(str(temp_output_zip_path), \"zip\", output_dir)\n",
|
| 308 |
+
" print(\"Output ZIP created successfully.\")\n",
|
| 309 |
+
"\n",
|
| 310 |
+
" # SAVE TO GOOGLE DRIVE\n",
|
| 311 |
+
" final_destination_path = drive_root / output_zip_filename\n",
|
| 312 |
+
" if final_destination_path.exists():\n",
|
| 313 |
+
" final_destination_path.unlink() # Overwrite if it exists\n",
|
| 314 |
+
"\n",
|
| 315 |
+
" shutil.move(str(temp_output_zip_path.with_suffix(\".zip\")), str(final_destination_path))\n",
|
| 316 |
+
" processed_output_zip_paths.append(final_destination_path)\n",
|
| 317 |
+
" print(f\"Processed output ZIP saved to Google Drive: {final_destination_path}\")\n",
|
| 318 |
+
"\n",
|
| 319 |
+
" # Clean up temporary directories for the current ZIP\n",
|
| 320 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 321 |
+
" print(f\"Cleaned up temporary working directory: {current_zip_base_dir}\")\n",
|
| 322 |
+
"\n",
|
| 323 |
+
"# =============================================================================\n",
|
| 324 |
+
"# FINAL SUMMARY\n",
|
| 325 |
+
"# =============================================================================\n",
|
| 326 |
+
"\n",
|
| 327 |
+
"print(\"\\n\" + \"=\" * 60)\n",
|
| 328 |
+
"print(\"ALL ANIMATION ZIP FILES PROCESSING COMPLETE\")\n",
|
| 329 |
+
"print(\"=\" * 60)\n",
|
| 330 |
+
"print(f\"Total 'animation' ZIP files found: {len(animation_zip_files)}\")\n",
|
| 331 |
+
"print(f\"Total images processed across all ZIPs: {total_processed_images_across_all_zips}\")\n",
|
| 332 |
+
"print(\"\\nGenerated output ZIP(s) saved to Google Drive:\")\n",
|
| 333 |
+
"if processed_output_zip_paths:\n",
|
| 334 |
+
" for path in processed_output_zip_paths:\n",
|
| 335 |
+
" print(f\"- {path}\")\n",
|
| 336 |
+
"else:\n",
|
| 337 |
+
" print(\"- No output ZIPs were generated.\")\n",
|
| 338 |
+
"print(\"=\" * 60)"
|
| 339 |
+
],
|
| 340 |
+
"metadata": {
|
| 341 |
+
"id": "HOMl19GZxBmh"
|
| 342 |
+
},
|
| 343 |
+
"execution_count": null,
|
| 344 |
+
"outputs": []
|
| 345 |
+
},
|
| 346 |
+
{
|
| 347 |
+
"cell_type": "code",
|
| 348 |
+
"metadata": {
|
| 349 |
+
"id": "58ad1230"
|
| 350 |
+
},
|
| 351 |
+
"source": [
|
| 352 |
+
"import zipfile\n",
|
| 353 |
+
"import shutil\n",
|
| 354 |
+
"from pathlib import Path\n",
|
| 355 |
+
"from PIL import Image\n",
|
| 356 |
+
"from tqdm import tqdm\n",
|
| 357 |
+
"\n",
|
| 358 |
+
"# =============================================================================\n",
|
| 359 |
+
"# SETTINGS\n",
|
| 360 |
+
"# =============================================================================\n",
|
| 361 |
+
"\n",
|
| 362 |
+
"input_zip_path = Path('/content/drive/MyDrive/running_animation_top_transparent.zip')\n",
|
| 363 |
+
"output_zip_filename = input_zip_path.name # Keep the same filename for the output zip\n",
|
| 364 |
+
"drive_root = Path(\"/content/drive/MyDrive\")\n",
|
| 365 |
+
"\n",
|
| 366 |
+
"# =============================================================================\n",
|
| 367 |
+
"# PREPARE WORKING DIRECTORIES\n",
|
| 368 |
+
"# =============================================================================\n",
|
| 369 |
+
"\n",
|
| 370 |
+
"base_dir_name = input_zip_path.stem.replace('_transparent', '_halved_top') # Unique base dir\n",
|
| 371 |
+
"base_dir = Path(\"/content\") / base_dir_name\n",
|
| 372 |
+
"\n",
|
| 373 |
+
"if base_dir.exists():\n",
|
| 374 |
+
" shutil.rmtree(base_dir)\n",
|
| 375 |
+
"\n",
|
| 376 |
+
"input_extract_dir = base_dir / \"extracted_images\"\n",
|
| 377 |
+
"output_top_halves_dir = base_dir / \"top_halves\"\n",
|
| 378 |
+
"\n",
|
| 379 |
+
"input_extract_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 380 |
+
"output_top_halves_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 381 |
+
"\n",
|
| 382 |
+
"# =============================================================================\n",
|
| 383 |
+
"# EXTRACT ZIP\n",
|
| 384 |
+
"# =============================================================================\n",
|
| 385 |
+
"\n",
|
| 386 |
+
"print(f\"Extracting {input_zip_path.name} to {input_extract_dir}...\")\n",
|
| 387 |
+
"with zipfile.ZipFile(input_zip_path, \"r\") as z:\n",
|
| 388 |
+
" z.extractall(input_extract_dir)\n",
|
| 389 |
+
"print(\"ZIP extracted successfully.\")\n",
|
| 390 |
+
"\n",
|
| 391 |
+
"# =============================================================================\n",
|
| 392 |
+
"# FIND IMAGES AND PROCESS\n",
|
| 393 |
+
"# =============================================================================\n",
|
| 394 |
+
"\n",
|
| 395 |
+
"IMAGE_EXTENSIONS = {\".jpg\", \".jpeg\", \".png\", \".webp\", \".bmp\", \".tif\", \".tiff\"}\n",
|
| 396 |
+
"image_files = []\n",
|
| 397 |
+
"for p in input_extract_dir.rglob(\"*\"):\n",
|
| 398 |
+
" if p.is_file() and p.suffix.lower() in IMAGE_EXTENSIONS:\n",
|
| 399 |
+
" image_files.append(p)\n",
|
| 400 |
+
"\n",
|
| 401 |
+
"if not image_files:\n",
|
| 402 |
+
" raise FileNotFoundError(\"No images found in the extracted ZIP file.\")\n",
|
| 403 |
+
"\n",
|
| 404 |
+
"print(f\"Found {len(image_files)} images. Cutting them in half...\")\n",
|
| 405 |
+
"\n",
|
| 406 |
+
"processed_count = 0\n",
|
| 407 |
+
"for image_path in tqdm(image_files, desc=\"Processing images\"):\n",
|
| 408 |
+
" try:\n",
|
| 409 |
+
" img = Image.open(image_path).convert(\"RGBA\") # Keep alpha channel\n",
|
| 410 |
+
" width, height = img.size\n",
|
| 411 |
+
"\n",
|
| 412 |
+
" # Define the halfway point\n",
|
| 413 |
+
" half_height = height // 2\n",
|
| 414 |
+
"\n",
|
| 415 |
+
" # Crop the top half\n",
|
| 416 |
+
" top_half = img.crop((0, 0, width, half_height))\n",
|
| 417 |
+
"\n",
|
| 418 |
+
" # Save the top half to the output directory, preserving subfolder structure\n",
|
| 419 |
+
" relative_path = image_path.relative_to(input_extract_dir)\n",
|
| 420 |
+
" output_half_path = output_top_halves_dir / relative_path\n",
|
| 421 |
+
"\n",
|
| 422 |
+
" output_half_path.parent.mkdir(parents=True, exist_ok=True)\n",
|
| 423 |
+
" top_half.save(output_half_path, format=img.format) # Save in original format\n",
|
| 424 |
+
" processed_count += 1\n",
|
| 425 |
+
"\n",
|
| 426 |
+
" except Exception as e:\n",
|
| 427 |
+
" print(f\"Error processing {image_path.name}: {e}\")\n",
|
| 428 |
+
"\n",
|
| 429 |
+
"print(f\"Processed {processed_count} images. Creating new ZIP file...\")\n",
|
| 430 |
+
"\n",
|
| 431 |
+
"# =============================================================================\n",
|
| 432 |
+
"# CREATE NEW ZIP WITH TOP HALVES\n",
|
| 433 |
+
"# =============================================================================\n",
|
| 434 |
+
"\n",
|
| 435 |
+
"output_zip_base_name = base_dir / input_zip_path.stem # e.g., /content/running_animation_top_transparent/running_animation_top_transparent\n",
|
| 436 |
+
"\n",
|
| 437 |
+
"# Ensure no old temporary zip file exists\n",
|
| 438 |
+
"if output_zip_base_name.with_suffix(\".zip\").exists():\n",
|
| 439 |
+
" output_zip_base_name.with_suffix(\".zip\").unlink()\n",
|
| 440 |
+
"\n",
|
| 441 |
+
"shutil.make_archive(str(output_zip_base_name), \"zip\", output_top_halves_dir)\n",
|
| 442 |
+
"print(f\"New ZIP '{output_zip_filename}' created successfully.\")\n",
|
| 443 |
+
"\n",
|
| 444 |
+
"# =============================================================================\n",
|
| 445 |
+
"# SAVE TO GOOGLE DRIVE AND CLEAN UP\n",
|
| 446 |
+
"# =============================================================================\n",
|
| 447 |
+
"\n",
|
| 448 |
+
"final_destination_path = drive_root / output_zip_filename\n",
|
| 449 |
+
"\n",
|
| 450 |
+
"# Overwrite if exists\n",
|
| 451 |
+
"if final_destination_path.exists():\n",
|
| 452 |
+
" final_destination_path.unlink()\n",
|
| 453 |
+
"\n",
|
| 454 |
+
"shutil.move(str(output_zip_base_name.with_suffix(\".zip\")), str(final_destination_path))\n",
|
| 455 |
+
"print(f\"New ZIP saved to Google Drive: {final_destination_path}\")\n",
|
| 456 |
+
"\n",
|
| 457 |
+
"shutil.rmtree(base_dir)\n",
|
| 458 |
+
"print(f\"Cleaned up temporary directory: {base_dir}\")\n",
|
| 459 |
+
"\n",
|
| 460 |
+
"print(\"\\nImage splitting and zipping complete!\")"
|
| 461 |
+
],
|
| 462 |
+
"execution_count": null,
|
| 463 |
+
"outputs": []
|
| 464 |
+
},
|
| 465 |
+
{
|
| 466 |
+
"cell_type": "code",
|
| 467 |
+
"metadata": {
|
| 468 |
+
"id": "5144d0e5"
|
| 469 |
+
},
|
| 470 |
+
"source": [
|
| 471 |
+
"import zipfile\n",
|
| 472 |
+
"import shutil\n",
|
| 473 |
+
"from pathlib import Path\n",
|
| 474 |
+
"from PIL import Image, ImageDraw\n",
|
| 475 |
+
"from tqdm import tqdm\n",
|
| 476 |
+
"\n",
|
| 477 |
+
"# =============================================================================\n",
|
| 478 |
+
"# SETTINGS\n",
|
| 479 |
+
"# =============================================================================\n",
|
| 480 |
+
"\n",
|
| 481 |
+
"drive_root = Path(\"/content/drive/MyDrive\")\n",
|
| 482 |
+
"showcase_output_filename = \"transparent_images_showcase.png\"\n",
|
| 483 |
+
"target_image_height = 256 # Fixed height for all images in the showcase\n",
|
| 484 |
+
"max_images_per_row = 30 # Maximum images from one zip before starting a new row\n",
|
| 485 |
+
"background_color = \"#181818\" # Dark gray background\n",
|
| 486 |
+
"\n",
|
| 487 |
+
"# =============================================================================\n",
|
| 488 |
+
"# FIND ALL 'TRANSPARENT' ZIP FILES\n",
|
| 489 |
+
"# =============================================================================\n",
|
| 490 |
+
"\n",
|
| 491 |
+
"transparent_zip_files = []\n",
|
| 492 |
+
"print(f\"Searching for 'transparent' zip files in {drive_root} (top-level only)...\")\n",
|
| 493 |
+
"\n",
|
| 494 |
+
"# Use .glob to find .zip files only in the top-level of MyDrive\n",
|
| 495 |
+
"for p in drive_root.glob(\"*.zip\"):\n",
|
| 496 |
+
" if \"transparent\" in p.name.lower():\n",
|
| 497 |
+
" transparent_zip_files.append(p)\n",
|
| 498 |
+
"\n",
|
| 499 |
+
"if not transparent_zip_files:\n",
|
| 500 |
+
" raise FileNotFoundError(\n",
|
| 501 |
+
" \"No ZIP files containing 'transparent' found in Google Drive (top-level). \"\n",
|
| 502 |
+
" \"Please ensure you have processed transparent images and they are zipped.\"\n",
|
| 503 |
+
" )\n",
|
| 504 |
+
"else:\n",
|
| 505 |
+
" print(f\"Found {len(transparent_zip_files)} transparent ZIP file(s):\")\n",
|
| 506 |
+
" for i, zip_file in enumerate(transparent_zip_files):\n",
|
| 507 |
+
" print(f\"{i+1}. {zip_file.name}\")\n",
|
| 508 |
+
"\n",
|
| 509 |
+
"# =============================================================================\n",
|
| 510 |
+
"# PROCESS EACH ZIP AND COLLECT RESIZED IMAGES\n",
|
| 511 |
+
"# =============================================================================\n",
|
| 512 |
+
"\n",
|
| 513 |
+
"all_resized_images_by_zip = [] # Stores lists of (resized_img, original_zip_name) for each zip\n",
|
| 514 |
+
"\n",
|
| 515 |
+
"for idx, zip_filepath in enumerate(transparent_zip_files):\n",
|
| 516 |
+
" print(f\"\\n{'='*10} Processing {zip_filepath.name} ({idx+1}/{len(transparent_zip_files)}) {'='*10}\")\n",
|
| 517 |
+
"\n",
|
| 518 |
+
" current_zip_base_dir = Path(\"/content/showcase_temp\") / zip_filepath.stem\n",
|
| 519 |
+
" input_dir = current_zip_base_dir / \"extracted_images\"\n",
|
| 520 |
+
"\n",
|
| 521 |
+
" if current_zip_base_dir.exists():\n",
|
| 522 |
+
" shutil.rmtree(current_zip_base_dir)\n",
|
| 523 |
+
" input_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 524 |
+
"\n",
|
| 525 |
+
" print(f\"Extracting {zip_filepath.name} to {input_dir}...\")\n",
|
| 526 |
+
" try:\n",
|
| 527 |
+
" with zipfile.ZipFile(zip_filepath, \"r\") as z:\n",
|
| 528 |
+
" z.extractall(input_dir)\n",
|
| 529 |
+
" print(\"ZIP extracted successfully.\")\n",
|
| 530 |
+
" except Exception as e:\n",
|
| 531 |
+
" print(f\"Error extracting {zip_filepath.name}: {e}. Skipping this ZIP.\")\n",
|
| 532 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 533 |
+
" continue\n",
|
| 534 |
+
"\n",
|
| 535 |
+
" IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.webp', '.bmp', '.tif', '.tiff'}\n",
|
| 536 |
+
" image_files = []\n",
|
| 537 |
+
" for p in input_dir.rglob(\"*\"):\n",
|
| 538 |
+
" if p.is_file() and p.suffix.lower() in IMAGE_EXTENSIONS:\n",
|
| 539 |
+
" image_files.append(p)\n",
|
| 540 |
+
"\n",
|
| 541 |
+
" if not image_files:\n",
|
| 542 |
+
" print(f\"No valid image files found in {zip_filepath.name}. Skipping.\")\n",
|
| 543 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 544 |
+
" continue\n",
|
| 545 |
+
"\n",
|
| 546 |
+
" images_for_current_zip = []\n",
|
| 547 |
+
" print(f\"Resizing {len(image_files)} images from {zip_filepath.name}...\")\n",
|
| 548 |
+
" for image_path in tqdm(image_files, desc=f\"Resizing images from {zip_filepath.name}\"):\n",
|
| 549 |
+
" try:\n",
|
| 550 |
+
" img = Image.open(image_path).convert(\"RGBA\")\n",
|
| 551 |
+
" width, height = img.size\n",
|
| 552 |
+
" aspect_ratio = width / height\n",
|
| 553 |
+
" new_width = int(target_image_height * aspect_ratio)\n",
|
| 554 |
+
" resized_img = img.resize((new_width, target_image_height), Image.Resampling.LANCZOS)\n",
|
| 555 |
+
" images_for_current_zip.append(resized_img)\n",
|
| 556 |
+
" except Exception as e:\n",
|
| 557 |
+
" print(f\"Error resizing {image_path.name}: {e}. Skipping.\")\n",
|
| 558 |
+
"\n",
|
| 559 |
+
" # Split images from current zip into chunks for rows\n",
|
| 560 |
+
" for i in range(0, len(images_for_current_zip), max_images_per_row):\n",
|
| 561 |
+
" all_resized_images_by_zip.append(\n",
|
| 562 |
+
" (images_for_current_zip[i:i + max_images_per_row], zip_filepath.name)\n",
|
| 563 |
+
" )\n",
|
| 564 |
+
"\n",
|
| 565 |
+
" shutil.rmtree(current_zip_base_dir, ignore_errors=True)\n",
|
| 566 |
+
" print(f\"Cleaned up temporary directory: {current_zip_base_dir}\")\n",
|
| 567 |
+
"\n",
|
| 568 |
+
"# =============================================================================\n",
|
| 569 |
+
"# CREATE SHOWCASE IMAGE\n",
|
| 570 |
+
"# =============================================================================\n",
|
| 571 |
+
"\n",
|
| 572 |
+
"if not all_resized_images_by_zip:\n",
|
| 573 |
+
" raise RuntimeError(\"No images were successfully processed for the showcase.\")\n",
|
| 574 |
+
"\n",
|
| 575 |
+
"print(\"\\nCreating showcase image...\")\n",
|
| 576 |
+
"\n",
|
| 577 |
+
"# Calculate dimensions for the showcase image\n",
|
| 578 |
+
"showcase_width = 0\n",
|
| 579 |
+
"showcase_height = len(all_resized_images_by_zip) * target_image_height\n",
|
| 580 |
+
"\n",
|
| 581 |
+
"for image_row_chunk, _ in all_resized_images_by_zip:\n",
|
| 582 |
+
" current_row_width = sum(img.width for img in image_row_chunk)\n",
|
| 583 |
+
" if current_row_width > showcase_width:\n",
|
| 584 |
+
" showcase_width = current_row_width\n",
|
| 585 |
+
"\n",
|
| 586 |
+
"# Create the blank canvas\n",
|
| 587 |
+
"showcase_image = Image.new(\"RGBA\", (showcase_width, showcase_height), color=background_color)\n",
|
| 588 |
+
"\n",
|
| 589 |
+
"current_y_offset = 0\n",
|
| 590 |
+
"for image_row_chunk, original_zip_name in tqdm(all_resized_images_by_zip, desc=\"Compositing showcase\"):\n",
|
| 591 |
+
" current_x_offset = 0\n",
|
| 592 |
+
" for img in image_row_chunk:\n",
|
| 593 |
+
" showcase_image.paste(img, (current_x_offset, current_y_offset), img)\n",
|
| 594 |
+
" current_x_offset += img.width\n",
|
| 595 |
+
" current_y_offset += target_image_height\n",
|
| 596 |
+
"\n",
|
| 597 |
+
"# =============================================================================\n",
|
| 598 |
+
"# SAVE SHOWCASE IMAGE TO GOOGLE DRIVE\n",
|
| 599 |
+
"# =============================================================================\n",
|
| 600 |
+
"\n",
|
| 601 |
+
"final_showcase_path = drive_root / showcase_output_filename\n",
|
| 602 |
+
"\n",
|
| 603 |
+
"if final_showcase_path.exists():\n",
|
| 604 |
+
" final_showcase_path.unlink() # Overwrite if it exists\n",
|
| 605 |
+
"\n",
|
| 606 |
+
"showcase_image.save(final_showcase_path)\n",
|
| 607 |
+
"print(f\"\\nShowcase image saved to Google Drive: {final_showcase_path}\")\n",
|
| 608 |
+
"\n",
|
| 609 |
+
"print(\"\\nShowcase creation complete!\")"
|
| 610 |
+
],
|
| 611 |
+
"execution_count": null,
|
| 612 |
+
"outputs": []
|
| 613 |
+
},
|
| 614 |
+
{
|
| 615 |
+
"cell_type": "markdown",
|
| 616 |
+
"metadata": {
|
| 617 |
+
"id": "6973ff74"
|
| 618 |
+
},
|
| 619 |
+
"source": [
|
| 620 |
+
"### Disconnect Runtime"
|
| 621 |
+
]
|
| 622 |
+
},
|
| 623 |
+
{
|
| 624 |
+
"cell_type": "code",
|
| 625 |
+
"metadata": {
|
| 626 |
+
"id": "75027cc1"
|
| 627 |
+
},
|
| 628 |
+
"source": [
|
| 629 |
+
"#@title Auto Disconnect Runtime upon code completion\n",
|
| 630 |
+
"disconnect_runtime = True #@param {type:'boolean'}\n",
|
| 631 |
+
"\n",
|
| 632 |
+
"if disconnect_runtime:\n",
|
| 633 |
+
" from google.colab import runtime\n",
|
| 634 |
+
" runtime.unassign()"
|
| 635 |
+
],
|
| 636 |
+
"execution_count": null,
|
| 637 |
+
"outputs": []
|
| 638 |
+
}
|
| 639 |
+
]
|
| 640 |
+
}
|