Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to rotate images on load by 90, 180, or 270 degrees #414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/picframe/config/configuration_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ viewer:
display_w: null # width of display surface (null->None will use max returned by hardware)
display_h: null # height of display surface
display_power: 2 # default=0. choices={0, 1, 2}, 0 will use legacy `vcgencmd`, 1 will use `xset`, 2 will use 'wlr-randr' to blank the display
image_rotate: 0 # default=0 (0, 90, 180, 720), amount in degrees to rotate image by
use_glx: False # default=False. Set to True on linux with xserver running. NB use_sdl2 might need to be False for this to work.
use_sdl2: True # default=True. pysdl2 can use display without xserver, it should be installed as a dependency of pi3d
# but might need `sudo apt install libsdl2-dev` if picframe gives errors about missing sdl2
Expand Down
1 change: 1 addition & 0 deletions src/picframe/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'display_w': None,
'display_h': None,
'display_power': 2,
'image_rotate': 0,
'use_glx': False, # default=False. Set to True on linux with xserver running
'use_sdl2': True,
'test_key': 'test_value',
Expand Down
49 changes: 29 additions & 20 deletions src/picframe/viewer_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def __init__(self, config):
self.__display_w = None if config['display_w'] is None else int(config['display_w'])
self.__display_h = None if config['display_h'] is None else int(config['display_h'])
self.__display_power = int(config['display_power'])
self.__image_rotate = int(config['image_rotate'])
if self.__image_rotate not in [0, 90, 180, 270]:
self.__image_rotate = 0
self.__use_sdl2 = config['use_sdl2']
self.__use_glx = config['use_glx']
self.__alpha = 0.0 # alpha - proportion front image to back
Expand Down Expand Up @@ -241,23 +244,31 @@ def __create_image_pair(self, im1, im2):

def __orientate_image(self, im, pic):
ext = os.path.splitext(pic.fname)[1].lower()
if ext in ('.heif', '.heic'): # heif and heic images are converted to PIL.Image obects and are alway in correct orienation # noqa: E501
return im
orientation = pic.orientation
if orientation == 2:
im = im.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
im = im.transpose(Image.ROTATE_180) # rotations are clockwise
elif orientation == 4:
im = im.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
elif orientation == 6:
im = im.transpose(Image.ROTATE_270)
elif orientation == 7:
im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
elif orientation == 8:

if ext not in ('.heif', '.heic'): # heif and heic images are converted to PIL.Image obects and are alway in correct orienation # noqa: E501
orientation = pic.orientation
if orientation == 2:
im = im.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
im = im.transpose(Image.ROTATE_180) # rotations are clockwise
elif orientation == 4:
im = im.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
elif orientation == 6:
im = im.transpose(Image.ROTATE_270)
elif orientation == 7:
im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
elif orientation == 8:
im = im.transpose(Image.ROTATE_90)

if self.__image_rotate == 90:
im = im.transpose(Image.ROTATE_90)
elif self.__image_rotate == 180:
im = im.transpose(Image.ROTATE_180)
elif self.__image_rotate == 270:
im = im.transpose(Image.ROTATE_270)

return im

def __get_mat_image_control_values(self, mat_images_value):
Expand Down Expand Up @@ -303,15 +314,13 @@ def __tex_load(self, pics, size=None): # noqa: C901
im = get_image_meta.GetImageMeta.get_image_object(pics[0].fname)
if im is None:
return None
if pics[0].orientation != 1:
im = self.__orientate_image(im, pics[0])
im = self.__orientate_image(im, pics[0])

if pics[1]:
im2 = get_image_meta.GetImageMeta.get_image_object(pics[1].fname)
if im2 is None:
return None
if pics[1].orientation != 1:
im2 = self.__orientate_image(im2, pics[1])
im2 = self.__orientate_image(im2, pics[1])

screen_aspect, image_aspect, diff_aspect = self.__get_aspect_diff(size, im.size)

Expand Down