diff --git a/.image-extraction/ROI_0.png b/.image-extraction/ROI_0.png deleted file mode 100644 index 3758878..0000000 Binary files a/.image-extraction/ROI_0.png and /dev/null differ diff --git a/.image-extraction/extract_character.py b/.image-extraction/extract_character.py new file mode 100644 index 0000000..e0ab480 --- /dev/null +++ b/.image-extraction/extract_character.py @@ -0,0 +1,82 @@ +import glob + +import numpy +from PIL import Image, ImageDraw + + +def extract_image(filename: str, output_path: str) -> None: + """ + Extract a character card from a screenshot from a device. Screenshot for each card + taken from the Shikigami page. 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 = [ + (768, 97), + (768, 512), + (534, 512), + (534, 97), + (559, 79), + (580, 70), + (604, 63), + (631, 58), + (651, 58), + (652, 58), + (671, 58), + (698, 63), + (722, 70), + (745, 79), + ] + + # 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/characters/in/*.*" + ): + print(image) + extract_image( + image, + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/characters/out", + ) diff --git a/.image-extraction/extract_combat.py b/.image-extraction/extract_combat.py new file mode 100644 index 0000000..3758e8f --- /dev/null +++ b/.image-extraction/extract_combat.py @@ -0,0 +1,78 @@ +import glob + +import numpy +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 + 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) + + # Convert to numpy array + initial_image_array = numpy.asarray(initial_image) + + # Create mask polygon using points of the card border + mask_polygon = [ + (788, 90), + (788, 567), + (515, 567), + (515, 130), + (496, 110), + (496, 89), + (513, 76), + (523, 76), + (528, 76), + (536, 81), + (651, 39), + (652, 39), + ] + + # 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/combat/in/*.*" + ): + print(image) + extract_image( + image, + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/combat/out", + ) diff --git a/.image-extraction/extract_form.py b/.image-extraction/extract_form.py new file mode 100644 index 0000000..65c3f84 --- /dev/null +++ b/.image-extraction/extract_form.py @@ -0,0 +1,82 @@ +import glob + +import numpy +from PIL import Image, ImageDraw + + +def extract_image(filename: str, output_path: str) -> None: + """ + Extract a form 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) + + # Convert to numpy array + initial_image_array = numpy.asarray(initial_image) + + # Create mask polygon using points of the card border + mask_polygon = [ + (788, 85), + (788, 567), + (515, 567), + (515, 130), + (496, 110), + (496, 89), + (513, 76), + (523, 76), + (528, 76), + (539, 66), + (581, 49), + (624, 40), + (679, 40), + (722, 49), + (764, 66), + (775, 74), + ] + + # 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/forms/in/*.*" + ): + print(image) + extract_image( + image, + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/forms/out", + ) diff --git a/.image-extraction/extract_spell.py b/.image-extraction/extract_spell.py new file mode 100644 index 0000000..b3a76a6 --- /dev/null +++ b/.image-extraction/extract_spell.py @@ -0,0 +1,76 @@ +import glob + +import numpy +from PIL import Image, ImageDraw + + +def extract_image(filename: str, output_path: str) -> None: + """ + Extract a spell card from a screenshot from a device. Mask is valid for 1304x603 + screenshot from an iPhone 12 Pro. + + Args: + filename (str): Full path to the input image. + output_path (str): Full path 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) + + # Convert to numpy array + initial_image_array = numpy.asarray(initial_image) + + # Create mask polygon using points of the card border (iPhone 12 Pro screenshot) + mask_polygon = [ + (535, 60), + (768, 60), + (788, 85), + (788, 567), + (515, 567), + (515, 130), + (496, 110), + (496, 89), + (513, 7), + (523, 7), + ] + + # 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/spells/in/*.jpeg" + ): + print(image) + extract_image( + image, + "/Users/dtomlinson/git-repos/web-dev/onmyoji-deck-builder/." + "image-extraction/images/spells/out", + ) diff --git a/.image-extraction/img.jpeg b/.image-extraction/img.jpeg deleted file mode 100644 index fa63228..0000000 Binary files a/.image-extraction/img.jpeg and /dev/null differ diff --git a/.image-extraction/main.py b/.image-extraction/main.py deleted file mode 100644 index af7997b..0000000 --- a/.image-extraction/main.py +++ /dev/null @@ -1,27 +0,0 @@ -import cv2 -import numpy as np - -image = cv2.imread('./img.jpeg') -original = image.copy() -gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) -blurred = cv2.GaussianBlur(gray, (3, 3), 0) -canny = cv2.Canny(blurred, 120, 255, 1) -kernel = np.ones((5,5),np.uint8) -dilate = cv2.dilate(canny, kernel, iterations=1) - -# Find contours -cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) -cnts = cnts[0] if len(cnts) == 2 else cnts[1] - -# Iterate thorugh contours and filter for ROI -image_number = 0 -for c in cnts: - x,y,w,h = cv2.boundingRect(c) - cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2) - ROI = original[y:y+h, x:x+w] - cv2.imwrite("ROI_{}.png".format(image_number), ROI) - image_number += 1 - -cv2.imshow('canny', canny) -cv2.imshow('image', image) -cv2.waitKey(0)