Skip to content

Instantly share code, notes, and snippets.

@tshrinivasan
Last active August 3, 2020 12:14
Show Gist options
  • Save tshrinivasan/f5065161ecf7cf3a1ea4209db1148497 to your computer and use it in GitHub Desktop.
Save tshrinivasan/f5065161ecf7cf3a1ea4209db1148497 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Author: T Shrinivasan
# Email : [email protected]
# License : GNU GPL 3.0
# sudo apt install libimage-exiftool-perl imagemagick gwenview
# /etc/ImageMagick-6/policy.xml
# <policy domain="resource" name="memory" value="2560MiB"/>
#change from 256 to 2560
# This program gets 3 inputs.
#--input_folder,Give the folder path, where the images to be splitted vertically, are kept. Remove any space, special charectors on the folder name.
#--skip_in_beginning,We can skip splitting for the few images in the beginning. Give the number of images to skip.
#--skip_in_end,We can skip splitting for the few images in the end. Give the number of images to skip
#
#All the images are rotated automatically and added few metadata using exiftool.
#convert from imagemagick is used to split the images vertically
#"Use the imageviewer gwenview to view the images, which shows the images in same direction as the images are as original.
#
import glob
import argparse
import os
import sys
import time
import subprocess
parser = argparse.ArgumentParser(description='Split the images Vertically')
parser.add_argument('--input_folder',
help='Give the folder path, where the images to be splitted vertically, are kept. Remove any space, special charectors on the folder name')
parser.add_argument('--skip_in_beginning',
help='We can skip splitting for the few images in the beginning. Give the number of images to skip')
parser.add_argument('--skip_in_end',
help='We can skip splitting for the few images in the end. Give the number of images to skip')
#parser.add_argument('--rotate',
# help='angle to rotate the images')
args = parser.parse_args()
#print(args)
input_folder = args.input_folder
skip_in_beginning = int(args.skip_in_beginning)
skip_in_end = int(args.skip_in_end)
#rotate = args.rotate
#print(input_folder)
print("Making a backup if input folder " + input_folder)
backup_command = "cp -iR " + input_folder + " " + input_folder + "_backup"
print(backup_command)
subprocess.getstatusoutput(backup_command)
print("Creaing a folder " + input_folder + "/splitted\n")
if not os.path.isdir(input_folder + "/splitted"):
os.mkdir(input_folder + "/splitted")
all_images = glob.glob(input_folder + "/*")
#print(sorted(all_images)[skip_in_beginning:-(skip_in_end+1)])
print("Adding BitsPerSample to all images")
for image in sorted(all_images):
add_exifdata = "exiftool -SamplesPerPixel=1 -BitsPerSample=1 " + image
print(add_exifdata)
#os.system(add_exifdata)
subprocess.getstatusoutput(add_exifdata)
print("==========\n")
print("Rotating and Cropping the images\n")
for image in sorted(all_images)[skip_in_beginning:-(skip_in_end+1)]:
# print(image)
base_name = os.path.basename(image)
filename_only = os.path.splitext(base_name)[0]
extension = os.path.splitext(base_name)[1].strip(".")
# print(filename_only)
# print(extension)
# if rotate:
# rotate_command = "mogrify -rotate '" + rotate + "' " + image
rotate_command = "exiftool -Orientation=1 -n " + image
print(rotate_command)
#os.system(rotate_command)
subprocess.getstatusoutput(rotate_command)
# time.sleep(5)
#
command = "convert " + image + " -crop 50%x100% " + input_folder +"/splitted/" + filename_only + "-%01d." + extension
print(command)
subprocess.getstatusoutput(command)
for image in sorted(all_images)[:skip_in_beginning]:
command = "cp " + image + " " + input_folder + "/splitted/"
# print(command)
subprocess.getstatusoutput(command)
for image in sorted(all_images)[-(skip_in_end+1):]:
command = "cp " + image + " " + input_folder + "/splitted/"
# print(command)
subprocess.getstatusoutput(command)
# os.system(command)
# image_slicer.slice(image, 2)
# plt.imshow(imCrop(image)[1])
#
# split_image_verticlly(image)
# sys.exit()
# /etc/ImageMagick-6/policy.xml
# <policy domain="resource" name="memory" value="2560MiB"/>
#changed from 256 to 2560
#https://blog.bigbinary.com/2018/09/12/configuring-memory-allocation-in-imagemagick.html
#https://www.daniloaz.com/en/high-cpu-load-when-converting-images-with-imagemagick/
#https://coderwall.com/p/ovlnwa/use-python-and-pil-to-slice-an-image-vertically
clean_up = "rm -f " + input_folder + "/*_original"
print(clean_up)
subprocess.getstatusoutput(clean_up)
print("All Done.")
print("Check the folder " + input_folder + "/splitted")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment