def which_type(self, path): # type: (AnyStr) -> Optional[int] """ Analyzes the image provided and attempts to determine whether it is a poster, banner or fanart. :param path: full path to the image :type path: AnyStr :return: BANNER, POSTER, FANART or None if image type is not detected or doesn't exist :rtype: int """ if not ek.ek(os.path.isfile, path): logger.log(u'File does not exist to determine image type of %s' % path, logger.WARNING) return None # use hachoir to parse the image for us try: img_parser = createParser(path) img_parser.parse_exif = False img_parser.parse_photoshop_content = False img_parser.parse_comments = False img_metadata = extractMetadata(img_parser) except (BaseException, Exception) as e: logger.log('Unable to extract metadata from %s, not using existing image. Error: %s' % (path, ex(e)), logger.DEBUG) return None if not img_metadata: logger.log(u'Unable to extract metadata from %s, not using existing image' % path, logger.DEBUG) return None img_ratio = float(img_metadata.get('width')) / float(img_metadata.get('height')) # noinspection PyProtectedMember img_parser.stream._input.close() msg_success = u'Treating image as %s' \ + u' with extracted aspect ratio from %s' % path.replace('%', '%%') # most posters are around 0.68 width/height ratio (eg. 680/1000) if 0.55 < img_ratio < 0.8: logger.log(msg_success % 'poster', logger.DEBUG) return self.POSTER # most banners are around 5.4 width/height ratio (eg. 758/140) elif 5 < img_ratio < 6: logger.log(msg_success % 'banner', logger.DEBUG) return self.BANNER # most fanart are around 1.7 width/height ratio (eg. 1280/720 or 1920/1080) elif 1.7 < img_ratio < 1.8: if 500 < img_metadata.get('width'): logger.log(msg_success % 'fanart', logger.DEBUG) return self.FANART logger.log(u'Image found with fanart aspect ratio but less than 500 pixels wide, skipped', logger.WARNING) return None logger.log(u'Image not useful with size ratio %s, skipping' % img_ratio, logger.WARNING)
def which_type(self, path): """ Analyzes the image provided and attempts to determine whether it is a poster, banner or fanart. returns: BANNER, POSTER, FANART or None if image type is not detected or doesn't exist path: full path to the image """ if not ek.ek(os.path.isfile, path): logger.log( u'File does not exist to determine image type of %s' % path, logger.WARNING) return None # use hachoir to parse the image for us img_parser = createParser(path) img_metadata = extractMetadata(img_parser) if not img_metadata: logger.log( u'Unable to extract metadata from %s, not using existing image' % path, logger.DEBUG) return None img_ratio = float(img_metadata.get('width')) / float( img_metadata.get('height')) img_parser.stream._input.close() msg_success = u'Treating image as %s'\ + u' with extracted aspect ratio from %s' % path.replace('%', '%%') # most posters are around 0.68 width/height ratio (eg. 680/1000) if 0.55 < img_ratio < 0.8: logger.log(msg_success % 'poster', logger.DEBUG) return self.POSTER # most banners are around 5.4 width/height ratio (eg. 758/140) elif 5 < img_ratio < 6: logger.log(msg_success % 'banner', logger.DEBUG) return self.BANNER # most fanart are around 1.7 width/height ratio (eg. 1280/720 or 1920/1080) elif 1.7 < img_ratio < 1.8: if 500 < img_metadata.get('width'): logger.log(msg_success % 'fanart', logger.DEBUG) return self.FANART logger.log( u'Image found with fanart aspect ratio but less than 500 pixels wide, skipped', logger.WARNING) return None else: logger.log( u'Image not useful with size ratio %s, skipping' % img_ratio, logger.WARNING) return None
def which_type(self, path): """ Analyzes the image provided and attempts to determine whether it is a poster, banner or fanart. returns: BANNER, POSTER, FANART or None if image type is not detected or doesn't exist path: full path to the image """ if not ek.ek(os.path.isfile, path): logger.log(u'File does not exist to determine image type of %s' % path, logger.WARNING) return None # use hachoir to parse the image for us img_parser = createParser(path) img_metadata = extractMetadata(img_parser) if not img_metadata: logger.log(u'Unable to extract metadata from %s, not using existing image' % path, logger.DEBUG) return None img_ratio = float(img_metadata.get('width')) / float(img_metadata.get('height')) img_parser.stream._input.close() msg_success = u'Treating image as %s'\ + u' with extracted aspect ratio from %s' % path.replace('%', '%%') # most posters are around 0.68 width/height ratio (eg. 680/1000) if 0.55 < img_ratio < 0.8: logger.log(msg_success % 'poster', logger.DEBUG) return self.POSTER # most banners are around 5.4 width/height ratio (eg. 758/140) elif 5 < img_ratio < 6: logger.log(msg_success % 'banner', logger.DEBUG) return self.BANNER # most fanart are around 1.7 width/height ratio (eg. 1280/720 or 1920/1080) elif 1.7 < img_ratio < 1.8: if 500 < img_metadata.get('width'): logger.log(msg_success % 'fanart', logger.DEBUG) return self.FANART logger.log(u'Image found with fanart aspect ratio but less than 500 pixels wide, skipped', logger.WARNING) return None else: logger.log(u'Image not useful with size ratio %s, skipping' % img_ratio, logger.WARNING) return None