Skip to content

Commit

Permalink
feat: added script for splitting images up into tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Springvalley committed Feb 15, 2024
1 parent b94eb44 commit 5d7df63
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 1 deletion.
54 changes: 53 additions & 1 deletion WMS/Util.py
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
#Dette scriptet skal ha en funksjon som gir muligheten til å bryte ned bilder
#Import Pillow library for working with images
from PIL import Image


def split_image(image_path, output_folder, tile_size):
'''
Splits an image (tested with .jpeg, .jpg, .tiff formats) into smaller tiles based on a desired size
Args:
image_path (str): The path to the image
output_folder (str): The path to the output folder
tile_size (int): Desired size of the tiles
Returns:
int: Number of tiles created
'''
#Defines the accepted (tested) filetypes that can be split
acceptedFileTypes = ["jpeg", "jpg", "png", "tiff"]
print("done");

#Checks if the file is of the correct type, otherwise raises an error
if(image_path.split(".")[2] not in acceptedFileTypes):
return "Filetype not supported"


#Using the Image class from the Pillow library to open the Image
image = Image.open(image_path)

#Get the size of the image
width, height = image.size

#Find out how many tiles there are in each direction using floor division
horizontal_tiles = width // tile_size
vertical_tiles = height // tile_size

#Loop through the amount of tiles in both directions and split the image up based on the desired tile size
for i in range(horizontal_tiles):
for j in range(vertical_tiles):
#Define the corners of each tile
left_top = i * tile_size
left_bottom = j * tile_size
right_top = (i+1) * tile_size
right_bottom = (j+1) * tile_size

#Crop the image based on the tile corners
tile = image.crop((left_top, left_bottom, right_top, right_bottom))

#Save the tile to the output folder
tile.save(f"{output_folder}/tile_{i}_{j}.jpeg")

#Return the amount of tiles created to be used for testing purposes
return horizontal_tiles + vertical_tiles

Binary file added WMS/__pycache__/test_util.cpython-311.pyc
Binary file not shown.
Binary file added WMS/__pycache__/util.cpython-311.pyc
Binary file not shown.
Binary file added WMS/rawphotos/eksempel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_0_0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_0_1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_0_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_1_0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_1_1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_1_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_2_0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_2_1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_2_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_3_0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_3_1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WMS/readyforemail/tile_3_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions WMS/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
import os
import shutil
from PIL import Image
import util


#Test class for the utilities library
class TestUtil(unittest.TestCase):

#Setup for testing
def setUp(self):
image = Image.new("RGB", (1024, 1024))
image.save("image.png");
os.mkdir("output")

#Teardown after testing
def tearDown(self):
os.remove("image.png")
shutil.rmtree("output")

#Tests the split_image method of util
def test_split_image_functionality(self):
a = util.split_image("image.png", "output", 512)
self.assertEquals(a, 4)

#Tests the split_image method of util
def test_split_image_wrong_file(self):
a = util.split_image("image.txt", "output", 512)
self.assertEquals(a, "Filetype not supported")



if __name__ == "main":
unittest.main()

0 comments on commit 5d7df63

Please sign in to comment.