|
2 | 2 | __docformat__ = "numpy"
|
3 | 3 | # pylint: disable=too-many-lines
|
4 | 4 | import argparse
|
| 5 | +import io |
5 | 6 | import logging
|
6 | 7 | from pathlib import Path
|
7 | 8 | from typing import List, Union, Optional, Dict
|
|
35 | 36 | import yfinance as yf
|
36 | 37 | import numpy as np
|
37 | 38 |
|
| 39 | +from PIL import Image, ImageDraw |
| 40 | + |
38 | 41 | from openbb_terminal.rich_config import console
|
39 | 42 | from openbb_terminal import feature_flags as obbff
|
40 | 43 | from openbb_terminal import config_plot as cfgPlot
|
@@ -1671,6 +1674,109 @@ def search_wikipedia(expression: str) -> None:
|
1671 | 1674 | )
|
1672 | 1675 |
|
1673 | 1676 |
|
| 1677 | +def screenshot() -> None: |
| 1678 | + """ |
| 1679 | + Screenshot the terminal window or the plot window |
| 1680 | +
|
| 1681 | + Parameters |
| 1682 | + ---------- |
| 1683 | + terminal_window_target: bool |
| 1684 | + Target the terminal window |
| 1685 | + """ |
| 1686 | + try: |
| 1687 | + if plt.get_fignums(): |
| 1688 | + img_buf = io.BytesIO() |
| 1689 | + plt.savefig(img_buf, format="png") |
| 1690 | + shot = Image.open(img_buf) |
| 1691 | + screenshot_to_canvas(shot, plot_exists=True) |
| 1692 | + console.print("") |
| 1693 | + |
| 1694 | + else: |
| 1695 | + console.print("No plots found.\n") |
| 1696 | + |
| 1697 | + except Exception as e: |
| 1698 | + console.print(f"Cannot reach window - {e}\n") |
| 1699 | + |
| 1700 | + |
| 1701 | +def screenshot_to_canvas(shot, plot_exists: bool = False): |
| 1702 | + """ |
| 1703 | + Frame image to OpenBB canvas. |
| 1704 | +
|
| 1705 | + Parameters |
| 1706 | + ---------- |
| 1707 | + shot |
| 1708 | + Image to frame with OpenBB Canvas |
| 1709 | + plot_exists: bool |
| 1710 | + Variable to say whether the image is a plot or screenshot of terminal |
| 1711 | + """ |
| 1712 | + |
| 1713 | + WHITE_LINE_WIDTH = 3 |
| 1714 | + OUTSIDE_CANVAS_WIDTH = shot.width + 4 * WHITE_LINE_WIDTH + 5 |
| 1715 | + OUTSIDE_CANVAS_HEIGHT = shot.height + 4 * WHITE_LINE_WIDTH + 5 |
| 1716 | + UPPER_SPACE = 40 |
| 1717 | + BACKGROUND_WIDTH_SLACK = 150 |
| 1718 | + BACKGROUND_HEIGHT_SLACK = 150 |
| 1719 | + |
| 1720 | + background = Image.open(Path("images/background.png")) |
| 1721 | + logo = Image.open(Path("images/openbb_horizontal_logo.png")) |
| 1722 | + |
| 1723 | + try: |
| 1724 | + if plot_exists: |
| 1725 | + HEADER_HEIGHT = 0 |
| 1726 | + RADIUS = 8 |
| 1727 | + |
| 1728 | + background = background.resize( |
| 1729 | + ( |
| 1730 | + shot.width + BACKGROUND_WIDTH_SLACK, |
| 1731 | + shot.height + BACKGROUND_HEIGHT_SLACK, |
| 1732 | + ) |
| 1733 | + ) |
| 1734 | + |
| 1735 | + x = int((background.width - OUTSIDE_CANVAS_WIDTH) / 2) |
| 1736 | + y = UPPER_SPACE |
| 1737 | + |
| 1738 | + white_shape = [ |
| 1739 | + (x, y), |
| 1740 | + (x + OUTSIDE_CANVAS_WIDTH, y + OUTSIDE_CANVAS_HEIGHT), |
| 1741 | + ] |
| 1742 | + img = ImageDraw.Draw(background) |
| 1743 | + img.rounded_rectangle( |
| 1744 | + white_shape, |
| 1745 | + fill="black", |
| 1746 | + outline="white", |
| 1747 | + width=WHITE_LINE_WIDTH, |
| 1748 | + radius=RADIUS, |
| 1749 | + ) |
| 1750 | + background.paste(shot, (x + WHITE_LINE_WIDTH + 5, y + WHITE_LINE_WIDTH + 5)) |
| 1751 | + |
| 1752 | + # Logo |
| 1753 | + background.paste( |
| 1754 | + logo, |
| 1755 | + ( |
| 1756 | + int((background.width - logo.width) / 2), |
| 1757 | + UPPER_SPACE |
| 1758 | + + OUTSIDE_CANVAS_HEIGHT |
| 1759 | + + HEADER_HEIGHT |
| 1760 | + + int( |
| 1761 | + ( |
| 1762 | + background.height |
| 1763 | + - UPPER_SPACE |
| 1764 | + - OUTSIDE_CANVAS_HEIGHT |
| 1765 | + - HEADER_HEIGHT |
| 1766 | + - logo.height |
| 1767 | + ) |
| 1768 | + / 2 |
| 1769 | + ), |
| 1770 | + ), |
| 1771 | + logo, |
| 1772 | + ) |
| 1773 | + |
| 1774 | + background.show(title="screenshot") |
| 1775 | + |
| 1776 | + except Exception: |
| 1777 | + console.print("Shot failed.") |
| 1778 | + |
| 1779 | + |
1674 | 1780 | @lru_cache
|
1675 | 1781 | def load_json(path: str) -> Dict[str, str]:
|
1676 | 1782 | """Loads a dictionary from a json file path
|
|
0 commit comments