-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
its a bit unstable now but it kinda works the picture is a little small, may need to scale it better
- Loading branch information
1 parent
88367ce
commit 79db34c
Showing
12 changed files
with
361 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from PIL import Image | ||
import base64 | ||
from io import BytesIO | ||
import imghdr | ||
import binascii | ||
|
||
class ImageWrapper: | ||
def __init__(self, img, type): | ||
self.img = img | ||
self.type = type | ||
|
||
def __str__(): | ||
return "ImageWrapper Object of type " + self.type + "size: " + len(self.img) | ||
|
||
|
||
INVALID_IMAGE = 'INVALID_IMAGE' | ||
iconsize = 256 | ||
|
||
def is_image(img): | ||
print(ImageWrapper, Image) | ||
return (isinstance(img, ImageWrapper) and isinstance(img.img, Image.Image)) or isinstance(img, Image.Image) | ||
|
||
|
||
def open_image(data, change_icon=False, b64=False): | ||
"""Returns an image object, given image binary data (gotten from request.get_file) """ | ||
if b64: | ||
try: | ||
with open('out.txt', 'w') as f: | ||
f.write(data[22:]) | ||
data = base64.b64decode(data[22:] if data.startswith("data:image/png;base64,") else data) | ||
except binascii.Error: | ||
print("unable to decode b64!") | ||
raise | ||
return INVALID_IMAGE | ||
|
||
io = BytesIO(data) | ||
|
||
with open("hi.png", 'wb') as f: | ||
import copy | ||
for i in (copy.deepcopy(io)): | ||
f.write(i) | ||
|
||
image_type = imghdr.what(io) | ||
if image_type: | ||
if change_icon: | ||
return ImageWrapper(to_icon(Image.open(io)), image_type) | ||
else: | ||
return ImageWrapper(Image.open(io), image_type) | ||
else: | ||
return INVALID_IMAGE | ||
|
||
|
||
def to_icon(img): | ||
"""Takes an Image object, and mutates it into a thumbname with the size iconsize""" | ||
reducerate = 1 # the ratio to times the img height and width | ||
if max(img.height, img.width) > iconsize: | ||
reducerate = 256 / max(img.height, img.width) | ||
|
||
return img.resize((int(img.width*reducerate), int(img.height*reducerate)), Image.ANTIALIAS) |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.