{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91249,"databundleVersionId":11294684,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Blended Image Animation\n\n","metadata":{}},{"cell_type":"markdown","source":"Sequentially blend five images at a time and use them for animation.","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport glob\nimport pandas as pd\nimport random\nimport os\n!mkdir train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T08:05:05.251357Z","iopub.execute_input":"2025-03-12T08:05:05.251718Z","iopub.status.idle":"2025-03-12T08:05:05.255310Z","shell.execute_reply.started":"2025-03-12T08:05:05.251694Z","shell.execute_reply":"2025-03-12T08:05:05.254457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"names0=os.listdir('/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/')\nprint(len(names0))\nnames=random.sample(names0,5)\nprint(names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T08:22:15.103518Z","iopub.execute_input":"2025-03-12T08:22:15.103807Z","iopub.status.idle":"2025-03-12T08:22:15.110365Z","shell.execute_reply.started":"2025-03-12T08:22:15.103786Z","shell.execute_reply":"2025-03-12T08:22:15.109721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for name in names:\n    folder_path =\"/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/\"+name+\"/*.jpg\"\n    image_files = sorted(glob.glob(folder_path))\n    images = [\n        Image.open(img).resize((Image.open(img).width // 2, Image.open(img).height // 2), Image.LANCZOS)\n        for img in image_files\n    ]\n    images2 = []\n    for i in range(0, len(images), 5):\n        blended = Image.blend(images[i], images[i+1], alpha=0.20)\n        for j in range(i+2, i+5):\n            blended = Image.blend(blended, images[j], alpha=0.20)\n        images2.append(blended)\n    images2[0].save('./train/'+name+\".gif\", save_all=True, append_images=images2[1:], duration=200, loop=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T08:05:05.413263Z","iopub.execute_input":"2025-03-12T08:05:05.413516Z","iopub.status.idle":"2025-03-12T08:05:30.475036Z","shell.execute_reply.started":"2025-03-12T08:05:05.413491Z","shell.execute_reply":"2025-03-12T08:05:30.473960Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"paths=[]\nfor dirname, _, filenames in os.walk('./train'):\n    for filename in filenames:\n        paths+=[(os.path.join(dirname, filename))]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T08:11:47.641391Z","iopub.execute_input":"2025-03-12T08:11:47.641646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Image\n\npath=paths[0]\nprint(path.split('/')[-1])\nImage(open(path,'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=paths[1]\nprint(path.split('/')[-1])\nImage(open(path,'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=paths[2]\nprint(path.split('/')[-1])\nImage(open(path,'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=paths[3]\nprint(path.split('/')[-1])\nImage(open(path,'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=paths[4]\nprint(path.split('/')[-1])\nImage(open(path,'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}