Last active
March 27, 2019 18:51
-
-
Save WitherOrNot/5507ae44777cd451bce16e9607e8ce0b to your computer and use it in GitHub Desktop.
A bot that draws images in MS Paint using the power of edge detection and dank memes
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
import numpy as np | |
import cv2 | |
import matplotlib.pyplot as plt | |
import pyautogui | |
import time | |
import sys | |
im = cv2.imread(sys.argv[1]) | |
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) | |
ret,thresh = cv2.threshold(imgray,127,255,0) | |
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) | |
print("Got contour") | |
cnth = [[x for x in list(np.vstack(cnt).squeeze())] for cnt in list(contours)] | |
print("Got point list") | |
for x in range(len(cnth)): | |
if type(cnth[x][0]) != type(np.array([])): | |
cnth[x] = [np.array(cnth[x])] | |
cntx = [[a[0] for a in b] for b in cnth] | |
cnty = [[a[1] for a in b] for b in cnth] | |
print("Got each coordinate") | |
""" | |
y: 139 - 996 | |
x: 0 - 1902 | |
max res: 1902 x 857 | |
""" | |
h,w = im.shape[:2] | |
if w > 1902 or h > 857: | |
raise Exception("Image is too large to draw. Please resize the image.") | |
img = cv2.drawContours(np.ones((h,w,3), np.uint8)*255, contours, -1, (0,0,0), 1) | |
cv2.imshow('Preview',img) | |
cv2.waitKey(0) | |
raw_input("Press enter to draw all "+str(len(cntx))+" layers") | |
for i in range(len(cntx)): | |
print("Plotting layer "+str(i)+"...") | |
x,y = cntx[i][0], cnty[i][0] | |
pyautogui.moveTo(x, y+139) | |
for x,y in zip(cntx[i],cnty[i]): | |
pyautogui.dragTo(x, y+139) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment