Skip to content

Instantly share code, notes, and snippets.

@tomatosoupcan
Created August 10, 2020 20:53
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 tomatosoupcan/26f75089f1bb23aee04cb9f9acedf5fe to your computer and use it in GitHub Desktop.
Save tomatosoupcan/26f75089f1bb23aee04cb9f9acedf5fe to your computer and use it in GitHub Desktop.
A simple python script to draw web images into jackbox games
import mouse
import keyboard
import cv2
import numpy as np
import urllib.request
import pyautogui
import time
from matplotlib import pyplot as plt
from matplotlib.patches import Circle
import gc
from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, \
AdaptiveETA, FileTransferSpeed, FormatLabel, Percentage, \
ProgressBar, ReverseBar, RotatingMarker, \
SimpleProgress, Timer, UnknownLength
#get image url and save it as lineart
url = input("Enter URL to black and white jpg: ")
if url:
urllib.request.urlretrieve(url, "image.jpg")
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
else:
img = cv2.imread('image2.png', cv2.IMREAD_GRAYSCALE)
cs = int(input("Set Cursor Size in Pixels (sweet spot seems to be 3): "))
input("Move Mouse to Upper Left Bounds and Press Enter")
ul = mouse.get_position()
input("Move Mouse to Lower Right Bounds and Press Enter")
lr = mouse.get_position()
xsize = lr[0]-ul[0]
ysize = lr[1]-ul[1]
resized = cv2.resize(img, (xsize, ysize), interpolation = cv2.INTER_AREA)
resized = cv2.Canny(resized,100,200)
#resized = cv2.ximgproc.thinning(resized,cv2.ximgproc.THINNING_GUOHALL)
resized = (255-resized)
resized = cv2.erode(resized,np.ones((5,5), np.uint8) , iterations=1)
resized = cv2.copyMakeBorder(resized, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=(255,255,255))
resized = (255-resized)
resized = cv2.erode(resized,np.ones((2,2), np.uint8) , iterations=3)
resized = (255-resized)
cv2.imwrite("Adaptivemean_thresh_binary.jpg", resized);
#intialize used pixels list
usedpixels=[]
#set relative cursor position
relcurx = 0
relcury = 0
#set absolute cursor location
mcurx = ul[0]
mcury = ul[1]
#set maximum x and y positions
maxx = resized.shape[0]
maxy = resized.shape[1]
def get_start():
#for pixel in image, find the next black pixel from the top that hasn't already been used and return it, if all pixels used, return 0
for x in range(0, maxx):
for y in range(0, maxy):
if resized[x, y] == 0:
return [y, x]
return 0
returnedpoint = [0,0]
topcount = (resized == 0).sum()
#setup the progressbar
bar = ProgressBar(widgets=[Percentage(), Bar()],maxval = topcount).start()
lastposx = 0
while returnedpoint != 0:
returnedpoint = get_start()
if returnedpoint == 0:
break
#provide myself a way out of mouse hell
if keyboard.is_pressed('escape'):
break
#store off the current first black pixel
posx = returnedpoint[0]
posy = returnedpoint[1]
resized[posy,posx] = 1
usedpixels=[(posy,posx)]
#delay more if moving left
pyautogui.PAUSE = 0.035
#move mouse to the start
mouse.move(mcurx+posx, mcury+posy, absolute = True, duration = 0)
#give the eventual downclick some breathing room
pyautogui.mouseDown();
lastposx = posx
#latchon from the start position, latch will turn off when we can't find any other pixels to jump to
loopon = 0
usedpixels = [(posy,posx)]
while loopon == 0:
#provide myself a way out of mouse hell
if keyboard.is_pressed('escape'):
break
#move mouse on each loop
mouse.move(mcurx+posx, mcury+posy, absolute = True, duration = 0)
time.sleep(1/10000000000)
#check each direction, prioritizing right due to the nature of finding the first point, work clockwise-ish
if resized[posy, posx + 1] == 0:
#print("right")
posy = posy
posx = posx + 1
elif resized[posy + 1, posx] == 0:
#print("down")
posx = posx
posy = posy + 1
elif resized[posy, posx - 1] == 0:
#print("left")
posx = posx - 1
posy = posy
elif resized[posy - 1, posx] == 0:
#print("up")
posx = posx
posy = posy - 1
elif resized[posy + 1, posx + 1] == 0:
#print("downright")
posy = posy + 1
posx = posx + 1
elif resized[posy + 1, posx - 1] == 0:
#print("downleft")
posx = posx - 1
posy = posy + 1
elif resized[posy - 1, posx - 1] == 0:
#print("upleft")
posx = posx - 1
posy = posy - 1
elif resized[posy - 1, posx + 1] == 0:
#print("upright")
posx = posx + 1
posy = posy - 1
else:
#print("break")
loopon = 1
break
resized[posy,posx] = 1
usedpixels.append((posy, posx))
for pos in usedpixels:
cv2.circle(resized, (pos[1],pos[0]), cs, (255,255,255),cv2.FILLED)
del usedpixels
gc.collect()
pyautogui.mouseUp();
curcount = (resized == 0).sum()
progress = topcount - curcount
bar.update(progress)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment