From b4f53ef0a976a5930e513cdbfc0b7cb7b453aace Mon Sep 17 00:00:00 2001
From: Edward Johan <53343081+younyokel@users.noreply.github.com>
Date: Sun, 21 May 2023 00:25:36 +0600
Subject: [PATCH] edit captions: added utf8 argument to save, added caption
saved message and fixed img quality typo
---
TO8_Captionr.ipynb | 56 ++++++++++++++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 17 deletions(-)
diff --git a/TO8_Captionr.ipynb b/TO8_Captionr.ipynb
index b2825bf..97b22e2 100644
--- a/TO8_Captionr.ipynb
+++ b/TO8_Captionr.ipynb
@@ -3,15 +3,14 @@
{
"cell_type": "markdown",
"metadata": {
- "colab_type": "text",
- "id": "view-in-github"
+ "id": "view-in-github",
+ "colab_type": "text"
},
"source": [
- "
"
+ "
"
]
},
{
- "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "OGgQmF2rO1DY"
@@ -512,37 +511,60 @@
" ext=\"JPEG\" \n",
"\n",
" if os.path.exists(folder_path+\"/\"+name + '.' + extension):\n",
- " with open(folder_path+\"/\"+name + '.' + extension, 'r') as f:\n",
+ " with open(folder_path+\"/\"+name + '.' + extension, 'r', encoding='utf-8') as f:\n",
" text = f.read()\n",
" else:\n",
- " with open(folder_path+\"/\"+name + '.' + extension, 'w') as f:\n",
+ " with open(folder_path+\"/\"+name + '.' + extension, 'w', encoding='utf-8') as f:\n",
" f.write(\"\")\n",
- " with open(folder_path+\"/\"+name + '.' + extension, 'r') as f:\n",
+ " with open(folder_path+\"/\"+name + '.' + extension, 'r', encoding='utf-8') as f:\n",
" text = f.read() \n",
"\n",
" img=Image.open(os.path.join(folder_path,path))\n",
- " img=img.resize((420, 420))\n",
+ " aspect_ratio = img.width / img.height\n",
+ " max_size = 460 # Set the maximum size here\n",
+ "\n",
+ " # Calculate new dimensions while maintaining aspect ratio\n",
+ " width, height = img.size\n",
+ " aspect_ratio = width / height\n",
+ "\n",
+ " if width > height:\n",
+ " new_width = max_size\n",
+ " new_height = int(new_width / aspect_ratio)\n",
+ " else:\n",
+ " new_height = max_size\n",
+ " new_width = int(new_height * aspect_ratio)\n",
+ "\n",
+ " img = img.resize((new_width, new_height))\n",
" image_bytes = BytesIO()\n",
- " img.save(image_bytes, format=ext, qualiy=10)\n",
+ " img.save(image_bytes, format=ext, quality=80)\n",
" image_bytes.seek(0)\n",
" image_data = image_bytes.read()\n",
- " img= image_data \n",
+ "\n",
" image = widgets.Image(\n",
- " value=img,\n",
- " width=420,\n",
- " height=420\n",
+ " value=image_data,\n",
+ " width=new_width,\n",
+ " height=new_height\n",
" )\n",
" text_area = widgets.Textarea(value=text, description='', disabled=False, layout={'width': '300px', 'height': '120px'})\n",
" \n",
+ " # create a label to display the save status\n",
+ " save_status = widgets.Label(value='')\n",
"\n",
" def update_text(text):\n",
+ " save_status.value = ''\n",
" with open(folder_path+\"/\"+name + '.' + extension, 'w') as f:\n",
" f.write(text)\n",
+ " # update the save status label\n",
+ " save_status.value = 'Image saved!'\n",
"\n",
" button = widgets.Button(description='Save', button_style='success')\n",
" button.on_click(lambda b: update_text(text_area.value))\n",
+ " text_area.observe(lambda change: setattr(save_status, 'value', ''), names='value')\n",
"\n",
- " return widgets.VBox([widgets.HBox([image, text_area, button])])\n",
+ " # change the layout of the widgets\n",
+ " return widgets.VBox([\n",
+ " widgets.HBox([image, widgets.VBox([text_area, button, save_status])])\n",
+ " ])\n",
"\n",
"\n",
"paths = os.listdir(folder_path)\n",
@@ -709,9 +731,9 @@
"metadata": {
"accelerator": "GPU",
"colab": {
- "include_colab_link": true,
"machine_shape": "hm",
- "provenance": []
+ "provenance": [],
+ "include_colab_link": true
},
"gpuClass": "standard",
"kernelspec": {
@@ -731,4 +753,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
-}
+}
\ No newline at end of file