Advertisement
snyderxc

Spotify Python Rainmeter Skin

Jan 11th, 2015
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. #!python
  2.  
  3. username = "snyderna1"
  4.  
  5.  
  6. TARGET_DARK_LUMA = float(0.26)
  7. MAX_DARK_LUMA = float(0.45)
  8. MIN_LIGHT_LUMA = float(0.55)
  9. TARGET_LIGHT_LUMA = float(0.74)
  10. MIN_NORMAL_LUMA = float(0.1)
  11. TARGET_NORMAL_LUMA = float(0.5)
  12. MAX_NORMAL_LUMA = float(0.7)
  13. TARGET_MUTED_SATURATION = float(0.3)
  14. MAX_MUTED_SATURATION = float(0.4);
  15. TARGET_VIBRANT_SATURATION = float(1)
  16. MIN_VIBRANT_SATURATION = float(0.35)
  17.  
  18. from PIL import Image
  19. from PIL import ImageFilter
  20. from PIL import ImageDraw
  21. import time
  22. import subprocess
  23. import ctypes
  24. import colorsys
  25. import numpy
  26. def draw_palette(infile, outfile, numcolors=20, swatchsize=20, resize=150):
  27.  
  28.     image = Image.open(infile)
  29.     image = image.resize((resize, resize))
  30.     result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
  31.     result.putalpha(0)
  32.     colors = result.getcolors(resize*resize)
  33.  
  34.     # Save colors to file
  35.  
  36.     pal = Image.new('RGB', (swatchsize*numcolors, swatchsize))
  37.     draw = ImageDraw.Draw(pal)
  38.  
  39.     posx = 0
  40.     for count, col in colors:
  41.         draw.rectangle([posx, 0, posx+swatchsize, swatchsize], fill=col)
  42.         posx = posx + swatchsize
  43.  
  44.     del draw
  45.     pal.save(outfile, "PNG")
  46.    
  47. def get_colors(infile, numcolors=20, swatchsize=20, resize=150):
  48.  
  49.     image = Image.open(infile)
  50.     image = image.resize((resize, resize))
  51.     result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
  52.     result.putalpha(0)
  53.     colors = result.getcolors(resize*resize)
  54.  
  55.     retColors = []
  56.     for count, col in colors:
  57.         retColors.append((col[0],col[1],col[2]))
  58.  
  59.     return retColors
  60.  
  61. def average_image_color(filename):
  62.     i = Image.open(filename)
  63.     h = i.histogram()
  64.  
  65.     # split into red, green, blue
  66.     r = h[0:256]
  67.     g = h[256:256*2]
  68.     b = h[256*2: 256*3]
  69.  
  70.     # perform the weighted average of each channel:
  71.     # the *index* is the channel value, and the *value* is its weight
  72.     return (
  73.         sum( i*w for i, w in enumerate(r) ) / sum(r),
  74.         sum( i*w for i, w in enumerate(g) ) / sum(g),
  75.         sum( i*w for i, w in enumerate(b) ) / sum(b)
  76.     )
  77. def createComparisonValue(saturation, targetSaturation, luma, targetLuma, population, highestPopulation):
  78.     return numpy.average((invertDiff(saturation, targetSaturation),invertDiff(luma, targetLuma),population / highestPopulation),
  79.                          weights=(float(3),float(6.5),float(.5)))
  80.    
  81.    
  82. def find_color(filename,target_luma,min_luma,max_luma,target_sat,min_sat,max_sat):
  83.     i = Image.open(filename)
  84.     myMax = None
  85.     maxValue = float(0)
  86.     colors = get_colors(filename)
  87.     draw_palette(filename,filename+".png")
  88.     swatches = []
  89.     for col in colors:
  90.         swatches.append( (col , colorsys.rgb_to_hls(float(col[0]/255),float(col[1]/255),float(col[2]/255))) )
  91.  
  92.     for swatch in swatches:
  93.         SAT = swatch[1][2]
  94.         LUMA = swatch[1][1]
  95.         if SAT >= min_sat and SAT <= max_sat and LUMA >= min_luma and LUMA <= max_luma:
  96.             thisValue = createComparisonValue(SAT,target_sat,LUMA,target_luma,.0,1)
  97.             if myMax == None or thisValue > maxValue:
  98.                 myMax = swatch[0]
  99.                 maxValue = thisValue
  100.                
  101.    
  102.     if myMax==None:
  103.         myMax = (255,255,255)
  104.     return myMax
  105.  
  106. def invertDiff(value, targetValue):
  107.     return float(1) - abs(value - targetValue)
  108.    
  109. time.sleep(1.5)
  110.  
  111. image = Image.open("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg")
  112.  
  113. blurred_image = image.filter(ImageFilter.GaussianBlur(70))
  114.  
  115. blurred_image.save("C:\\Users\\"+username+"\\Documents\\Rainmeter\\Skins\\Centered\\@Resources\\images\\blurredcover.png")
  116.  
  117. SPI_SETDESKWALLPAPER = 20
  118. SPIF_SENDCHANGE = 2
  119. SPIF_UPDATEINIFILE = 1
  120. wallstring = "C:\\Users\\"+username+r"\Documents\Rainmeter\Skins\Centered\@Resources\images\blurredcover.png"
  121. result = ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallstring, SPIF_SENDCHANGE)
  122.  
  123. #color = average_image_color("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg")
  124. color = find_color("C:\\Users\\"+username+"\\Documents\\Rainmeter\\SpotifyPlugin\\cover.jpg", TARGET_NORMAL_LUMA, MIN_NORMAL_LUMA, MAX_NORMAL_LUMA,
  125.                 TARGET_VIBRANT_SATURATION, MIN_VIBRANT_SATURATION, float(1))
  126. settings = open(r'C:\Users\\'+username+'\Documents\Rainmeter\Skins\Centered\@Resources\settings.inc', 'r+')
  127.  
  128. settings.seek(settings.tell()+0)
  129. line=settings.read(6)
  130.  
  131. while not line=="":
  132.   if line=="Color=":
  133.     settings.seek(settings.tell()-6)
  134.     settings.write("Color="+str("%03d" % color[0])+","+str("%03d" % color[1])+","+str("%03d" % color[2])+",255")
  135.     break
  136.   settings.seek(settings.tell()-5)
  137.   line=settings.read(6)
  138. settings.close()
  139.  
  140. subprocess.call("C:\\Program Files\\Rainmeter\\Rainmeter.exe !RefreshApp")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement