Commit 2e4a62e9 authored by Thomas Tsiakalakis's avatar Thomas Tsiakalakis

implement image rotation

parent ca82987b
......@@ -6,7 +6,7 @@ After=multi-user.target
Type=simple
ExecStartPre=/bin/dmesg -D
ExecStartPre=/bin/bash -c '/bin/echo 0 > /sys/class/graphics/fbcon/cursor_blink'
ExecStart=/opt/imageshow/server.py
ExecStart=/opt/imageshow/server.py 80 1
ExecStop=/bin/dmesg -E
ExecStop=/bin/bash -c '/bin/echo 1 > /sys/class/graphics/fbcon/cursor_blink'
Restart=always
......
#!/usr/bin/env python3
"""
Usage:
./server.py [<port>]
./server.py [<port> [<rotation>]]
port: http port to listen on
rotation: rotate image <rotation> * 90 degrees
"""
import sys
......@@ -20,6 +22,7 @@ RSVGCONVERT_ARGS = ['--unlimited', '--keep-image-data', '--background-color=None
BLANKINGCMD = '/usr/bin/vcgencmd'
BLANKINGARGS_OFF = ['display_power', '0']
BLANKINGARGS_ON = ['display_power', '1']
ROTATION = 0
class Server(BaseHTTPRequestHandler):
_processing = False
......@@ -48,7 +51,7 @@ class Server(BaseHTTPRequestHandler):
def _respondCurrentImage(self):
fh = open('/dev/' + FRAMEBUFFER, 'rb')
image = Image.frombytes('RGBA', (buf_width, buf_height), fh.read(), 'raw').convert('RGBA')
image = Image.frombytes('RGBA', (buf_width, buf_height), fh.read(), 'raw').convert('RGBA').rotate(ROTATION * -90, expand=1)
fh.close()
b,g,r,a = image.split()
new_image = Image.merge("RGBA", (r,g,b,a))
......@@ -144,7 +147,7 @@ def render(payload, isSvg):
else:
print('detected non-SVG')
imageData = payload
return Image.open(BytesIO(imageData)).convert('RGBA')
return Image.open(BytesIO(imageData)).convert('RGBA').rotate(ROTATION * 90, expand=1)
def convert(image):
print('converting')
......@@ -166,7 +169,10 @@ def setDisplayPower(turnOn):
Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
def init():
initfile = '%dX%d.svg' % (buf_width, buf_height)
if(ROTATION % 2 == 0):
initfile = '%dX%d.svg' % (buf_width, buf_height)
else:
initfile = '%dX%d.svg' % (buf_height, buf_width)
if path.isfile(initfile):
print('found init file')
svg = open(initfile, 'rb')
......@@ -180,7 +186,7 @@ def run(server_class=HTTPServer, handler_class=Server, port=80):
w, h = fbsize.read().strip().split(',')
buf_width = int(w)
buf_height = int(h)
lastImage = Image.new('RGBA', (buf_width, buf_height))
lastImage = Image.new('RGBA', (buf_width, buf_height)).rotate(ROTATION * 90, expand=1)
print('detected frame buffer size %dX%d' % (buf_width, buf_height))
chdir(path.dirname(path.realpath(__file__)))
......@@ -197,7 +203,9 @@ def shutdown():
if __name__ == "__main__":
atexit.register(shutdown)
try:
if len(sys.argv) == 2:
if len(sys.argv) == 3:
ROTATION = int(sys.argv[2])
if len(sys.argv) >= 2:
run(port=int(sys.argv[1]))
else:
run()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment