trading_agent.attachments.image_preparer
¶
Checks uploaded pictures, and prepares them for the models.
A phone photo is often 4000 pixels wide and several megabytes, while the models see detail only up to about 1568 pixels on the long side and charge context for every pixel up to that. Before a picture goes to a model it is turned upright by its EXIF orientation, shrunk to at most 1568 pixels on its long side, and saved as a JPEG, or as a PNG when it has transparency. The uploaded file itself is kept unchanged.
Typical usage example:
preparer = ImagePreparer() media_type = preparer.check(path) image = preparer.prepare(path)
ImageError
¶
Bases: ValueError
A file is not a picture that can be used.
ImagePreparer
¶
Checks pictures and resizes them for the models.
check
¶
Checks that a file is a PNG, JPEG, WebP or GIF picture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Its media type, from the file's contents rather than its name. |
Raises:
| Type | Description |
|---|---|
ImageError
|
The file is not one of those pictures, or is damaged. |
Source code in src/trading_agent/attachments/image_preparer.py
prepare
¶
Makes the copy of a picture that a model is sent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The picture. |
required |
Returns:
| Type | Description |
|---|---|
MessageImage | None
|
The upright, resized picture, or None when the file is gone or cannot be read. |