You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/docs/swarms/utils/find_image_path.md

3.7 KiB

find_image_path

Firstly, we will divide this documentation into multiple sections.

Overview

The module swarms.utils has the main goal of providing necessary utility functions that are crucial during the creation of the swarm intelligence frameworks. These utility functions can include common operations such as handling input-output operations for files, handling text parsing, and handling basic mathematical computations necessary during the creation of swarm intelligence models.

The current function find_image_path in the module is aimed at extracting an image path from a given text document.

Function Detailed Explanation

Definition

The function find_image_path takes a singular argument as an input:

def find_image_path(text):
    # function body

Parameter

The parameter text in the function is a string that represents the document or text from which the function is trying to extract all paths to the images present. The function scans the given text, looking for absolute or relative paths to image files (.png, .jpg, .jpeg) on the disk.

Parameter Name Data Type Default Value Description
text str - The text content to scan for image paths

Return Value

The return value of the function find_image_path is a string that represents the longest existing image path extracted from the input text. If no image paths exist within the text, the function returns None.

Return Value Data Type Description
Path str Longest image path found in the text or None if no path found

Function's Code

The function find_image_path performs text parsing and pattern recognition to find image paths within the provided text. The function uses regular expressions (re) module to detect all potential paths.

def find_image_path(text):
    pattern = r"([A-Za-z]:\\[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))|(/[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))"
    matches = [match.group() for match in re.finditer(pattern, text) if match.group()]
    matches += [match.replace("\\", "") for match in matches if match]
    existing_paths = [match for match in matches if os.path.exists(match)]
    return max(existing_paths, key=len) if existing_paths else None

Usage Examples

Let's consider examples of how the function find_image_path can be used in different scenarios.

Example 1:

Consider the case where a text without any image path is provided.

from swarms.utils import find_image_path

text = "There are no image paths in this text"
print(find_image_path(text))  # Outputs: None

Example 2:

Consider the case where the text has multiple image paths.

from swarms.utils import find_image_path

text = "Here is an image path: /home/user/image1.png. Here is another one: C:\\Users\\User\\Documents\\image2.jpeg"
print(
    find_image_path(text)
)  # Outputs: the longest image path (depends on your file system and existing files)

Example 3:

In the final example, we consider a case where the text has an image path, but the file does not exist.

from swarms.utils import find_image_path

text = "Here is an image path: /home/user/non_existant.png"
print(find_image_path(text))  # Outputs: None

Closing Notes

In conclusion, the find_image_path function is crucial in the swarms.utils module as it supports a key operation of identifying image paths within given input text. This allows users to automate the extraction of such data from larger documents/text. However, it's important to note the function returns only existing paths in your file system and only the longest if multiple exist.