memex-in commited on
Commit
4c475a5
·
verified ·
1 Parent(s): 06a0302

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from selenium import webdriver
4
+ from selenium.webdriver.chrome.options import Options
5
+ import ascii_magic
6
+
7
+ # Function to capture a screenshot of a webpage
8
+ def capture_screenshot(url, output_path='screenshot.png'):
9
+ chrome_options = Options()
10
+ chrome_options.add_argument('--headless') # Run Chrome in headless mode
11
+ chrome_options.add_argument('--disable-gpu') # Disable GPU acceleration
12
+ chrome_options.add_argument('--no-sandbox') # Disable sandbox for better compatibility
13
+ chrome_options.add_argument('--window-size=1920,1080') # Set window size for consistent screenshots
14
+
15
+ driver = webdriver.Chrome(options=chrome_options)
16
+ try:
17
+ driver.get(url) # Open the URL
18
+ driver.save_screenshot(output_path) # Save screenshot to file
19
+ finally:
20
+ driver.quit() # Ensure the driver is closed even if an error occurs
21
+
22
+ # Function to convert an image to ASCII art
23
+ def convert_to_ascii(image_path):
24
+ try:
25
+ output = ascii_magic.from_image_path(image_path, columns=100, mode=ascii_magic.Modes.ASCII)
26
+ return str(output)
27
+ except Exception as e:
28
+ raise Exception(f"Failed to convert image to ASCII: {e}")
29
+
30
+ # Streamlit app layout
31
+ st.title("ASCII Web Screenshot Viewer")
32
+
33
+ # Input field for URL
34
+ url = st.text_input("Enter a website URL:", "https://example.com")
35
+
36
+ # Button to trigger the screenshot and conversion process
37
+ if st.button("Generate ASCII Screenshot"):
38
+ try:
39
+ screenshot_file = "screenshot.png"
40
+ st.write("Capturing website screenshot...")
41
+ capture_screenshot(url, screenshot_file)
42
+
43
+ st.write("Converting to ASCII art...")
44
+ ascii_output = convert_to_ascii(screenshot_file)
45
+
46
+ # Display ASCII art in a text area
47
+ st.text_area("ASCII Art Output", ascii_output, height=600)
48
+
49
+ except Exception as e:
50
+ st.error(f"An error occurred: {e}")
51
+ finally:
52
+ # Clean up the screenshot file if it exists
53
+ if os.path.exists(screenshot_file):
54
+ os.remove(screenshot_file)