-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageHandler.py
138 lines (114 loc) · 4.41 KB
/
ImageHandler.py
1
"""Macstodon - a Mastodon client for classic Mac OSMIT LicenseCopyright (c) 2022-2024 Scott Small and ContributorsPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associateddocumentation files (the "Software"), to deal in the Software without restriction, including without limitation therights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permitpersons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of theSoftware.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THEWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS ORCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OROTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."""# ############### Python Imports # ##############import Imageimport GifImagePluginimport JpegImagePluginimport PngImagePluginimport osimport stringimport sysimport urllib# #################### Third-Party Imports# ###################from third_party.PixMapWrapper import PixMapWrapper# ########### My Imports# ##########from MacstodonHelpers import dprint, getFilenameFromURL# ############ Application# ###########class ImageHandler: def __init__(self, app): """ Initializes the ImageHandler class. """ self.app = app def getImageFromURL(self, url, imgtype=None): """ Wrapper function - given an image URL, either downloads and caches the image, or loads it from the cache if it already exists there. """ try: dprint("Want to cache image: %s" % imgtype) file_name = getFilenameFromURL(url) if not self.isCached(file_name): dprint("Image is not cached, downloading") self.downloadImage(url, file_name) dprint("Resizing image: %s" % imgtype) img = self.resizeAndGetImage(file_name, imgtype) else: dprint("Image is already cached") img = self.getImageFromCache(file_name) return img except: etype, evalue = sys.exc_info()[:2] dprint("Error loading image: %s: %s" % (etype, evalue)) return None def isCached(self, file_name): """ Checks if an image exists in cache or not and returns true/false. """ cachepath = self.app.cachefolderpath try: os.stat(os.path.join(cachepath, file_name)) return 1 except: exc_info = sys.exc_info() if str(exc_info[0]) == "mac.error" and exc_info[1][0] == 2: return 0 raise def getImageFromCache(self, file_name): """ Loads an image file from the cache. """ pm = PixMapWrapper() file_path = os.path.join(self.app.cachefolderpath, file_name) handle = open(file_path, "rb") pil_image = Image.open(handle, "r") pm.fromImage(pil_image) handle.close() del pil_image return pm def downloadImage(self, url, file_name=None): """ Downloads an image from the given URL and writes it to the cache. """ http_url = string.replace(url, "https://", "http://") file_path = os.path.join(self.app.cachefolderpath, file_name) urllib.urlretrieve(http_url, file_path) def resizeAndGetImage(self, file_name, imgtype): """ Resizes an image to an appropriate size and overwrites the existing cached image. """ file_path = os.path.join(self.app.cachefolderpath, file_name) handle = open(file_path, "rb") pil_image = Image.open(handle, "r") if imgtype == "account": pil_image_small = pil_image.resize((48, 48)) elif imgtype == "banner": pil_image_small = pil_image.resize((384, 128)) del pil_image handle.close() pil_image_small.save(file_path) pm = PixMapWrapper() pm.fromImage(pil_image_small) del pil_image_small return pm