Skip to content

Instantly share code, notes, and snippets.

@RH2
Created September 21, 2020 07:31
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 RH2/d9cdc52f9196dd1d691c5ef1e7598d4c to your computer and use it in GitHub Desktop.
Save RH2/d9cdc52f9196dd1d691c5ef1e7598d4c to your computer and use it in GitHub Desktop.
backup python files
import PIL
from PIL import Image
import math
#from skimage.color import rgb2hsv
#from skimage.color import hsv2rgb
import os
import colorsys
inputDirectory ='C:/Users/Reference/Desktop/raylib-live/games/pixel-runner/conversion-in' #specify a directory of source images
exitDirectory = 'C:/Users/Reference/Desktop/raylib-live/games/pixel-runner/conversion-out' #where to put output images
#get pallet
phex = [
"#dbe0e7","#a3acbe","#67708b","#4e5371","#393a56","#26243a","#141020","#7bcf5c",
"#509b4b","#2e6a42","#1a453b","#0f2738","#0d2f6d","#0f4da3","#0e82ce","#13b2f2",
"#41f3fc","#f0d2af","#e5ae78","#c58158","#945542","#623530","#46211f","#97432a",
"#e57028","#f7ac37","#fbdf6b","#fe979b","#ed5259","#c42c36","#781f2c","#351428",
"#4d2352","#7f3b86","#b45eb3","#e38dd6"]
prgb = [[219,224,231],[163,172,190],[103,112,139],[78,83,113],[57,58,86],[38,36,58],[20,16,32],[123,207,92],[80,155,75],[46,106,66],[26,69,59],[15,39,56],[13,47,109],[15,77,163],[14,130,206],[19,178,242],[65,243,252],[240,210,175],[229,174,120],[197,129,88],[148,85,66],[98,53,48],[70,33,31],[151,67,42],[229,112,40],[247,172,55],[251,223,107],[254,151,155],[237,82,89],[196,44,54],[120,31,44],[53,20,40],[77,35,82],[127,59,134],[180,94,179],[227,141,214]]
def ptohsv(input):
return colorsys.rgb_to_hsv(input[0],input[1],input[2])
def rgbtotup(input):
return((input[0],input[1],input[2]))
phsv = []
for e in prgb:
phsv.append(ptohsv(e))
print(phsv)
#1. go through image pixel by pixel and find pallet color with lowest difference
#2. write that pallet pixel to new image
#3. save file
#image.setpixel((50,60),(255,255,255,0))
#bg = Image.new("RGBA", image.size, (0,0,0,255))
#bg.paste(alpha, mask=alpha)
#bg.convert('L').convert('P', palette=Image.ADAPTIVE, colors=8).save(newfilepath,optimize=True)
a = colorsys.rgb_to_hsv(0.1,0.1,0.1)
print(a)
a = ptohsv(prgb[0])
print(a)
print(len(prgb))
if os.path.exists(inputDirectory): #safety, if the directory to read does not exist, assume that there is a mistake and do not create new filestructure.
if not os.path.exists(exitDirectory):
os.makedirs(exitDirectory)
for filename in os.listdir(inputDirectory):
if filename.endswith(".png") or filename.endswith(".jpg"):
#print(os.path.join(inputDirectory, filename))
thisfile = os.path.join(inputDirectory, filename)
newfilepath = os.path.join(exitDirectory, filename)
#print(newfilepath)
image = Image.open(thisfile).convert('RGBA')
output = Image.new( 'RGBA', (image.size[0],image.size[1]), "black")
#print(image)
#print(output)
#print(image.getpixel((50,60)))
for i in range(image.size[0]): # for every col:
for j in range(image.size[1]): # For every row
lowestScore = 9999999999 #default pallet to use
currentIndex = 0
#print(image.getpixel((i,j)))
#pixels[i,j] = (i, j, 100) # set the colour accordingly
#1. get pixel
thisrgba = image.getpixel((i,j))
#2. convert to hsl
thishsv = colorsys.rgb_to_hsv(thisrgba[0],thisrgba[1],thisrgba[2])
#3. score h, s, and l.
for a in range(len(phsv)):
#score system hue
# totalScore = 0
# hueScore = abs(thishsv[0]-phsv[a][0])*1.0
# hueScore2 = abs(phsv[a][0]-thishsv[0])*1.0
# if(hueScore2<hueScore):
# huescore =hueScore2
# satScore = abs(thishsv[1]-phsv[a][1])*5.0
# valScore = (abs(thishsv[2]-phsv[a][2])/255)*5
# #6. add scores
# totalScore = hueScore+satScore+valScore
# #7. select low score
# if(totalScore<lowestScore):
# currentIndex=a
# lowestScore=totalScore
# #8. write lowest scoring color to pixel
# output.putpixel((i,j),rgbtotup(prgb[currentIndex]))
totalScore = 0
rScore = abs(thisrgba[0]-prgb[a][0])
gScore = abs(thisrgba[1]-prgb[a][1])
bScore = abs(thisrgba[2]-prgb[a][2])
#6. add scores
totalScore = rScore+gScore+bScore
#7. select low score
if(totalScore<lowestScore):
currentIndex=a
lowestScore=totalScore
#8. write lowest scoring color to pixel
output.putpixel((i,j),rgbtotup(prgb[currentIndex]))
#output.convert('L').convert('P', palette=Image.ADAPTIVE, colors=8).save(newfilepath,optimize=True)
output.save(newfilepath,optimize=True)
import PIL
from PIL import Image
import math
from datetime import datetime
#from skimage.color import rgb2hsv
#from skimage.color import hsv2rgb
import os
import colorsys
inputDirectory ='C:/Users/Reference/Desktop/raylib-live/games/pixel-runner/conversion-in' #specify a directory of source images
exitDirectory = 'C:/Users/Reference/Desktop/raylib-live/games/pixel-runner/conversion-out' #where to put output images
#get pallet
phex = [
"#dbe0e7","#a3acbe","#67708b","#4e5371","#393a56","#26243a","#141020","#7bcf5c",
"#509b4b","#2e6a42","#1a453b","#0f2738","#0d2f6d","#0f4da3","#0e82ce","#13b2f2",
"#41f3fc","#f0d2af","#e5ae78","#c58158","#945542","#623530","#46211f","#97432a",
"#e57028","#f7ac37","#fbdf6b","#fe979b","#ed5259","#c42c36","#781f2c","#351428",
"#4d2352","#7f3b86","#b45eb3","#e38dd6"]
prgb = [[219,224,231],[163,172,190],[103,112,139],[78,83,113],[57,58,86],[38,36,58],[20,16,32],[123,207,92],[80,155,75],[46,106,66],[26,69,59],[15,39,56],[13,47,109],[15,77,163],[14,130,206],[19,178,242],[65,243,252],[240,210,175],[229,174,120],[197,129,88],[148,85,66],[98,53,48],[70,33,31],[151,67,42],[229,112,40],[247,172,55],[251,223,107],[254,151,155],[237,82,89],[196,44,54],[120,31,44],[53,20,40],[77,35,82],[127,59,134],[180,94,179],[227,141,214]]
def ptohsv(input):
return colorsys.rgb_to_hsv(input[0],input[1],input[2])
def rgbtotup(input):
return((input[0],input[1],input[2]))
phsv = []
for e in prgb:
phsv.append(ptohsv(e))
# print(phsv)
#1. go through image pixel by pixel and find pallet color with lowest difference
#2. write that pallet pixel to new image
#3. save file
#image.setpixel((50,60),(255,255,255,0))
#bg = Image.new("RGBA", image.size, (0,0,0,255))
#bg.paste(alpha, mask=alpha)
#bg.convert('L').convert('P', palette=Image.ADAPTIVE, colors=8).save(newfilepath,optimize=True)
a = colorsys.rgb_to_hsv(0.1,0.1,0.1)
print(a)
a = ptohsv(prgb[0])
print(a)
print(len(prgb))
print("starting")
if os.path.exists(inputDirectory): #safety, if the directory to read does not exist, assume that there is a mistake and do not create new filestructure.
if not os.path.exists(exitDirectory):
os.makedirs(exitDirectory)
for filename in os.listdir(inputDirectory):
if filename.endswith(".png") or filename.endswith(".jpg"):
#print(os.path.join(inputDirectory, filename))
thisfile = os.path.join(inputDirectory, filename)
newfilepath = os.path.join(exitDirectory, filename)
#print(newfilepath)
image = Image.open(thisfile).convert('RGBA')
output = Image.new( 'RGBA', (image.size[0],image.size[1]), "black")
#print(image)
#print(output)
#print(image.getpixel((50,60)))
for i in range(image.size[0]): # for every col:
for j in range(image.size[1]): # For every row
lowestScore = 9999999999 #default pallet to use
currentIndex = 0
#print(image.getpixel((i,j)))
#pixels[i,j] = (i, j, 100) # set the colour accordingly
#1. get pixel
thisrgba = image.getpixel((i,j))
#2. convert to hsl
thishsv = colorsys.rgb_to_hsv(thisrgba[0],thisrgba[1],thisrgba[2])
#3. score h, s, and l.
for a in range(len(phsv)):
#score system hue
# totalScore = 0
# hueScore = abs(thishsv[0]-phsv[a][0])*1.0
# hueScore2 = abs(phsv[a][0]-thishsv[0])*1.0
# if(hueScore2<hueScore):
# huescore =hueScore2
# satScore = abs(thishsv[1]-phsv[a][1])*5.0
# valScore = (abs(thishsv[2]-phsv[a][2])/255)*5
# #6. add scores
# totalScore = hueScore+satScore+valScore
# #7. select low score
# if(totalScore<lowestScore):
# currentIndex=a
# lowestScore=totalScore
# #8. write lowest scoring color to pixel
# output.putpixel((i,j),rgbtotup(prgb[currentIndex]))
ditherRGB = [0,0,0]
ditherRGB[0] = thisrgba[0]
ditherRGB[1] = thisrgba[1]
ditherRGB[2] = thisrgba[2]
#type0
ditherRGB[0] = ditherRGB[0] + 5.0*math.sin((i/math.tau)+j)
ditherRGB[1] = ditherRGB[1] + 5.0*math.cos((i/math.tau)+j)
ditherRGB[2] = ditherRGB[2] + 5.0*math.sin((i/math.tau)+j)
#type1
switch = 1
if(j%2==0):
switch = -1
ditherRGB[0] = ditherRGB[0] + 2.0*math.sin(1*(i+0 )*(switch))
ditherRGB[1] = ditherRGB[1] + 2.0*math.sin(1*(i+math.tau/3 )*(switch))
ditherRGB[2] = ditherRGB[2] + 2.0*math.sin(1*(i+2*math.tau/3)*(switch))
totalScore = 0
rScore = abs(ditherRGB[0]-prgb[a][0])
gScore = abs(ditherRGB[1]-prgb[a][1])
bScore = abs(ditherRGB[2]-prgb[a][2])
#6. add scores
totalScore = rScore+gScore+bScore
#7. select low score
if(totalScore<lowestScore):
currentIndex=a
lowestScore=totalScore
#8. write lowest scoring color to pixel
output.putpixel((i,j),rgbtotup(prgb[currentIndex]))
#output.convert('L').convert('P', palette=Image.ADAPTIVE, colors=8).save(newfilepath,optimize=True)
output.save(newfilepath,optimize=True)
print("finished")
print(datetime.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment