Skip to content

feat: improve Results.save() with pathlib and optional directory creation#23592

Merged
Laughing-q merged 7 commits into
ultralytics:mainfrom
ShuaiLYU:feat/improve-results-save
Feb 11, 2026
Merged

feat: improve Results.save() with pathlib and optional directory creation#23592
Laughing-q merged 7 commits into
ultralytics:mainfrom
ShuaiLYU:feat/improve-results-save

Conversation

@ShuaiLYU

@ShuaiLYU ShuaiLYU commented Feb 9, 2026

Copy link
Copy Markdown
Contributor

…tion

This pull request updates the save method in the Results class to add more flexibility when saving annotated images. The main change is the introduction of a create_dir parameter, allowing users to control whether the parent directory is created automatically when saving results.

Enhancements to the save method:

  • Added a create_dir boolean parameter (defaulting to True) to the save method in ultralytics/engine/results.py, allowing users to specify whether the parent directory should be created if it doesn't exist. [1] [2]
  • Updated the method implementation to create the parent directory only if create_dir is True.
  • Improved the docstring and usage examples to document the new create_dir parameter and demonstrate its usage. [1] [2]

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Enhances Results.save() by adopting pathlib and optionally creating output directories for smoother save workflows 🧰

📊 Key Changes

-Adds a new create_dir: bool = True argument to Results.save()
-Converts filename to an absolute Path before saving
-Automatically creates the parent directory (when enabled) via mkdir(parents=True, exist_ok=True)
-Updates docstring and examples to reflect the new behavior

🎯 Purpose & Impact

-Prevents save failures when targeting non-existent directories by creating them automatically 📁
-Improves path handling consistency and cross-platform robustness using pathlib
-Gives users control to disable directory creation (create_dir=False) for stricter filesystem behavior

@UltralyticsAssistant UltralyticsAssistant added enhancement New feature or request python Pull requests that update python code labels Feb 9, 2026
@UltralyticsAssistant

Copy link
Copy Markdown
Member

👋 Hello @ShuaiLYU, thank you for submitting a ultralytics/ultralytics 🚀 PR! This is an automated message to help streamline review—an Ultralytics engineer will assist you soon 🛠️

-✅ Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions.
-✅ Synchronize with Source: Confirm your PR is synchronized with the ultralytics/ultralytics main branch. If it's behind, update it by clicking the 'Update branch' button or by running git pull and git merge main locally.
-✅ Ensure CI Checks Pass: Verify all Ultralytics Continuous Integration (CI) checks are passing. If any checks fail, please address the issues.
-✅ Update Documentation: Update the relevant documentation for any new or modified features.
-✅ Add Tests: If applicable, include or update tests to cover your changes, and confirm that all tests are passing.
-✅ Sign the CLA: Please ensure you have signed our Contributor License Agreement if this is your first Ultralytics PR by writing "I have read the CLA Document and I sign the CLA" in a new message.
-✅ Minimize Changes: Limit your changes to the minimum necessary for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee

For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀

@UltralyticsAssistant UltralyticsAssistant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 PR Review

Made with ❤️ by Ultralytics Actions

Overall looks good and the new directory-creation behavior is helpful. The only concern is the return type now implicitly becoming Path due to the new conversion; consider keeping the return type consistent with the annotation/docstring.

💬 Posted 1 inline comment

Comment thread ultralytics/engine/results.py Outdated
@codecov

codecov Bot commented Feb 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultralytics/engine/results.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@ShuaiLYU

ShuaiLYU commented Feb 9, 2026

Copy link
Copy Markdown
Contributor Author

@Laughing-q Now, the "save function" will automatically create the parent folder (rather than skipping the image saving process).


def yolov8s_predict():

	
	from ultralytics import YOLO

	model = YOLO("yolov8s.pt")
	res = model.predict("ultralytics/assets/bus.jpg", conf=0.25)[0]
	res.save("./runs/visualization/yolov8s_predict.png")


yolov8s_predict()

@ShuaiLYU ShuaiLYU assigned ShuaiLYU and Laughing-q and unassigned ShuaiLYU Feb 9, 2026
@glenn-jocher

Copy link
Copy Markdown
Member

@ShuaiLYU this looks good, but we can skip the argument and create it always if needed. You can use is_missing_ok=True I think in the mkdir() method, or check if the dir exists first.

@ShuaiLYU

Copy link
Copy Markdown
Contributor Author

@ShuaiLYU this looks good, but we can skip the argument and create it always if needed. You can use is_missing_ok=True I think in the mkdir() method, or check if the dir exists first.

You’re right, there is a 'exist_ok' argument for mkdir()! I’m removing the create_dir argument and using mkdir(exist_ok=True) to auto-handle the directory check (create if missing, skip if present).

@ShuaiLYU ShuaiLYU force-pushed the feat/improve-results-save branch from f2ff569 to d43b49c Compare February 11, 2026 02:14
@Laughing-q Laughing-q changed the title feat: improve Results.save() with pathlib and optional directory creation feat: improve Results.save() with pathlib and optional directory creation Feb 11, 2026
@Laughing-q Laughing-q merged commit 61ac65d into ultralytics:main Feb 11, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants