Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!python
- username = "snyderna1"
- TARGET_DARK_LUMA = float(0.26)
- MAX_DARK_LUMA = float(0.45)
- MIN_LIGHT_LUMA = float(0.55)
- TARGET_LIGHT_LUMA = float(0.74)
- MIN_NORMAL_LUMA = float(0.1)
- TARGET_NORMAL_LUMA = float(0.5)
- MAX_NORMAL_LUMA = float(0.7)
- TARGET_MUTED_SATURATION = float(0.3)
- MAX_MUTED_SATURATION = float(0.4);
- TARGET_VIBRANT_SATURATION = float(1)
- MIN_VIBRANT_SATURATION = float(0.35)
- from PIL import Image
- from PIL import ImageFilter
- from PIL import ImageDraw
- import time
- import subprocess
- import ctypes
- import colorsys
- import numpy
- def draw_palette(infile, outfile, numcolors=20, swatchsize=20, resize=150):
- image = Image.open(infile)
- image = image.resize((resize, resize))
- result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
- result.putalpha(0)
- colors = result.getcolors(resize*resize)
- # Save colors to file
- pal = Image.new('RGB', (swatchsize*numcolors, swatchsize))
- draw = ImageDraw.Draw(pal)
- posx = 0
- for count, col in colors:
- draw.rectangle([posx, 0, posx+swatchsize, swatchsize], fill=col)
- posx = posx + swatchsize
- del draw
- pal.save(outfile, "PNG")
- def get_colors(infile, numcolors=20, swatchsize=20, resize=150):
- image = Image.open(infile)
- image = image.resize((resize, resize))
- result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
- result.putalpha(0)
- colors = result.getcolors(resize*resize)
- retColors = []
- for count, col in colors:
- retColors.append((col[0],col[1],col[2]))
- return retColors
- def average_image_color(filename):
- i = Image.open(filename)
- h = i.histogram()
- # split into red, green, blue
- r = h[0:256]
- g = h[256:256*2]
- b = h[256*2: 256*3]
- # perform the weighted average of each channel:
- # the *index* is the channel value, and the *value* is its weight
- return (
- sum( i*w for i, w in enumerate(r) ) / sum(r),
- sum( i*w for i, w in enumerate(g) ) / sum(g),
- sum( i*w for i, w in enumerate(b) ) / sum(b)
- )
- def createComparisonValue(saturation, targetSaturation, luma, targetLuma, population, highestPopulation):
- return numpy.average((invertDiff(saturation, targetSaturation),invertDiff(luma, targetLuma),population / highestPopulation),
- weights=(float(3),float(6.5),float(.5)))
- def find_color(filename,target_luma,min_luma,max_luma,target_sat,min_sat,max_sat):
- i = Image.open(filename)
- myMax = None
- maxValue = float(0)
- colors = get_colors(filename)
- draw_palette(filename,filename+".png")
- swatches = []
- for col in colors:
- swatches.append( (col , colorsys.rgb_to_hls(float(col[0]/255),float(col[1]/255),float(col[2]/255))) )
- for swatch in swatches:
- SAT = swatch[1][2]
- LUMA = swatch[1][1]
- if SAT >= min_sat and SAT <= max_sat and LUMA >= min_luma and LUMA <= max_luma:
- thisValue = createComparisonValue(SAT,target_sat,LUMA,target_luma,.0,1)
- if myMax == None or thisValue > maxValue:
- myMax = swatch[0]
- maxValue = thisValue
- if myMax==None:
- myMax = (255,255,255)
- return myMax
- def invertDiff(value, targetValue):
- return float(1) - abs(value - targetValue)
- time.sleep(1.5)
- image = Image.open("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg")
- blurred_image = image.filter(ImageFilter.GaussianBlur(70))
- blurred_image.save("C:\\Users\\"+username+"\\Documents\\Rainmeter\\Skins\\Centered\\@Resources\\images\\blurredcover.png")
- SPI_SETDESKWALLPAPER = 20
- SPIF_SENDCHANGE = 2
- SPIF_UPDATEINIFILE = 1
- wallstring = "C:\\Users\\"+username+r"\Documents\Rainmeter\Skins\Centered\@Resources\images\blurredcover.png"
- result = ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallstring, SPIF_SENDCHANGE)
- #color = average_image_color("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg")
- color = find_color("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg", TARGET_NORMAL_LUMA, MIN_NORMAL_LUMA, MAX_NORMAL_LUMA,
- TARGET_VIBRANT_SATURATION, MIN_VIBRANT_SATURATION, float(1))
- settings = open(r'C:\Users\\'+username+'\Documents\Rainmeter\Skins\Centered\@Resources\settings.inc', 'r+')
- settings.seek(settings.tell()+0)
- line=settings.read(6)
- while not line=="":
- if line=="Color=":
- settings.seek(settings.tell()-6)
- settings.write("Color="+str("%03d" % color[0])+","+str("%03d" % color[1])+","+str("%03d" % color[2])+",255")
- break
- settings.seek(settings.tell()-5)
- line=settings.read(6)
- settings.close()
- subprocess.call("C:\\Program Files\\Rainmeter\\Rainmeter.exe !RefreshApp")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement