Merge branch 'main' into fullscreen_weather_module
This commit is contained in:
		| @@ -321,7 +321,7 @@ splitting the text into the next chunk.</p></li> | ||||
| <dl class="py function"> | ||||
| <dt class="sig sig-object py" id="inkycal.custom.functions.write"> | ||||
| <span class="sig-prename descclassname"><span class="pre">inkycal.custom.functions.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">image</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">xy</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">box_size</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">font</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inkycal.custom.functions.write" title="Link to this definition"></a></dt> | ||||
| <dd><p>Writes text on a image.</p> | ||||
| <dd><p>Writes text on an image.</p> | ||||
| <p>Writes given text at given position on the specified image.</p> | ||||
| <dl class="simple"> | ||||
| <dt>Args:</dt><dd><ul class="simple"> | ||||
| @@ -340,9 +340,9 @@ as much of the box-height as possible.</p></li> | ||||
| <li><p>colour: black by default, do not change as it causes issues with rendering | ||||
| on e-Paper.</p></li> | ||||
| <li><p>rotation: Rotate the text with the text-box by a given angle anti-clockwise.</p></li> | ||||
| <li><p>fill_width: Decimal representing a percentage e.g. 0.9 # 90%. Fill a | ||||
| <li><p>fill_width: Decimal representing a percentage e.g. 0.9 # 90%. Fill | ||||
| maximum of 90% of the size of the full width of text-box.</p></li> | ||||
| <li><p>fill_height: Decimal representing a percentage e.g. 0.9 # 90%. Fill a | ||||
| <li><p>fill_height: Decimal representing a percentage e.g. 0.9 # 90%. Fill | ||||
| maximum of 90% of the size of the full height of the text-box.</p></li> | ||||
| </ul> | ||||
| </dd> | ||||
|   | ||||
| @@ -116,7 +116,7 @@ def auto_fontsize(font, max_height): | ||||
|  | ||||
|  | ||||
| def write(image, xy, box_size, text, font=None, **kwargs): | ||||
|     """Writes text on a image. | ||||
|     """Writes text on an image. | ||||
|  | ||||
|     Writes given text at given position on the specified image. | ||||
|  | ||||
| @@ -135,9 +135,9 @@ def write(image, xy, box_size, text, font=None, **kwargs): | ||||
|       - colour: black by default, do not change as it causes issues with rendering | ||||
|         on e-Paper. | ||||
|       - rotation: Rotate the text with the text-box by a given angle anti-clockwise. | ||||
|       - fill_width: Decimal representing a percentage e.g. 0.9 # 90%. Fill a | ||||
|       - fill_width: Decimal representing a percentage e.g. 0.9 # 90%. Fill | ||||
|         maximum of 90% of the size of the full width of text-box. | ||||
|       - fill_height: Decimal representing a percentage e.g. 0.9 # 90%. Fill a | ||||
|       - fill_height: Decimal representing a percentage e.g. 0.9 # 90%. Fill | ||||
|         maximum of 90% of the size of the full height of the text-box. | ||||
|     """ | ||||
|     allowed_kwargs = ["alignment", "autofit", "colour", "rotation", "fill_width", "fill_height"] | ||||
| @@ -145,7 +145,7 @@ def write(image, xy, box_size, text, font=None, **kwargs): | ||||
|     # Validate kwargs | ||||
|     for key, value in kwargs.items(): | ||||
|         if key not in allowed_kwargs: | ||||
|             print("{0} does not exist".format(key)) | ||||
|             print(f'{key} does not exist') | ||||
|  | ||||
|     # Set kwargs if given, it not, use defaults | ||||
|     alignment = kwargs["alignment"] if "alignment" in kwargs else "center" | ||||
| @@ -180,7 +180,7 @@ def write(image, xy, box_size, text, font=None, **kwargs): | ||||
|     text_bbox_height = font.getbbox("hg") | ||||
|     text_height = text_bbox_height[3] - text_bbox_height[1] | ||||
|  | ||||
|     # Truncate text if text is too long so it can fit inside the box | ||||
|     # Truncate text if text is too long, so it can fit inside the box | ||||
|     if (text_width, text_height) > (box_width, box_height): | ||||
|         logs.debug(("truncating {}".format(text))) | ||||
|         while (text_width, text_height) > (box_width, box_height): | ||||
| @@ -199,17 +199,16 @@ def write(image, xy, box_size, text, font=None, **kwargs): | ||||
|     elif alignment == "right": | ||||
|         x = int(box_width - text_width) | ||||
|  | ||||
|     y = int((box_height / 2) - (text_height / 2)) | ||||
|  | ||||
|     # Draw the text in the text-box | ||||
|     space = Image.new("RGBA", (box_width, box_height)) | ||||
|     ImageDraw.Draw(space).text((x, y), text, fill=colour, font=font) | ||||
|     draw = ImageDraw.Draw(image) | ||||
|     space = Image.new('RGBA', (box_width, box_height)) | ||||
|     ImageDraw.Draw(space).text((x, 0), text, fill=colour, font=font) | ||||
|  | ||||
|     # Uncomment following two lines, comment out above two lines to show | ||||
|     # red text-box with white text (debugging purposes) | ||||
|  | ||||
|     # space = Image.new('RGBA', (box_width, box_height), color= 'red') | ||||
|     # ImageDraw.Draw(space).text((x, y), text, fill='white', font=font) | ||||
|     # ImageDraw.Draw(space).text((x, 0), text, fill='white', font=font, anchor="la") | ||||
|  | ||||
|     if rotation: | ||||
|         space.rotate(rotation, expand=True) | ||||
|   | ||||
							
								
								
									
										12
									
								
								tests/test_functions.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tests/test_functions.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| """ | ||||
| Test the functions in the functions module. | ||||
| """ | ||||
| from PIL import Image, ImageFont | ||||
| from inkycal.custom import write, fonts | ||||
|  | ||||
|  | ||||
| def test_write(): | ||||
|     im = Image.new("RGB", (500, 200), "white") | ||||
|     font = ImageFont.truetype(fonts['NotoSans-SemiCondensed'], size = 40) | ||||
|     write(im, (125,75), (250, 50), "Hello World", font) | ||||
|     # im.show() | ||||
| @@ -14,7 +14,7 @@ from tests import Config | ||||
| preview = Inkyimage.preview | ||||
| merge = Inkyimage.merge | ||||
|  | ||||
| url ="https://raw.githubusercontent.com/aceinnolab/Inkycal/assets/tests/mark-harpur-unsplash.jpg" | ||||
| url ="https://github.com/aceinnolab/Inkycal/blob/assets/tests/Inkycal_cover.png" | ||||
|  | ||||
| im = Image.open(requests.get(url, stream=True).raw) | ||||
| im.save("test.png", "PNG") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user