1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""
gallery.py

Code generated with help from ChatGPT

Pradeep Gowda
Nov 18, 2023
"""

import os
from PIL import Image
import markdown

# Define the directory path containing the JPG images
image_directory = os.getcwd()

# Create a directory for thumbnails if it doesn't exist
thumbnail_directory = os.path.join(image_directory, "thumbnails")
os.makedirs(thumbnail_directory, exist_ok=True)


# Function to create a thumbnail for an image
def create_thumbnail(image_path, thumbnail_path, size=(800, 800)):
    if os.path.exists(thumbnail_path):
        print(f"Skip: {thumbnail_path.split('/')[-1]}")
        return
    else:
        print(f"Generating: {thumbnail_path.split('/')[-1]}")
    with Image.open(image_path) as img:
        img.thumbnail(size)
        img.save(thumbnail_path)


def fileinfo(md):
    with open(md) as f:
        content = f.read()
    return markdown.markdown(content)


# Loop through all JPG files in the directory, creating thumbnails
for filename in sorted(os.listdir(image_directory)):
    if filename.lower().endswith(".jpg"):
        original_path = os.path.join(image_directory, filename)
        thumbnail_path = os.path.join(thumbnail_directory, filename)
        create_thumbnail(original_path, thumbnail_path)

# Create an HTML file for the image gallery
html_file = os.path.join(image_directory, "gallery.html")

content = markdown.markdown(
    """# kr̥ṣimēḷa 2023 / ಕೃಷಿಮೇಳ 2023 - ಔಷಧೀಯ ಸಸ್ಯಗಳು

On Sat, Nov 18, 2023, I visited *kr̥ṣimēḷa*   at GKVK campus in Bengaluru.

The photos below are from the **medicinal and aromatic plants** pavilion.

TODO: Add Kannada names by hand.

## Gallery

"""
)

with open(html_file, "w") as file:
    file.write(
        f"""<!doctype html>
<html>
<head>
<title>krishimela 2023 - medicinal plants</title>
<link rel="stylesheet" href="https://www.btbytes.com/css/tacit.css"/>
</head>
<body>
{content}
<table>
<thead>
<td width=5%>#</td>
<td>Plant</td>
<td>Info</td>
</thead>
"""
    )

    footer = markdown.markdown(
        """
Programming Note: I tried using `tesseract` OCR to automatically [extract](extract.html)
(source code) description from the pictures. It did not come out very well. 
Something to finetune again. Maybe I can use some kind of object recognition library to
seperate the white signboard from the background and then run OCR on it.


Source code for this [gallery generator](code.html).

Page generated on Nov 19-20, 2023.

&copy; 2023, [Pradeep Kishore Gowda](https://www.btbytes.com/).
"""
    )

    for filename in sorted(os.listdir(thumbnail_directory)):
        original_path = os.path.join(image_directory, filename).replace(
            "/Users/pradeep", "https:/"
        )
        thumbnail_path = os.path.join(thumbnail_directory, filename).replace(
            "/Users/pradeep", "https:/"
        )
        print(f"Gallery: {filename}")
        file.write("<tr>\n")
        fname = filename.split("/")[-1].split(".")[0]
        md = f"{fname}.md"
        file.write(
            f"""<td width=5%><a id={fname}>{fname}</a></td>
            <td><a href="{original_path}">
            <img src="{thumbnail_path}" alt="{filename}"></a></td>
            """
        )
        file.write(f"<td>{fileinfo(md)}</td>\n")
        file.write("</tr>\n")
    file.write(
        f"""</table>
{footer}    
</body>
</html>"""
    )