Datasets:
Tags:
Not-For-All-Audiences
Upload Vertical_slice.ipynb
Browse files- Vertical_slice.ipynb +1 -0
Vertical_slice.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"cells":[{"cell_type":"code","source":["import os\n","import io\n","import random\n","from google.colab import files\n","from PIL import Image\n","import cv2\n","import numpy as np\n","\n","# ββββββββββββββββββββββββββββββββββββββββββββββββ\n","# Number of frames to produce\n","# ββββββββββββββββββββββββββββββββββββββββββββββββ\n","num_frames = 125 #@param {type:\"slider\", min:1, max:150, step:1}\n","\n","# ββββββββββββββββββββββββββββββββββββββββββββββββ\n","# Upload your images/videos\n","# ββββββββββββββββββββββββββββββββββββββββββββββββ\n","print(\"Upload your jpg, jpeg, png, webp or webm files\")\n","uploaded = files.upload()\n","\n","# Collect images\n","images = []\n","\n","for filename, filedata in uploaded.items():\n"," fname_lower = filename.lower()\n"," if fname_lower.endswith(('.jpg', '.jpeg', '.png', '.webp')):\n"," try:\n"," img = Image.open(io.BytesIO(filedata)).convert('RGB')\n"," images.append(img)\n"," except:\n"," print(f\"Could not open image: {filename}\")\n"," elif fname_lower.endswith('.webm'):\n"," try:\n"," # Read first frame from webm\n"," cap = cv2.VideoCapture(filename)\n"," ret, frame = cap.read()\n"," cap.release()\n"," if ret:\n"," frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n"," img = Image.fromarray(frame_rgb)\n"," images.append(img)\n"," else:\n"," print(f\"Could not read video frame: {filename}\")\n"," except:\n"," print(f\"Error processing video: {filename}\")\n","\n","if not images:\n"," print(\"No valid images were loaded. Please upload jpg/jpeg/png/webp/webm files.\")\n","else:\n"," print(f\"Loaded {len(images)} images\")\n","\n"," # ββββββββββββββββββββββββββββββββββββββββββββββββ\n"," # Settings\n"," # ββββββββββββββββββββββββββββββββββββββββββββββββ\n"," FRAME_SIZE = 1024\n"," BORDER_PX = 14\n"," INNER_SIZE = FRAME_SIZE - 2 * BORDER_PX # 996\n"," NUM_SLICES = 12\n"," SLICE_WIDTH = INNER_SIZE // NUM_SLICES # 83 px (996 // 12 = 83)\n"," BORDER_COLOR = (24, 24, 24) # #181818\n","\n"," # ββββββββββββββββββββββββββββββββββββββββββββββββ\n"," # Extract multiple vertical slices from each image\n"," # ββββββββββββββββββββββββββββββββββββββββββββββββ\n"," all_slices = []\n"," target_w = SLICE_WIDTH\n"," target_h = INNER_SIZE\n","\n"," for idx, photo in enumerate(images):\n"," w, h = photo.size\n"," if h == 0 or w == 0:\n"," continue # skip invalid\n","\n"," scale = float(target_h) / h\n"," new_w = int(w * scale)\n"," new_h = target_h\n","\n"," if new_w <= 0:\n"," continue\n","\n"," resized = photo.resize((new_w, new_h), Image.LANCZOS)\n","\n"," if new_w < target_w:\n"," # Pad to one centered slice\n"," slice_img = Image.new('RGB', (target_w, target_h), BORDER_COLOR)\n"," paste_x = (target_w - new_w) // 2\n"," slice_img.paste(resized, (paste_x, 0))\n"," all_slices.append(slice_img)\n"," else:\n"," num_slices_from_this = new_w // target_w\n"," if num_slices_from_this < 1:\n"," num_slices_from_this = 1 # shouldn't happen\n"," total_slices_width = num_slices_from_this * target_w\n"," start_left = (new_w - total_slices_width) // 2\n"," for i in range(num_slices_from_this):\n"," left = start_left + i * target_w\n"," crop = resized.crop((left, 0, left + target_w, target_h))\n"," all_slices.append(crop)\n","\n"," print(f\"Extracted {len(all_slices)} unique slices from {len(images)} images\")\n","\n"," if not all_slices:\n"," print(\"No slices extracted. Check image dimensions.\")\n"," else:\n"," output_dir = \"vertical_slice_frames\"\n"," os.makedirs(output_dir, exist_ok=True)\n","\n"," # To ensure uniqueness to best ability: shuffle the slices once\n"," # Then sample without replacement for each frame; if not enough, reshuffle and reuse\n"," total_needed = num_frames * NUM_SLICES\n"," if len(all_slices) < total_needed:\n"," print(f\"Note: Only {len(all_slices)} slices available, but {total_needed} needed. Will reuse slices as necessary.\")\n","\n"," # Create a shuffled copy of slices for selection\n"," shuffled_slices = all_slices[:]\n"," random.shuffle(shuffled_slices)\n","\n"," frame_idx = 0\n"," used_slices = set() # Track used slices across frames (by id)\n","\n"," while frame_idx < num_frames:\n"," # Select 12 unique slices for this frame, preferring unused ones\n"," group = []\n"," available = [slc for slc in shuffled_slices if id(slc) not in used_slices]\n","\n"," if len(available) < NUM_SLICES:\n"," # Not enough unused; reshuffle all and reset used if necessary\n"," shuffled_slices = all_slices[:]\n"," random.shuffle(shuffled_slices)\n"," used_slices.clear()\n"," available = shuffled_slices[:]\n","\n"," # Take up to 12\n"," for _ in range(NUM_SLICES):\n"," if available:\n"," slc = available.pop(0)\n"," group.append(slc)\n"," used_slices.add(id(slc))\n"," else:\n"," # Fallback: use a random one if somehow exhausted\n"," group.append(random.choice(all_slices))\n","\n"," # Create new 996Γ996 inner canvas\n"," canvas = Image.new('RGB', (INNER_SIZE, INNER_SIZE), (0, 0, 0))\n","\n"," for col in range(NUM_SLICES):\n"," if col < len(group):\n"," crop = group[col]\n"," else:\n"," # filler for missing (shouldn't happen)\n"," crop = Image.new('RGB', (target_w, target_h), BORDER_COLOR)\n","\n"," # Paste into canvas\n"," x = col * SLICE_WIDTH\n"," canvas.paste(crop, (x, 0))\n","\n"," # ββ Add gray border around the whole thing βββββββββββββββββββββββββββ\n"," final = Image.new('RGB', (FRAME_SIZE, FRAME_SIZE), BORDER_COLOR)\n"," final.paste(canvas, (BORDER_PX, BORDER_PX))\n","\n"," # Save\n"," frame_idx += 1\n"," out_path = os.path.join(output_dir, f\"frame_{frame_idx:03d}.jpg\")\n"," final.save(out_path, \"JPEG\", quality=92)\n"," print(f\"Saved: {out_path}\")\n","\n"," print(f\"\\nDone. Created {frame_idx} frames in folder: /{output_dir}\")\n","\n"," # Optional: zip and download\n"," zip_name = \"vertical_frames.zip\"\n"," !zip -r -q {zip_name} {output_dir}\n"," files.download(zip_name)"],"metadata":{"id":"uQNPVVghmKza"},"execution_count":null,"outputs":[]}],"metadata":{"colab":{"provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1772741185417},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1763646205520},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1760993725927},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1760450712160},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1756712618300},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1747490904984},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1740037333374},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1736477078136},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1725365086834}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
|