Image Validation Utility
Image validation and conversion utility.
ImgValidation Class
app.utils.img.ImgValidation
Image validation and conversion utility.
Validates image files, converts them to PNG format, and generates MD5 hash for identification. Supports multiple image formats including HEIC.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str | None
|
Path to the normalized PNG image file. |
name |
str | None
|
Image filename without extension. |
ext |
str | None
|
File extension (always .png after normalization). |
hash |
str | None
|
MD5 hash of the image content. |
Source code in backend/app/utils/img.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
Attributes
is_valid
property
Check if the image validation was successful.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all attributes (path, name, ext, hash) are set, False otherwise. |
Functions
__init__
Initialize ImgValidation and normalize the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the image file to validate. |
required |
delete
|
bool
|
Whether to delete the original file after conversion. Default: False. |
False
|
Source code in backend/app/utils/img.py
compare_img
Compare this image with another image file by hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to image file to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if images have the same MD5 hash, False otherwise. |
Source code in backend/app/utils/img.py
convert_any_to_png
staticmethod
Convert image file to PNG format.
Converts supported image formats to PNG and saves in configured images folder. Supports: PNG, JPG, JPEG, BMP, TIFF, GIF, HEIC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Original image file path. |
required |
name
|
str
|
Base filename without extension. |
required |
ext
|
str
|
Original file extension. |
required |
delete
|
bool
|
Whether to delete original file after conversion. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Path to converted PNG file, or None if conversion fails. |
Source code in backend/app/utils/img.py
hash_img
staticmethod
Generate MD5 hash of PNG image content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to PNG image file. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: MD5 hash hexdigest if valid PNG, None otherwise. |
Source code in backend/app/utils/img.py
normalize_img
Normalize image: convert to PNG, validate, and generate hash.
Processes image file through conversion, validation, and hashing pipeline. Sets instance attributes (path, name, ext, hash) on success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to image file to normalize. |
required |
delete
|
bool
|
Whether to delete original file after conversion. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if normalization successful, False otherwise. |
Source code in backend/app/utils/img.py
validate_png
staticmethod
Validate that a file exists and has PNG extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if file exists and has .png extension, False otherwise. |