Skip to content

Commit 56c2b4d

Browse files
committed
Add the option to include overlay text on saved images
1 parent 4ee19d1 commit 56c2b4d

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

examples/image_test.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
1-
"""
2-
Pull an image from a website and save it as a PNG file.
3-
"""
4-
51
from seleniumbase import BaseCase
62

73

84
class ImageTest(BaseCase):
95

106
def test_pull_image_from_website(self):
7+
""" Pull an image from a website and save it as a PNG file. """
118
self.open("https://xkcd.com/1117/")
129
selector = "#comic"
1310
file_name = "comic.png"
1411
folder = "images_exported"
1512
self.save_element_as_image_file(selector, file_name, folder)
1613
print('"%s/%s" has been saved!' % (folder, file_name))
14+
15+
def test_add_text_overlay_to_image(self):
16+
""" Add a text overlay to an image. """
17+
self.open("https://xkcd.com/1117/")
18+
selector = "#comic"
19+
file_name = "image_overlay.png"
20+
folder = "images_exported"
21+
overlay_text = 'This is an XKCD comic!\nTitle: "My Sky"'
22+
self.save_element_as_image_file(
23+
selector, file_name, folder, overlay_text)
24+
print('"%s/%s" has been saved!' % (folder, file_name))
25+
26+
def test_add_text_overlay_to_page_section(self):
27+
""" Add a text overlay to a section of a page. """
28+
self.open("https://xkcd.com/2200/")
29+
selector = "#middleContainer"
30+
file_name = "section_overlay.png"
31+
folder = "images_exported"
32+
overlay_text = (
33+
'Welcome to %s\n'
34+
'This is a comment added to the image.\n'
35+
'Unreachable states come from logic errors.'
36+
% self.get_current_url())
37+
self.save_element_as_image_file(
38+
selector, file_name, folder, overlay_text)
39+
print('"%s/%s" has been saved!' % (folder, file_name))
40+
41+
def test_add_text_overlay_to_full_page(self):
42+
""" Add a text overlay to a full page. """
43+
self.open("https://xkcd.com/1922/")
44+
self.remove_element("#bottom")
45+
selector = "body"
46+
file_name = "page_overlay.png"
47+
folder = "images_exported"
48+
overlay_text = ("A text overlay on %s" % self.get_current_url())
49+
self.save_element_as_image_file(
50+
selector, file_name, folder, overlay_text)
51+
print('"%s/%s" has been saved!' % (folder, file_name))

help_docs/method_summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ self.create_folder(folder)
277277

278278
self.choose_file(selector, file_path, by=By.CSS_SELECTOR, timeout=None)
279279

280-
self.save_element_as_image_file(selector, file_name, folder=None)
280+
self.save_element_as_image_file(selector, file_name, folder=None, overlay_text="")
281281

282282
self.download_file(file_url, destination_folder=None)
283283

seleniumbase/fixtures/base_case.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,9 +2784,11 @@ def choose_file(self, selector, file_path, by=By.CSS_SELECTOR,
27842784
abs_path = os.path.abspath(file_path)
27852785
self.add_text(selector, abs_path, by=by, timeout=timeout)
27862786

2787-
def save_element_as_image_file(self, selector, file_name, folder=None):
2787+
def save_element_as_image_file(
2788+
self, selector, file_name, folder=None, overlay_text=""):
27882789
""" Take a screenshot of an element and save it as an image file.
2789-
If no folder is specified, will save it to the current folder. """
2790+
If no folder is specified, will save it to the current folder.
2791+
If overlay_text is provided, will add that to the saved image. """
27902792
element = self.wait_for_element_visible(selector)
27912793
element_png = element.screenshot_as_png
27922794
if len(file_name.split('.')[0]) < 1:
@@ -2804,6 +2806,26 @@ def save_element_as_image_file(self, selector, file_name, folder=None):
28042806
image_file_path = file_name
28052807
with open(image_file_path, "wb") as file:
28062808
file.write(element_png)
2809+
# Add a text overlay if given
2810+
if type(overlay_text) is str and len(overlay_text) > 0:
2811+
from PIL import Image, ImageDraw
2812+
text_rows = overlay_text.split("\n")
2813+
len_text_rows = len(text_rows)
2814+
max_width = 0
2815+
for text_row in text_rows:
2816+
if len(text_row) > max_width:
2817+
max_width = len(text_row)
2818+
image = Image.open(image_file_path)
2819+
draw = ImageDraw.Draw(image)
2820+
draw.rectangle(
2821+
(0, 0, (max_width * 6) + 6, 16 * len_text_rows),
2822+
fill=(236, 236, 28))
2823+
draw.text(
2824+
(4, 2), # Coordinates
2825+
overlay_text, # Text
2826+
(8, 38, 176) # Color
2827+
)
2828+
image.save(image_file_path, 'PNG', quality=100, optimize=True)
28072829

28082830
def download_file(self, file_url, destination_folder=None):
28092831
""" Downloads the file from the url to the destination folder.

0 commit comments

Comments
 (0)