56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Simple Example: Highlight a Word on Tap
|
|
|
|
This is the minimal example showing how to:
|
|
1. Load an ebook
|
|
2. Simulate a tap
|
|
3. Find the word at that location
|
|
4. Highlight it
|
|
|
|
Perfect for understanding the basic query system.
|
|
"""
|
|
|
|
from PIL import Image, ImageDraw
|
|
from dreader.application import EbookReader
|
|
|
|
|
|
def main():
|
|
# 1. Create reader and load book
|
|
reader = EbookReader(page_size=(800, 1000))
|
|
reader.load_epub("tests/data/test.epub")
|
|
|
|
# 2. Get current page as image
|
|
page_img = reader.get_current_page()
|
|
|
|
# 3. Simulate a tap at pixel coordinates
|
|
tap_x, tap_y = 200, 300
|
|
result = reader.query_pixel(tap_x, tap_y)
|
|
|
|
# 4. If we found a word, highlight it
|
|
if result and result.text:
|
|
print(f"Tapped on word: '{result.text}'")
|
|
print(f"Bounds: {result.bounds}")
|
|
|
|
# Draw yellow highlight
|
|
x, y, w, h = result.bounds
|
|
overlay = Image.new('RGBA', page_img.size, (255, 255, 255, 0))
|
|
draw = ImageDraw.Draw(overlay)
|
|
draw.rectangle([x, y, x + w, y + h], fill=(255, 255, 0, 100))
|
|
|
|
# Combine and save
|
|
highlighted = Image.alpha_composite(
|
|
page_img.convert('RGBA'),
|
|
overlay
|
|
)
|
|
highlighted.save("highlighted_word.png")
|
|
print("Saved: highlighted_word.png")
|
|
else:
|
|
print("No word found at that location")
|
|
|
|
reader.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|