wallpaper: python3 compatibility

This commit is contained in:
Vincent Bernat 2018-08-05 22:37:14 +02:00
parent c7a3a44b37
commit c3f44fb0c3
2 changed files with 33 additions and 28 deletions

View file

@ -1,21 +1,21 @@
#!/usr/bin/env python #!/usr/bin/env python3
# Build a multi screen wallpaper # Build a multi screen wallpaper
# First argument is the directory where the wallpapers can be # First argument is the directory where the wallpapers can be
# found. We use xinerama to know the dimension of each screen. # found. We use xinerama to know the dimension of each screen.
from __future__ import print_function, unicode_literals, division
import os import os
import random import random
import string
import optparse import optparse
import tempfile import tempfile
import xcb from Xlib import X, display
import xcb.xproto from Xlib.ext import randr
import xcb.xinerama
import Image from PIL import Image
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-d", "--directory", dest="directory", default=".", parser.add_option("-d", "--directory", dest="directory", default=".",
@ -31,27 +31,32 @@ assert not args, "No additional arguments are accepted"
background = None background = None
# Get display size # Get display size
display = xcb.connect() d = display.Display()
root = display.get_setup().roots[0] screen = d.screen()
background = Image.new('RGB', (root.width_in_pixels, root.height_in_pixels)) window = screen.root.create_window(0, 0, 1, 1, 1, screen.root_depth)
background = Image.new('RGB', (screen.width_in_pixels, screen.height_in_pixels))
# Query xinerama (not randr since the query is longer) # Query randr extension
try: screens = []
xinerama = display(xcb.xinerama.key) screen_resources = randr.get_screen_resources(window)
except xcb.ExtensionException: for output in screen_resources.outputs:
xinerama = None output_info = randr.get_output_info(window, output,
if not xinerama or not xinerama.IsActive().reply().state: screen_resources.timestamp)
if output_info.crtc == 0:
continue
crtc_info = randr.get_crtc_info(window, output_info.crtc,
output_info.timestamp)
screens.append((crtc_info.width, crtc_info.height,
crtc_info.x, crtc_info.y))
if not screens:
screens = [(background.size[0], background.size[1], 0, 0)] screens = [(background.size[0], background.size[1], 0, 0)]
else:
screens = [(screen.width, screen.height, screen.x_org, screen.y_org)
for screen in xinerama.QueryScreens().reply().screen_info]
screens.sort(key=lambda screen: -screen[0]*screen[1]) screens.sort(key=lambda screen: -screen[0]*screen[1])
# Get as many random image as we have screens # Get as many random image as we have screens
images = [] images = []
for base, _, files in os.walk(os.path.join(options.directory)): for base, _, files in os.walk(os.path.join(options.directory)):
for i in files: for i in files:
if string.lower(os.path.splitext(i)[1]) in ('.jpg', if os.path.splitext(i)[1].lower() in ('.jpg',
'.jpeg', '.jpeg',
'.png'): '.png'):
images.append(os.path.join(base, i)) images.append(os.path.join(base, i))
@ -63,7 +68,7 @@ images = images[:len(screens)]
# If more than one screen and one image has the right aspect ratio, # If more than one screen and one image has the right aspect ratio,
# use it. # use it.
if len(screens) > 1: if len(screens) > 1:
target = root.width_in_pixels * 100 / root.height_in_pixels target = screen.width_in_pixels * 100 / screen.height_in_pixels
ratios = [image.size[0] * 100 / image.size[1] for image in images] ratios = [image.size[0] * 100 / image.size[1] for image in images]
try: try:
index = ratios.index(target) index = ratios.index(target)
@ -84,8 +89,8 @@ for idx, image in enumerate(images):
if len(screens) > 1 and len(images) == 1: if len(screens) > 1 and len(images) == 1:
# Wide wallpaper # Wide wallpaper
if image.size != (root.width_in_pixels, root.height_in_pixels): if image.size != (screen.width_in_pixels, screen.height_in_pixels):
image = image.resize((root.width_in_pixels, root.height_in_pixels), image = image.resize((screen.width_in_pixels, screen.height_in_pixels),
Image.CUBIC) Image.CUBIC)
background.paste(image, (0, 0)) background.paste(image, (0, 0))
else: else:
@ -94,9 +99,9 @@ else:
image = images[index] image = images[index]
# Find the right size for the screen # Find the right size for the screen
imx, imy = x, image.size[1]*x/image.size[0] imx, imy = x, image.size[1]*x//image.size[0]
if (options.crop and imy < y) or (not options.crop and imy > y): if (options.crop and imy < y) or (not options.crop and imy > y):
imx, imy = image.size[0]*y/image.size[1], y imx, imy = image.size[0]*y//image.size[1], y
if image.size != (imx, imy): if image.size != (imx, imy):
image = image.resize((imx, imy), Image.CUBIC) image = image.resize((imx, imy), Image.CUBIC)
if options.crop: if options.crop:

View file

@ -8,8 +8,8 @@ libnotify-bin
xfonts-terminus xfonts-terminus
fonts-dejavu fonts-dejavu
compton compton
python-xpyb python3-xlib
python-imaging python3-pil
fvwm fvwm
fvwm-crystal fvwm-crystal
gnome-themes-standard gnome-themes-standard