-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for determining completeness of bytes or BytesIO object…
…s rather than just file names; added is_XYZ methods that determine whether a file/bytes/BytesIO represents file type XYZ
- Loading branch information
Showing
18 changed files
with
423 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
from image_complete.bmp import is_bmp_complete | ||
from image_complete.gif import is_gif_complete | ||
from image_complete.jpg import is_jpg_complete | ||
from image_complete.png import is_png_complete | ||
from image_complete.webp import is_webp_complete | ||
from io import BytesIO | ||
|
||
from image_complete.bmp import is_bmp_complete, is_bmp | ||
from image_complete.gif import is_gif_complete, is_gif | ||
from image_complete.jpg import is_jpg_complete, is_jpg | ||
from image_complete.png import is_png_complete, is_png | ||
from image_complete.webp import is_webp_complete, is_webp | ||
|
||
def is_image_complete(img_path): | ||
|
||
def is_image_complete(img): | ||
""" | ||
Checks whether the image is complete. Auto-detects the type based on extension. | ||
If the type is not supported, it will throw an exception. | ||
:param img_path: the absolute path to the JPG image | ||
:type img_path: str | ||
:param img: the absolute path to the image or a bytes/BytesIO object | ||
:type img: str or bytes or BytesIO | ||
:return: True if complete | ||
:rtype: bool | ||
""" | ||
if isinstance(img, bytes): | ||
img = BytesIO(img) | ||
|
||
if isinstance(img, str): | ||
name = img.lower() | ||
if name.endswith(".gif"): | ||
return is_gif_complete(img) | ||
elif name.endswith(".jpg") or name.endswith(".jpeg"): | ||
return is_jpg_complete(img) | ||
elif name.endswith(".png"): | ||
return is_png_complete(img) | ||
elif name.endswith(".bmp"): | ||
return is_bmp_complete(img) | ||
elif name.endswith(".webp"): | ||
return is_webp_complete(img) | ||
else: | ||
raise Exception("Unsupported file type: " + img) | ||
|
||
name = img_path.lower() | ||
if name.endswith(".gif"): | ||
return is_gif_complete(img_path) | ||
elif name.endswith(".jpg") or name.endswith(".jpeg"): | ||
return is_jpg_complete(img_path) | ||
elif name.endswith(".png"): | ||
return is_png_complete(img_path) | ||
elif name.endswith(".bmp"): | ||
return is_bmp_complete(img_path) | ||
elif name.endswith(".webp"): | ||
return is_webp_complete(img_path) | ||
elif isinstance(img, BytesIO): | ||
if is_bmp(img): | ||
return is_bmp_complete(img) | ||
elif is_gif(img): | ||
return is_gif_complete(img) | ||
elif is_jpg(img): | ||
return is_jpg_complete(img) | ||
elif is_png(img): | ||
return is_png_complete(img) | ||
elif is_webp(img): | ||
return is_webp_complete(img) | ||
else: | ||
raise Exception("Failed to determine file type!") | ||
else: | ||
raise Exception("Unsupported file type: " + img_path) | ||
raise Exception("Unsupported data type: %s" % str(type(img))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
|
||
from io import BytesIO | ||
|
||
|
||
def load(img): | ||
""" | ||
Loads the data and returns it as BytesIO object and the associated length. | ||
:param img: the image to load | ||
:type img: str or bytes or BytesIO | ||
:return: tuple of BytesIO wrapper and length | ||
:rtype: tuple | ||
""" | ||
if isinstance(img, bytes): | ||
img = BytesIO(img) | ||
if isinstance(img, BytesIO): | ||
return img, img.getbuffer().nbytes | ||
if isinstance(img, str): | ||
flen = os.path.getsize(img) | ||
with open(img, "rb") as f: | ||
img = BytesIO(f.read()) | ||
return img, flen | ||
else: | ||
print("Unhandled data type: %s" % str(type(img))) | ||
return None, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.