Skip to content

Instantly share code, notes, and snippets.

@exp111
Created June 18, 2020 18:53
Show Gist options
  • Save exp111/51ce853e5dd8e12ce8664650c34f9e58 to your computer and use it in GitHub Desktop.
Save exp111/51ce853e5dd8e12ce8664650c34f9e58 to your computer and use it in GitHub Desktop.
Some stuff I tried to do with color blending and such. The only thing that was actually useful was imgdiff.py
import sys
from PIL import Image
# reads two images and only copies the changed pixels in a new transparent image
bolted = Image.open('bolted.png')
closed = Image.open('closed.png')
boltPix = bolted.load()
closePix = closed.load()
out = Image.new('RGBA', bolted.size, color=(0,0,0,0))
outPix = out.load()
if bolted.size != closed.size:
print("not the same size")
for x in range(bolted.size[0]):
for y in range(bolted.size[1]):
if boltPix[x,y] != closePix[x,y]:
outPix[x,y] = boltPix[x,y]
out.save('out.png') # Save the modified pixels as .png
# the color now
now = (137,48,10)
# the color in the before img
before = (95,66,14)
# r1 = (r3 - r2 + r2*a1)/a1
# g1 = (g3 - g2 + g2*a1)/a1
# b1 = (b3 - b2 + b2*a1)/a1
def calc(now, before, P):
return (
(now[0] - before[0] + before[0] * P)/P,
(now[1] - before[1] + before[1] * P)/P,
(now[2] - before[2] + before[2] * P)/P
)
for i in range(1, 255):
print(str(i) + " = " + str(calc(now, before, i / 255)))
#now = (137,48,10)
trans = (158,39,8)
#before = (95,66,14)
now = (125,53,11)
before= (95,66,14)
# a1 = (r3-r2) / (r1-r2)
# a1 = (g3-g2) / (g1-g2)
# a1 = (b3-b2) / (b1-b2)
def calc(now, before, trans):
return (
(now[0]-before[0])/(trans[0]-before[0]),
(now[1]-before[1])/(trans[1]-before[1]),
(now[2]-before[2])/(trans[2]-before[2])
)
def average(r):
return (r[0] + r[1] + r[2])/3
res = calc(now, before, trans)
res = average(res)
print(res)
print(res * 255)
# the color now
now = (137,48,10)
now2 = (128,41,9)
# the color in the before img
before = (95,66,14)
before2 = (83,57,13)
# r1 = (r3 - r2 + r2*a1)/a1
# g1 = (g3 - g2 + g2*a1)/a1
# b1 = (b3 - b2 + b2*a1)/a1
def calc(now, before, P):
return (
(now[0] - before[0] + before[0] * P)/P,
(now[1] - before[1] + before[1] * P)/P,
(now[2] - before[2] + before[2] * P)/P
)
arr1 = []
for i in range(1, 255):
val = calc(now, before, i / 255)
arr1.append(val)
print(str(i) + " = " + str(val))
arr2 = []
for i in range(1, 255):
val = calc(now2, before2, i / 255)
arr2.append(val)
print(str(i) + " = " + str(val))
def comp(c1,c2):
return round(c1[0]) == round(c2[0]) and round(c1[1]) == round(c2[1]) and round(c1[2]) == round(c2[2])
print("Comparing:")
for i in range(len(arr1)):
#for j in range(len(arr2)):
# if comp(arr1[i], arr2[j]):
# print(str(i) +","+ str(j) + " = " + str(arr1[i]) + ", " + str(arr2[j]))
if comp(arr1[i], arr2[i]):
print(str(i) + " = " + str(arr1[i]) + ", " + str(arr2[i]))
now = [
(137,48,10),
(125,53,11),
(118,56,11),
(115,57,12)
]
trans = (158,39,8)
before= [
(95,66,14),
(95,66,14),
(95,66,14),
(95,66,14)
]
# a1 = (r3-r2) / (r1-r2)
# a1 = (g3-g2) / (g1-g2)
# a1 = (b3-b2) / (b1-b2)
def calc(now, before, trans):
return (
(now[0]-before[0])/(trans[0]-before[0]),
(now[1]-before[1])/(trans[1]-before[1]),
(now[2]-before[2])/(trans[2]-before[2])
)
def average(r):
return (r[0] + r[1] + r[2])/3
for i in range(len(now)):
print("-- " + str(i) + " --")
res = calc(now[i], before[i], trans)
res = average(res)
print(res)
print(res * 255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment