Processing Images
Warning
After version 0.3.1 the output has been updated to logging
, which by default only outputs Warning and above. If you want to see the processing, set the logging
level to INFO:
import logging
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.WARNING)
logging.basicConfig(level=logging.INFO)
For demonstration purposes, the following code examples all set the logging
level to INFO.
Client.pic2file
Process one or more image files into the specified output format.
Parameters
Parameter Name | Type | Required | Default Value | Description |
---|---|---|---|---|
image_file | str or list | Yes | - | Single image file path or list of image file paths |
output_path | str | No | "./Output" | Output folder path |
output_names | list | No | None | Custom output file name list, length must match image_file . If the file name contains a folder path, the system will automatically create the corresponding folder structure |
output_format | str | No | "md_dollar" | Output format, options: "texts" , "md" , "md_dollar" , "latex" |
img_correction | bool | No | True | Whether to perform image correction |
equation | bool | No | False | Whether to use pure equation output mode |
convert | bool | No | False | Whether to convert [ to $ , and [[ to $$ (effective only when output_format is "texts" ) |
Return Values
Returns a tuple (success_list, fail_list, has_failed)
containing three elements, in the same order as the input files:
success_list
(list): List of successfully processed files- Elements are the paths of processed files (strings)
- Empty string if processing fails
fail_list
(list): List of files that failed to process- Elements are dictionaries containing two keys:
'error'
: Error message (string)'path'
: Path of the failed file (string)
- Values for both keys are empty strings if processing is successful
- Elements are dictionaries containing two keys:
has_failed
(bool): Processing statusTrue
: At least one file failed to processFalse
: All files processed successfully
Notes
- The lengths of
success_list
andfail_list
are the same - When the output format is
"texts"
, text is returned directly and not saved to a file
Example
Tips
In the following example, 'sample_bad.png' is a corrupted image, so it is normal for processing to fail.
Warning
Make sure you have configured the key in environment variables as per the Initialization section.
Processing multiple images with rpm limit
from pdfdeal import Doc2X
client = Doc2X()
file_list = ["tests/image/sample_bad.png", "tests/image/sample.png"]
success, failed, flag = client.pic2file(
image_file=file_list,
output_path="./Output/test/multiple/pdf2file",
output_names=["sample1.docx", "sample2.docx"],
output_format="docx",
)
print(success)
print(failed)
print(flag)
When the first file fails and the second file succeeds, here is an example output with dark sections showing the values of printed variables: 'success', 'failed', 'flag':
Get exception Upload file error! 400:{"code":"invalid request","msg":"img locked"}. Retrying in 1 seconds.
Waiting for processing: 0% -- uuid: 8c438409-b409-444b-b6df-b89a00d77445
Get exception Upload file error! 400:{"code":"invalid request","msg":"img locked"}. Retrying in 2 seconds.
Success: 100% -- uuid: 8c438409-b409-444b-b6df-b89a00d77445
Get exception Upload file error! 400:{"code":"invalid request","msg":"img locked"}. Retrying in 4 seconds.
IMG Progress: 1/2 files successfully processed.
-----
Failed deal with tests/image/sample_bad.png with error:
Error Upload file error! 400:{"code":"invalid request","msg":"img locked"}
-----
['', './Output/test/multiple/pdf2file/sample2.docx']
[{'error': 'Error Upload file error! 400:{"code":"invalid request","msg":"img locked"}', 'path': 'tests/image/sample_bad.png'}, {'error': '', 'path': ''}]
True
Converting all images in a folder to docx files while maintaining original folder structure
Before processing, the folder structure is as follows:
image
├── sample_bad.png
├── sample.png
└── test
└── sample1.png
Note that 'sample_bad.pdf' is a corrupted file used for testing error handling; it is normal for processing to fail.
In order to maintain the original file structure, use the built-in Directory Generation Tool to generate the paths of the images to be processed:
Warning
Note that the out
parameter of get_files
must match the output_format
in the conversion function on this page!
from pdfdeal import Doc2X
from pdfdeal import get_files
Client = Doc2X()
files, rename = get_files(path="tests/image", mode="img", out="docx")
success, failed, flag = Client.pic2file(
image_file=files, output_names=rename, output_format="docx"
)
print(success)
print(failed)
print(flag)
Example output with dark sections showing printed values of 'success', 'failed', 'flag':
Get exception Upload file error! 400:{"code":"bad_request","msg":"参数错误或无效请求","detail":"img locked"}. Retrying in 1 seconds.
Waiting for processing: 0% -- uuid: b11dc645-d68b-49c3-b222-5c8f6b041332
Waiting for processing: 0% -- uuid: 58630842-3a2c-46ad-a0e5-c15386183d2a
Get exception Upload file error! 400:{"code":"bad_request","msg":"参数错误或无效请求","detail":"img locked"}. Retrying in 2 seconds.
Success: 100% -- uuid: b11dc645-d68b-49c3-b222-5c8f6b041332
Success: 100% -- uuid: 58630842-3a2c-46ad-a0e5-c15386183d2a
Get exception Upload file error! 400:{"code":"bad_request","msg":"参数错误或无效请求","detail":"img locked"}. Retrying in 4 seconds.
IMG Progress: 2/3 files successfully processed.
-----
Failed deal with tests/image/sample_bad.png with error:
Error Upload file error! 400:{"code":"bad_request","msg":"参数错误或无效请求","detail":"img locked"}
-----
['./Output/sample.docx', '', './Output/test/sample1.docx']
[{'error': '', 'path': ''}, {'error': 'Error Upload file error! 400:{"code":"bad_request","msg":"参数错误或无效请求","detail":"img locked"}', 'path': 'tests/image/sample_bad.png'}, {'error': '', 'path': ''}]
True
After processing, the folder structure is as follows:
Output
├── sample.docx
└── test
└── sample1.docx
Processing a single image in pure equation mode to get content formatted as $equation$
from pdfdeal import Doc2X
client = Doc2X()
text, _, _ = client.pic2file(
"tests/image/sample.png", output_format="texts", equation=True, convert=True
)
print(text[0][0])
Example output with dark section showing value printed by 'print(text[0][0])':
Waiting for processing: 0% -- uuid: e631048a-be65-4e0d-b22e-047aebd9baa1
IMG Progress: 1/1 files successfully processed.
$$\text{R}$$