-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added script for splitting images up into tiles
- Loading branch information
Springvalley
committed
Feb 15, 2024
1 parent
b94eb44
commit 5d7df63
Showing
17 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |