Skip to content

Instantly share code, notes, and snippets.

@calini
Created May 9, 2019 09:38
Show Gist options
  • Save calini/515f7d7f7345f2ab36d5ddadd9cd4d04 to your computer and use it in GitHub Desktop.
Save calini/515f7d7f7345f2ab36d5ddadd9cd4d04 to your computer and use it in GitHub Desktop.
Split Wallpapers
#!/usr/bin/env python
from PIL import Image
import argparse
from pathlib import Path
def crop(image_path, coords, saved_location):
"""
@param image_path: The path to the image to edit
@param coords: A tuple of x/y coordinates (x1, y1, x2, y2)
@param saved_location: Path to save the cropped image
"""
image_obj = Image.open(image_path)
cropped_image = image_obj.crop(coords)
cropped_image.save(saved_location)
cropped_image.show()
def crop_in_half(image_path):
filename = Path(image_path)
image_obj = Image.open(image_path)
x, y = image_obj.size
image_left = image_obj.crop((0, 0, x/2, y))
image_right = image_obj.crop((x/2+1, 0, x, y))
image_left.save(filename.parent / (str(filename.stem) + "_left" + str(filename.suffix)))
image_right.save(filename.parent / (str(filename.stem) + "_right" + str(filename.suffix)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("file", help="image you want to split", type=str)
args = parser.parse_args()
crop_in_half(args.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment