{ "cells": [ { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "import os\n", "import random\n", "from PIL import Image, ImageDraw, ImageFont\n" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "def create_text_thumbnail(file_path):\n", " # Create a folder for thumbnails if it doesn't exist\n", " thumbnail_folder = os.path.join(os.path.dirname(file_path), 'thumbnails')\n", " os.makedirs(thumbnail_folder, exist_ok=True)\n", " \n", " # Extract file name (without extension)\n", " file_name = os.path.splitext(os.path.basename(file_path))[0]\n", " \n", " # Create a random background color\n", " background_color = tuple(random.randint(0, 255) for _ in range(3))\n", " \n", " # Create an image with the random background color\n", " img = Image.new('RGB', (800, 400), color=background_color)\n", " \n", " # Initialize drawing context\n", " d = ImageDraw.Draw(img)\n", " \n", " # Load a font\n", " try:\n", " font = ImageFont.truetype(\"arial.ttf\", 25) # Adjust the font size as needed\n", " except IOError:\n", " font = ImageFont.load_default()\n", " \n", " # Get the bounding box of the text\n", " text_bbox = d.textbbox((0, 0), file_name, font=font)\n", " text_width = text_bbox[2] - text_bbox[0]\n", " text_height = text_bbox[3] - text_bbox[1]\n", " \n", " # Calculate the position to center the text\n", " text_x = (img.width - text_width) / 2\n", " text_y = (img.height - text_height) / 2\n", " \n", " # Draw the text onto the image\n", " d.text((text_x, text_y), file_name, font=font, fill=(255, 255, 255)) # White text\n", " \n", " # Save the image\n", " thumbnail_path = os.path.join(thumbnail_folder, f\"{file_name}.png\")\n", " img.save(thumbnail_path)\n", " \n", " print(f\"Thumbnail created: {thumbnail_path}\")\n", "\n", "def process_directory(directory_path):\n", " supported_extensions = ['.txt', '.pdf', '.docx', '.mp3', '.m4a']\n", " \n", " for file in os.listdir(directory_path):\n", " file_path = os.path.join(directory_path, file)\n", " if os.path.isfile(file_path):\n", " file_extension = os.path.splitext(file)[1].lower()\n", " if file_extension in supported_extensions:\n", " create_text_thumbnail(file_path)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Thumbnail created: data\\thumbnails\\audio-2.png\n", "Thumbnail created: data\\thumbnails\\Car-Repair-Receipt-repair.png\n", "Thumbnail created: data\\thumbnails\\Car-Repair-Receipt-service.png\n", "Thumbnail created: data\\thumbnails\\Car-Repair-Receipt-tire.png\n", "Thumbnail created: data\\thumbnails\\Car-Repair-Receipt-tuning.png\n", "Thumbnail created: data\\thumbnails\\Car-Repair-Receipt-wash.png\n", "Thumbnail created: data\\thumbnails\\corolla-2020-toyota-owners-manual.png\n", "Thumbnail created: data\\thumbnails\\How to change engine oil and filter on TOYOTA Corolla.png\n", "Thumbnail created: data\\thumbnails\\How to change front brake pads on TOYOTA Corolla.png\n", "Thumbnail created: data\\thumbnails\\How to change rear windshield wipers on TOYOTA Corolla.png\n", "Thumbnail created: data\\thumbnails\\How to change spark plugs on TOYOTA COROLLA.png\n", "Thumbnail created: data\\thumbnails\\test_rec.png\n" ] } ], "source": [ "# Example usage:\n", "directory_path = 'data'\n", "process_directory(directory_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "smog_env", "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }