Skip to content

Instantly share code, notes, and snippets.

@arenasys
arenasys / blend.py
Created March 2, 2023 21:08
np/cv2 blending modes
import numpy as np
import cv2
def NormalComposition(srcRGBA, dstRGBA):
srcA = np.dstack((srcRGBA[:,:,3]/255,)*3)
srcRGB = srcRGBA[:,:,:3]
dstA = np.dstack((dstRGBA[:,:,3]/255,)*3)
dstRGB = dstRGBA[:,:,:3]
outA = srcA + dstA*(1-srcA)
@arenasys
arenasys / parse.py
Last active January 9, 2023 09:20
prompt parser
import lark
class WeightedTree(lark.Tree):
pass
prompt_grammar = lark.Lark(r"""
!start: (prompt | /[][():]/+)*
prompt: (emphasis | deemphasis | numeric | scheduled | alternate | plain | WHITESPACE)*
emphasis: "(" prompt ")"
deemphasis: "[" prompt "]"
@arenasys
arenasys / extract_vae.py
Last active December 30, 2022 18:41
VAE Extractor
import torch
allowed_keys = [
"encoder",
"decoder",
"quant_conv",
"post_quant_conv"]
def extract_vae(model, half=False):
out = {}
@arenasys
arenasys / prune.py
Last active January 3, 2023 14:01
prune.py
#!/usr/bin/python
import torch
import safetensors
import safetensors.torch
import os
MODEL_KEYS = [
"model.",
"first_stage_model",
@arenasys
arenasys / squarize.py
Created October 11, 2022 11:12
squarize images via LD infilling
import argparse, os, sys, glob
import urllib.request
import statistics
# put this script in stable-diffusion-webui and run it: python squarize.py
# the model will be downloaded and the squarize folder will be created.
# all images in squarize/input get processed and inpainted, ending up in squarize/output
# *stable-diffusion-webui needs to be working
@arenasys
arenasys / arenas_downloader.py
Last active October 4, 2022 22:45
scrapes booru's into training data for waifu diffusion
#!/usr/bin/python
import os
import re
import json
import random
from PIL import Image, ImageDraw, ImageOps
from multiprocessing import Pool
import subprocess
import sys
import urllib.request