Skip to content

Instantly share code, notes, and snippets.

@smoketh
Created March 27, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoketh/814bc095880bc48bc64058d52fb108c8 to your computer and use it in GitHub Desktop.
Save smoketh/814bc095880bc48bc64058d52fb108c8 to your computer and use it in GitHub Desktop.
#%%
#imports
from string import ascii_uppercase
import numpy as np
from PIL import Image
import cv2
from numpy import asarray
import os
#%%
def lerp(a:float,b:float,t:float):
return a + (b - a) * t
#%%
###RESIZER
patterns = ['DTRN', 'DTR2', 'DTR3']
directoryIn = 'dtrp'
directoryOut= 'dtrp_out'
directoryMult = 'dtrp_mult'
projectileframeNames = [
'image_026_0014.png',
'image_026_0015.png',
'image_026_0016.png',
'image_026_0017.png',
'image_026_0018.png'
]
projectileStartFrame=12
#cprojframe=0
#pattern= 'DTRN'
#ascii_uppercase
asi=0
pat=0
projectiledata = asarray(Image.open(projectileframeNames[0]))
for filename in os.listdir(directoryIn):
f = os.path.join(directoryIn, filename)
# checking if it is a file
if os.path.isfile(f) and '.png' in f:
namPattern=patterns[pat]+ascii_uppercase[asi]+'0'+".png"
image = Image.open(f)
data = asarray(image)
#print(type(data))
# summarize shape
#print(data.shape)
vl=asi-projectileStartFrame
print("vl: "+str(vl))
dataMult = asarray(Image.open(os.path.join(directoryMult, namPattern )))
ndata=cv2.resize(data, dsize=(320, 200), interpolation=cv2.INTER_AREA)
for y in range(ndata.shape[0]):
for x in range(ndata.shape[1]):
if ndata[y][x][3] < 180:
ndata[y][x][3] = 0
if vl >=0 and vl<len(projectileframeNames):
ndata[y][x]=projectiledata[y][x]
else:
for p in range(3):
a = float(ndata[y][x][p])/255.0
b = float(dataMult[y][x][p])/255.0
ndata[y][x][p] = int(lerp(a, a*b, 0.5)*255.0)
ndata[y][x][3] = 255
image2 = Image.fromarray(ndata)
#print(type(image2))
#print(image2.mode)
#print(image2.size)
new_file = os.path.join(directoryOut, namPattern )
asi+=1
vl=asi-projectileStartFrame
if asi==26:
asi=0
pat+=1
if vl >=0 and vl<len(projectileframeNames):
print('trying to load new projectile data at '+str(vl))
projectiledata = asarray(Image.open(projectileframeNames[vl]))
image2.save(new_file)
#%%
###ALPHA BY GREEN
directory = 'dtrp'
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f) and '.png' in f:
image = Image.open(f)
data = asarray(image)
print(f)
print(data.shape)
for y in range(data.shape[0]):
for x in range(data.shape[1]):
data[y][x][3]=data[y][x][1]
carr= np.array([data[y][x][0], data[y][x][1], data[y][x][2]])
#print(carr)
carr = np.true_divide(carr, 255.0)
divisor=np.linalg.norm(carr)
if divisor == 0:
continue
carr = carr/np.linalg.norm(carr)
carr = np.multiply(carr, 255.0)
data[y][x][0]=int(carr[0])
data[y][x][1]=int(carr[1])
data[y][x][2]=int(carr[2])
#data[y][x][3]=data[y][x][1]
image = Image.fromarray(data)
image.save(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment