Multiplex
Multiplex

Reputation: 11

Pillow not recognising any of its save formats

I am currently trying to use a python file downloaded from GitHub to understand how it works. It picks up system camera inputs as well as the display using pyscreenshot and allows these to be used from a webpage. Everytime I run it I encounter this error message when a user connects to the webpage.

    save_handler = SAVE[format.upper()]
KeyError: 'PNG'

Here is the code:

from flask import Flask, render_template, Response
from camera import VideoCamera
import pyscreenshot
import flask
from PIL import ImageGrab
import PIL
from io import BytesIO
import socket

app = Flask(__name__)

print(PIL.PILLOW_VERSION)

@app.route('/')
def index():
    return render_template('index.html')


def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/screen.png')
def serve_pil_image():
    img_buffer = BytesIO()
    img = ImageGrab.grab()
    img.save(img_buffer, format = 'PNG', quality=10)
    img_buffer.seek(0)
    return flask.send_file(img_buffer, mimetype='image/png')


@app.route('/js/<path:path>')
def send_js(path):
    return flask.send_from_directory('js', path)


@app.route('/css/<path:path>')
def send_css(path):
    return flask.send_from_directory('css', path)


def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP


if __name__ == '__main__':
    app.run(host=get_ip(), debug=False)

I can't seem to find any useful information on why the error is being thrown. I am using Windows 10 and python 3.8.2

Update:

After running a smaller version of this focusing on what I thought the issue was, it shows a screenshot but as a PNG and not JPEG.

Here is the smaller version of the code:

import pyscreenshot
from PIL import ImageGrab
import PIL
from io import BytesIO

img_buffer = BytesIO()
img = ImageGrab.grab()
img.save(img_buffer, format = 'JPEG', quality=10)
img_buffer.seek(0)

img.show()

Upvotes: 0

Views: 58

Answers (1)

Multiplex
Multiplex

Reputation: 11

Changing to a format that does not need a support library and checking that the MIME is set to 'image/gif' or whatever format.

Installing the support libraries for the other formats now.

Upvotes: 1

Related Questions