Skip to content

Instantly share code, notes, and snippets.

@Wingman4l7
Last active March 14, 2023 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wingman4l7/99c8c2283f4dff695f11cb2768b100ce to your computer and use it in GitHub Desktop.
Save Wingman4l7/99c8c2283f4dff695f11cb2768b100ce to your computer and use it in GitHub Desktop.
import sys
import cv2
if len(sys.argv) != 3:
print("Usage: python comic_slicer.py input_file output_prefix")
sys.exit(1)
input_file = sys.argv[1]
output_prefix = sys.argv[2]
# Load the input image
image = cv2.imread(input_file)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply binary thresholding to convert the image to black and white
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours in the thresholded image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Sort the contours from left to right
contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])
# Loop over the contours and extract each panel as a separate image
for i, ctr in enumerate(contours):
# Get the bounding box coordinates for the contour
x, y, w, h = cv2.boundingRect(ctr)
# Extract the panel as a separate image
panel = image[y:y+h, x:x+w]
# Save the panel as a separate image file
cv2.imwrite(f'output_prefix_{i+1}.png', panel)
## ChatGPT input: ##
# Write a script in python:
# input: an image of a multi-panel comic strip. the panels are delineated clearly by black borders, and the background of the image is white
# output: a series of separate images that consist of all of the individual panels of the comic
# the script will accept the input filename as a command line argument, and to accept a second command line argument as the prefix for the output filenames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment