FIX: Standardize image-text converter font default to None#2232
Open
romanlutz wants to merge 1 commit into
Open
FIX: Standardize image-text converter font default to None#2232romanlutz wants to merge 1 commit into
romanlutz wants to merge 1 commit into
Conversation
AddTextImageConverter defaulted font_name to "helvetica.ttf", which does not exist on Linux and triggered a spurious "Cannot open font resource" warning (surfaced in the 3_image_converters notebook) before falling back to Pillow's built-in font. AddImageTextConverter already defaults to None. Default AddTextImageConverter.font_name to None and use Pillow's bundled default directly when None (mirroring AddImageTextConverter via the _font_load_failed pattern), so both converters render identically across platforms. The .ttf validation is now gated on a non-None font_name. Update tests to exercise the None default (and assert no font warning), and re-execute the 3_image_converters notebook so the plain constructors run cleanly with no "Cannot open font resource" warning. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 43b99657-b60e-47dc-a0fb-d8babd5c0860
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
PyRIT's two image-text converters had inconsistent font-name defaults.
AddImageTextConverteralready defaultedfont_nametoNone(using Pillow's bundled default), butAddTextImageConverterdefaulted to"helvetica.ttf", which does not exist on Linux. On Linux that causedImageFont.truetype("helvetica.ttf", ...)to raiseOSErrorand log a spuriousCannot open font resource: helvetica.ttf. Using Pillow built-in default font.warning before falling back. This warning surfaced in the3_image_convertersnotebook during release validation, and the mismatched defaults made the two converters behave differently across platforms.This change makes
AddTextImageConvertermirrorAddImageTextConverter:font_name: str = "helvetica.ttf"tofont_name: str | None = None..ttfextension validation is now gated onfont_name is not None._font_load_failed = font_name is Nonepattern so_load_fontgoes straight toImageFont.load_default(size=...)whenNone(notruetypeattempt, no warning), while keeping the existingOSError->load_defaultfallback for custom fonts.AddImageTextConverter.On the pinned Pillow (
>=12.2.0),ImageFont.load_default(size=...)returns a real scalable TrueType font, so rendering was already fine; this cleans up the warning and makes both converters cross-platform consistent. No matplotlib/DejaVu font lookup was introduced.AddImageTextConverterwas left untouched (already correct).Tests and Documentation
test_add_text_image_converter_initializationto construct with the defaultNone, assert_font_name is Noneand a realFreeTypeFont, and assert (viacaplog) that noCannot open font resourcewarning is emitted. Kept the invalid-.ttfValueErrortest and the nonexistent-custom-font fallback-warning test. Both converter test files pass (31 tests), includingtest_add_image_text_converter_equal_to_add_text_image, which now confirms both converters produce byte-identical output for theNonedefault on all platforms (previously only matched on Linux via fallback).doc/code/converters/3_image_convertersnotebook via JupyText (jupytext --to ipynb --execute). It executes end to end with 0 error outputs and 0 occurrences ofCannot open font resource/helvetica. The notebook source uses the plain converter constructors with no font workaround; the converter fix is what makes the defaults run cleanly.