def optimize_thumbnail(thumbnail): '''Optimize thumbnail images by removing unnecessary data''' try: optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[ determinetype(thumbnail.path)] if not optimize_command: return except (TypeError, KeyError, NotImplementedError): return storage = thumbnail.storage try: with NamedTemporaryFile() as temp_file: thumbnail.seek(0) temp_file.write(thumbnail.read()) temp_file.flush() optimize_command = optimize_command.format(filename=temp_file.name) output = check_output( optimize_command, stderr=subprocess.STDOUT, shell=True) if output: logger.warning( '{0} returned {1}'.format(optimize_command, output)) else: logger.info('{0} returned nothing'.format(optimize_command)) with open(temp_file.name, 'rb') as f: thumbnail.file = ContentFile(f.read()) storage.delete(thumbnail.path) storage.save(thumbnail.path, thumbnail) except Exception as e: logger.error(e)
def optimize_thumbnail(thumbnail): '''Optimize thumbnail images by removing unnecessary data''' try: optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[ determinetype(thumbnail.path)] if not optimize_command: return except (TypeError, KeyError, NotImplementedError): return storage = thumbnail.storage try: with NamedTemporaryFile() as temp_file: thumbnail.seek(0) temp_file.write(thumbnail.read()) temp_file.flush() optimize_command = optimize_command.format(filename=temp_file.name) output = check_output( optimize_command, stderr=subprocess.STDOUT, shell=True) if output: logger.warn( '{0} returned {1}'.format(optimize_command, output)) else: logger.info('{0} returned nothing'.format(optimize_command)) with open(temp_file.name, 'rb') as f: thumbnail.file = ContentFile(f.read()) storage.delete(thumbnail.path) storage.save(thumbnail.path, thumbnail) except Exception as e: logger.error(e)
def squeeze(path, backup=settings.DIET_IMAGE_BACKUPS, flag_processed_file=settings.DIET_FLAG_PROCESSED_FILE, new_only=settings.DIET_NEW_FILES_ONLY): '''Returns path of optimized image or None if something went wrong.''' if not os.path.exists(path): logger.error("'%s' does not point to a file." % path) return None filetype = determinetype(path) if backup: backup_copy(path) squeeze_cmd = "" if filetype == "jpeg": squeeze_cmd = squeeze_jpeg() elif filetype == "gif": squeeze_cmd = squeeze_gif() elif filetype == "png": squeeze_cmd = squeeze_png() if new_only: should_process_file = False flag_path = get_flag_path(path) flag_path = get_flag_path(path) if not os.path.exists(flag_path): should_process_file = True else: file_mt = os.path.getmtime(path) flag_mt = os.path.getmtime(flag_path) if flag_mt < file_mt: should_process_file = True if not should_process_file: squeeze_cmd = False if squeeze_cmd: try: retcode = call(squeeze_cmd % {'file': path}, shell=True, stdout=PIPE) if flag_processed_file: create_processed_file_flag(path) except: raise logger.error('Squeezing failed with parameters:') logger.error(squeeze_cmd[filetype] % {'file': path}) logger.exception() return None if retcode != 0: # Failed. logger.error( ('Squeezing failed. ' 'Likely because you are missing one of required utilities.')) return None return path
def _determinetype(self): """ Determine the filetype of the file using imghdr. """ filetype = determinetype(self.fullpath) if filetype in ["jpeg", "png"]: self.filetype = filetype else: self.filetype = None return self.filetype
def __init__(self, fullpath): """ gather image information. """ self.valid = False self.reset() self.fullpath = fullpath if path.isfile(self.fullpath): self.filetype = determinetype(self.fullpath) if self.filetype in ["jpeg", "png"]: oldfile = QFileInfo(self.fullpath) self.shortname = oldfile.fileName() self.oldfilesize = oldfile.size() self.icon = QIcon(self.fullpath) self.valid = True
def __init__(self, fullpath): """ gather image information. """ self.valid = False self.reset() self.fullpath = fullpath if path.isfile(self.fullpath) and access(self.fullpath, WRITEABLE): self.filetype = determinetype(self.fullpath) if self.filetype in ["jpeg", "png"]: oldfile = QFileInfo(self.fullpath) self.shortname = oldfile.fileName() self.oldfilesize = oldfile.size() self.icon = QIcon(self.fullpath) self.valid = True
def squeeze(path): '''Returns path of optimized image or None if something went wrong.''' if not isfile(path): logger.error("'%s' does not point to a file." % path) return None if settings.DIET_DEBUG: # Create a copy of original file for debugging purposes call("cp '%(file)s' '%(file)s'.orig" % {'file': path}, shell=True, stdout=PIPE) filetype = determinetype(path) squeeze_cmd = "" if filetype == "jpeg": squeeze_cmd = squeeze_jpeg() elif filetype == "gif": squeeze_cmd = squeeze_gif() elif filetype == "png": squeeze_cmd = squeeze_png() if squeeze_cmd: try: retcode = call(squeeze_cmd % {'file': path}, shell=True, stdout=PIPE) except: logger.error('Squeezing failed with parameters:') logger.error(squeeze_cmd[filetype] % {'file': path}) logger.exception() return None if retcode != 0: # Failed. logger.error( ('Squeezing failed. ' 'Likely because you are missing one of required utilities.')) return None return path