]> Frank Brehm's Git Trees - my-stuff/python.git/commitdiff
Changed resize algorithm
authorFrank Brehm <frank@brehm-online.com>
Sat, 24 May 2014 14:37:39 +0000 (16:37 +0200)
committerFrank Brehm <frank@brehm-online.com>
Sat, 24 May 2014 14:37:39 +0000 (16:37 +0200)
DE-env.py

index e86c8e96923f7fdcb5dfda42352848b4e1fd5ffa..012c91cf9e8a786e41d968a97a8716d33fbcc868 100755 (executable)
--- a/DE-env.py
+++ b/DE-env.py
@@ -37,6 +37,9 @@ if not os.path.exists(font):
     font = '/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf'
 opacity = 0.6
 
+DEFAULT_MIN_WIDTH = 800
+DEFAULT_MIN_HEIGTH = 600
+
 #-------------------------------------------------------------------------
 def init_logging():
     """
@@ -301,12 +304,14 @@ def get_home_dir():
         raise KeyError("Neither USERPROFILE or HOME environment variables set.")
 
 #-------------------------------------------------------------------------
-def get_max_monitor_height():
+def get_max_monitor_geometry():
 
-    max_height = 768
+    max_width = DEFAULT_MIN_WIDTH
+    max_height = DEFAULT_MIN_HEIGTH
     screen = Gdk.Screen.get_default()
     if screen is None:
-        return max_height
+        log.debug("No screen found, returning %d x %d.", max_width, max_height)
+        return (max_width, max_height)
 
     monitors = []
     nmons = screen.get_n_monitors()
@@ -315,10 +320,12 @@ def get_max_monitor_height():
     for m in range(nmons):
         mg = screen.get_monitor_geometry(m)
         log.debug("Monitor geometry %d: %d x %d", m, mg.width, mg.height)
+        if mg.width > max_width:
+            max_width = mg.width
         if mg.height > max_height:
             max_height = mg.height
 
-    return max_height
+    return (max_width, max_height)
 
 #-------------------------------------------------------------------------
 bg_dirs = [
@@ -368,20 +375,25 @@ new_img_fname = '/tmp/wallpaper.jpg'
 old_img = Image(filename = wpaper)
 new_img = old_img.clone()
 
-screen_height = get_max_monitor_height()
+(screen_width, screen_height) = get_max_monitor_geometry()
+new_height = screen_height
 new_width = int(float(old_img.width) / float(old_img.height) * float(screen_height))
+if new_width < screen_width:
+    new_width = screen_width
+    new_height = int(float(old_img.height) / float(old_img.width) * float(screen_width))
 
 log.debug("Old image size: %d x %d.", old_img.width, old_img.height)
+log.debug("Calculated new image size: %d x %d.", new_width, new_height)
 
 x_plus = int(float(new_width) * 1.01)
 x_minus = int(float(new_width) * 0.99)
-y_plus = int(float(screen_height) * 1.01)
-y_minus = int(float(screen_height) * 0.99)
+y_plus = int(float(new_height) * 1.01)
+y_minus = int(float(new_height) * 0.99)
 
 if (old_img.width < x_minus or old_img.width > x_plus or
         old_img.height < y_minus or old_img.height > y_plus):
-    log.debug("Resizing image to %d x %d.", new_width, screen_height)
-    new_img.resize(new_width, screen_height)
+    log.debug("Resizing image to %d x %d.", new_width, new_height)
+    new_img.resize(new_width, new_height)
 
 black = Color('srgba(0,0,0,%0.1f)' % (opacity))
 white = Color('srgba(255,255,255,%0.1f)' % (opacity))
@@ -400,7 +412,7 @@ draw.font_size = fsize_title
 fm = draw.get_font_metrics(new_img, title, False)
 x = int(new_img.width / 2)
 y = int(fm.text_height)
-log.debug("Koordinaten: x %r, y %r.", x, y)
+log.debug("Koordinaten Titel: x %r, y %r.", x, y)
 draw.text(x, y, title)
 
 draw.font_size = fsize_subtitle
@@ -408,7 +420,7 @@ draw.stroke_width = ssize_subtitle
 fm = draw.get_font_metrics(new_img, wpaper, False)
 x = int(new_img.width / 2)
 y = int(fm.text_height) + y + 5
-log.debug("Koordinaten: x %r, y %r.", x, y)
+log.debug("Koordinaten Untertitel: x %r, y %r.", x, y)
 draw.text(x, y, wpaper)
 
 draw(new_img)