Home

code from Alyssa

Preview

Download here: why2.py

Back to the article: 88x31 button you can draw on

import time
from PIL import Image
import requests
from io import BytesIO
import random
import threading

def do_the_thing():
    # load our image
    im = Image.open(r"what.png")
    # get it as a 2d array
    overlay = make_array(im)
    debug_image(overlay, "overlay.png")
    imgdat = overlay
    # how many threads do you want?
    num_threads = 10
    threads = []
    thread_pixels = []
    thread_content = []
    # make arrays
    for stuff in range(num_threads):
        thread_content.append([])
    # prepare a bunch of pixel packets
    # pretty much just: x, y, r, g, b
    print("Creating pixel packets...")
    for y in range(31):
        for x in range(88):
            # get pixel color information
            raw = imgdat[y][x]
            red = raw[0]
            green = raw[1]
            blue = raw[2]
            if (raw[3] == 0):
                print("transparent pixel, skipping")
                continue
            # create a pixel packet
            thread_pixels.append([x, y, red, green, blue])
    # shuffle for good measure
    random.shuffle(thread_pixels)
    print("Created " + str(len(thread_pixels)) + " pixel packets")
    count = 0
    # spread these out between threads
    for pixel in thread_pixels:
            thread_content[count].append(pixel)
            count += 1
            # make sure it doesn't overflow
            if (count == num_threads):
                count = 0
    # make threads
    print("Now creating threads...")
    for x in range(num_threads):
        random.shuffle(thread_content[x])
        thread = threading.Thread(target=draw_thread, args=(thread_content[x], "thread" + str(x),))
        threads.append(thread)
    print("Starting threads, prepare for vandalism!")
    for evil in threads:
        evil.start()
    for evil in threads:
        evil.join()

def draw_pixel(x, y, r, g, b):
    text = ""
    count = 0
    while (text != "\"OK\""):
        print("Send attempt #" + str(count))
        # headers for funny
        headers = {'User-Agent': "Mozilla/5.0 (World's shittiest code) Dankbrowser/4.20"}
        print("Sending pixel at X: " + str(x) + " Y: " + str(y))
        url = "https://cqql.site/chaosbtn/draw?x=" + str(x) + "&y=" + str(y) + "&r=" + str(r) + "&g=" + str(g) + "&b=" + str(b)
        # send the request
        resp = requests.get(url, headers=headers)
        # i dont think this actually matters
        text = resp.text
        print(text)
        count += 1


def draw_thread(array, name):
    print("Thread " + name + " is now ready to blast pixels!")
    # loop thru all pixel packets and do the thing
    for pixel in array:
        print("Drawing pixel at X:" + str(pixel[0]) + " Y:" + str(pixel[1]))
        draw_pixel(pixel[0], pixel[1], pixel[2], pixel[3], pixel[4])
    print("Thread " + name + " is done drawing its pixels!")

def debug_image(array, name):
    verify(array)
    test_save(array, name)

def test_save(array, name):
    im = Image.new(mode="RGBA", size=(88,31))
    for y in range(31):
        for x in range(88):
            crying = array[y][x]
            im.putpixel((x, y), (crying[0], crying[1], crying[2], crying[3]))
    im.save(name)



def verify(data):
    count = 0
    for y in range(31):
        for x in range(88):
            bruh = data[y][x]
            for c in range(4):
                if (bruh[c] > 0):
                    count += 1
    if (count == 0):
        print("ERRROR: image is all 0!")
        exit()

def make_array(img):
    pixels = list(img.getdata())
    # index of current pixel
    index = 0
    theimg = []
    for y in range(31):
        row = []
        for x in range(88):
            row.append(list(pixels[index]))
            index += 1
        theimg.append(row)
    if (len(theimg) < 31):
        print("ERROR: The math didnt math. you should look into this")
        exit()
    return theimg




if __name__ == "__main__":
    do_the_thing()

Home