def _create_icons(item, pbar, force=False, color_variants=True): if settings.BOUNDLESS_ICONS_MAPPING.get(item.game_id): image_dir = os.path.join( settings.BOUNDLESS_ICONS_LOCATION, settings.BOUNDLESS_ICONS_MAPPING.get(item.game_id, ""), ) else: image_dir = os.path.join(settings.BOUNDLESS_ICONS_LOCATION, item.name) if not os.path.isdir(image_dir): image_dir = os.path.join(settings.BOUNDLESS_ICONS_LOCATION, item.string_id) if os.path.isdir(image_dir): default_image = _get_default_image(image_dir, item) if os.path.isfile(default_image) and (force or item.image is None or not item.image.name): if item.image is not None and item.image.name: item.image.delete() item.image = _crop_image(default_image, f"{item.game_id}") if item.image is None: click.echo(f"Invalid image file: {default_image}") return if item.image_small is not None and item.image_small.name: item.image_small.delete() item.image_small = make_thumbnail(item.image) if color_variants and os.path.isfile(os.path.join(image_dir, "1_0.png")): if os.path.isfile(os.path.join(image_dir, "10_0.png")): _create_color_icons(force, item, image_dir, pbar) else: _create_metal_icons(force, item, image_dir, pbar)
def _create_metal_icons(force, item, image_dir, pbar): for icv in ItemColorVariant.objects.filter(item=item): icv.image.delete() if icv.image_small is not None and icv.image_small.name: icv.image_small.delete() if force: existing_colors = [] variants = ItemMetalVariant.objects.filter(item=item) for v in variants: v.image.delete() if v.image_small is not None and v.image_small.name: v.image_small.delete() variants.delete() else: existing_colors = [ i.metal.game_id for i in ItemMetalVariant.objects.filter(item=item) ] for metal in Metal.objects.exclude(game_id__in=existing_colors): pbar.label = f"{item.game_id}:{metal.game_id}" pbar.render_progress() image_path = os.path.join(image_dir, f"{metal.game_id}_0.png") if os.path.isfile(image_path): image = _crop_image(image_path, f"{item.game_id}_{metal.game_id}") ItemMetalVariant.objects.create( item=item, metal=metal, image=image, image_small=make_thumbnail(image), )
def _thumbs(): click.echo("Adding thumbs/renmaing images...") duplicates = [] worlds = World.objects.filter(image__isnull=False) with click.progressbar(worlds.iterator(), show_percent=True, show_pos=True, length=worlds.count()) as pbar: for world in pbar: if world.image is not None and world.image.name: expected_name = f"{world.id}.png" if world.image.name != expected_name: try: temp_file = download_image(world.image.url) except (AzureMissingResourceHttpError, HTTPError): world.image = None world.save() continue else: world.image.delete() world.image = get_django_image_from_file( temp_file.name, expected_name) world.save() world.refresh_from_db() if world.image.name != expected_name: duplicates.append(world.image.name) continue if world.image_small is None or not world.image_small.name: try: world.image_small = make_thumbnail(world.image) except AzureMissingResourceHttpError: world.image = None expected_thumb_name = f"{world.id}_small.png" if world.image_small.name != expected_thumb_name: try: temp_file = download_image(world.image_small.url) except (AzureMissingResourceHttpError, HTTPError): world.image_small = None world.save() continue else: world.image_small.delete() world.image_small = get_django_image_from_file( temp_file.name, expected_thumb_name) world.save() world.refresh_from_db() if world.image_small.name != expected_thumb_name: duplicates.append(world.image_small.name) continue click.echo("-----duplicates") click.echo(duplicates)
def _missing(): click.echo("Fixing abandoned images...") not_found = [] multiple = [] for (dirpath, _, filenames) in os.walk(settings.BOUNDLESS_WORLDS_LOCATIONS): with click.progressbar(filenames, show_percent=True, show_pos=True) as pbar: for filename in pbar: world_name = filename.split(".")[0] if world_name.endswith("_small"): os.remove(os.path.join(dirpath, filename)) continue world_name = world_name.replace("_", " ").title() worlds = World.objects.filter( display_name__icontains=world_name) if worlds.count() == 0: # pylint: disable=no-else-continue not_found.append(filename) continue elif worlds.count() > 1: multiple.append(filename) continue world = worlds.get() image_path = os.path.join(dirpath, filename) if world.image is not None and world.image.name: os.remove(image_path) else: world.image = get_django_image_from_file( image_path, f"{world.id}.png") if world.image_small is not None and world.image_small.name: world.image_small.delete() world.image_small = make_thumbnail(world.image) world.save() os.remove(image_path) click.echo("-----not_found") click.echo(not_found) click.echo("-----multiple") click.echo(multiple)
def command(world_id, image_url): world = World.objects.get(pk=world_id) response = requests.get(image_url) response.raise_for_status() image = ContentFile(response.content) image.name = f"{world.id}.png" if world.image is not None and world.image.name: world.image.delete() if world.image_small is not None and world.image_small.name: world.image_small.delete() world.image = image world.image_small = make_thumbnail(image) world.save()
def _process_image(world, root, name): atlas_image_file = os.path.join(root, name) with open(atlas_image_file, "rb") as image_file: atlas_image = ContentFile(image_file.read()) atlas_image.name = f"{world.id}.png" image = _draw_world_image(atlas_image_file, world.id, world.atmosphere_color_tuple) if world.atlas_image is not None and world.atlas_image.name: world.atlas_image.delete() if world.image is not None and world.image.name: world.image.delete() if world.image_small is not None and world.image_small.name: world.image_small.delete() world.atlas_image = atlas_image world.image = image world.image_small = make_thumbnail(image) world.save()
def run(**kwargs): # pylint: disable=too-many-locals emoji_list = _get_emoji_list() emoji_nametable = GameFile.objects.get(folder="assets/gui/emoji", filename="emoji.json").content emoji_created = 0 click.echo("Importing emojis...") with click.progressbar(range(len(emoji_layer_data) // 2), show_pos=True, show_percent=True) as pbar: for index in pbar: name, layers = ( emoji_layer_data[2 * index], emoji_layer_data[(2 * index) + 1], ) image = get_emoji(name, layers) emoji_image = get_django_image(image, f"{name}.png") emoji, created = Emoji.objects.get_or_create( name=name, defaults={"image": emoji_image}, ) if not created: if emoji.image is not None and emoji.image.name: emoji.image.delete() emoji.image = emoji_image if emoji.image_small is not None and emoji.image_small.name: emoji.image_small.delete() emoji.image_small = make_thumbnail(emoji_image) alt_names = emoji_nametable.get(name) try: int(emoji.name, 16) except ValueError: emoji.category = "BOUNDLESS" else: lookup = emoji.name.upper() for emoji_dict in emoji_list: if lookup in emoji_dict["codePoint"].split(" "): emoji.category = GROUP_TO_CATEGORY[emoji_dict["group"]] if emoji.category is None: emoji.category = Emoji.EmojiCategory.UNCATEGORIZED emoji.active = alt_names is not None emoji.save() if alt_names is not None: for alt_name in alt_names: EmojiAltName.objects.get_or_create(emoji=emoji, name=alt_name) if created: emoji_created += 1 print_result("emojis", emoji_created) click.echo("Purging CDN cache...") purge_static_cache(["emoji"])