From a399ca0e2b858976a92408c596103bb2ba8594b8 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 20 Mar 2021 05:28:09 +0000 Subject: [PATCH] adding latest --- .image-extraction/extract_combat.py | 2 +- .image-extraction/extract_field.py | 98 +++++++++++++++++++++++++++++ .image-extraction/extract_spell.py | 6 +- 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 .image-extraction/extract_field.py diff --git a/.image-extraction/extract_combat.py b/.image-extraction/extract_combat.py index 3758e8f..cfeeb25 100644 --- a/.image-extraction/extract_combat.py +++ b/.image-extraction/extract_combat.py @@ -6,7 +6,7 @@ from PIL import Image, ImageDraw def extract_image(filename: str, output_path: str) -> None: """ - Extract a combatcard from a screenshot from a device. Screenshot for each card + Extract a combat card from a screenshot from a device. Screenshot for each card taken from the Hyakabun scrollery. Image will be resized and the card extracted. Args: diff --git a/.image-extraction/extract_field.py b/.image-extraction/extract_field.py new file mode 100644 index 0000000..8e7555f --- /dev/null +++ b/.image-extraction/extract_field.py @@ -0,0 +1,98 @@ +import glob + +import numpy +from PIL import Image, ImageDraw + + +def extract_image(filename: str, output_path: str) -> None: + """ + Extract a field card from a screenshot from a device. Screenshot for each card + taken from the Hyakabun scrollery. Image will be resized and the card extracted. + + Args: + filename (str): Full path to the input image. + output_path (str): Full path to the directory where the image should be saved. + """ + # Open the image, add alpha channel for transparency + initial_image = Image.open(filename).convert("RGBA") + + # Check for size + if initial_image.size[0] != 1304: + basewidth = 1304 + wpercent = basewidth / float(initial_image.size[0]) + hsize = int((float(initial_image.size[1]) * float(wpercent))) + initial_image = initial_image.resize((basewidth, hsize), Image.ANTIALIAS) + # initial_image.save(f"{output_path}/{filename.split('/')[-1]}") + # raise SystemExit + + # Convert to numpy array + initial_image_array = numpy.asarray(initial_image) + + # Create mask polygon using points of the card border + mask_polygon = [ + (788, 68), + (788, 568), + (515, 568), + (515, 130), + (495, 112), + (491, 104), + (491, 96), + (495, 88), + (515, 72), + (515, 68), + (500, 46), + (499, 46), + (499, 43), + (502, 43), + (512, 47), + (523, 49), + (534, 51), + (550, 52), + (567, 52), + (584, 53), + (719, 53), + (736, 52), + (753, 52), + (769, 51), + (774, 50), + (779, 49), + (785, 48), + (790, 47), + (794, 46), + (803, 43), + ] + + # Create the mask + mask_image = Image.new( + "L", (initial_image_array.shape[1], initial_image_array.shape[0]), 0 + ) + ImageDraw.Draw(mask_image).polygon(mask_polygon, outline=1, fill=1) + mask = numpy.array(mask_image) + + # create a new empty image + extracted_image_array = numpy.empty(initial_image_array.shape, dtype="uint8") + + # copy the colours from the first 3 columns + extracted_image_array[:, :, :3] = initial_image_array[:, :, :3] + + # apply transparency to the alpha channel (4th column) + extracted_image_array[:, :, 3] = mask * 255 + + # convert back to an image + extracted_image = Image.fromarray(extracted_image_array, "RGBA") + + # save the image + extracted_image.save(f"{output_path}/{filename.split('/')[-1].split('.')[-2]}.png") + + +if __name__ == "__main__": + for image in glob.glob( + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/fields/in/*.*" + ): + print(image) + extract_image( + image, + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/fields/out", + ) diff --git a/.image-extraction/extract_spell.py b/.image-extraction/extract_spell.py index b3a76a6..05f1227 100644 --- a/.image-extraction/extract_spell.py +++ b/.image-extraction/extract_spell.py @@ -35,9 +35,9 @@ def extract_image(filename: str, output_path: str) -> None: (515, 567), (515, 130), (496, 110), - (496, 89), - (513, 7), - (523, 7), + (496, 91), + (513, 73), + (523, 73), ] # Create the mask