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

Fallback to ImageMagick (with subprocess) if PIL is not available #27

Merged
merged 1 commit into from
Mar 19, 2018
Merged
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
54 changes: 39 additions & 15 deletions octoprint_astroprint/cameramanager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# coding=utf-8

__author__ = "AstroPrint Product Team <product@astroprint.com>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2017 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

import time
import requests
from StringIO import StringIO
from PIL import Image
from threading import Event
import traceback
import warnings

try:
from PIL import Image
except ImportError:
Image = None
import subprocess
traceback.print_exc()
warnings.warn("PIL/Pillow is not available. Will fallback to "
+ "ImageMagick, make sure it is installed.")

# singleton
_instance = None
Expand Down Expand Up @@ -119,20 +130,33 @@ def getPic(self):
pic = r.content
if pic is not None:
if self._settings.global_get(["webcam", "flipH"]) or self._settings.global_get(["webcam", "flipV"]) or self._settings.global_get(["webcam", "rotate90"]):
buf = StringIO()
buf.write(pic)
image = Image.open(buf)
if self._settings.global_get(["webcam", "flipH"]):
image = image.transpose(Image.FLIP_LEFT_RIGHT)
if self._settings.global_get(["webcam", "flipV"]):
image = image.transpose(Image.FLIP_TOP_BOTTOM)
if self._settings.global_get(["webcam", "rotate90"]):
image = image.transpose(Image.ROTATE_90)
transformedImage = StringIO()
image.save(transformedImage, format="jpeg")
transformedImage.seek(0, 2)
transformedImage.seek(0)
pic = transformedImage.read()
if Image:
buf = StringIO()
buf.write(pic)
image = Image.open(buf)
if self._settings.global_get(["webcam", "flipH"]):
image = image.transpose(Image.FLIP_LEFT_RIGHT)
if self._settings.global_get(["webcam", "flipV"]):
image = image.transpose(Image.FLIP_TOP_BOTTOM)
if self._settings.global_get(["webcam", "rotate90"]):
image = image.transpose(Image.ROTATE_90)
transformedImage = StringIO()
image.save(transformedImage, format="jpeg")
transformedImage.seek(0, 2)
transformedImage.seek(0)
pic = transformedImage.read()
else:
args = ["convert", "-"]
if self._settings.global_get(["webcam", "flipV"]):
args += ["-flip"]
if self._settings.global_get(["webcam", "flipH"]):
args += ["-flop"]
if self._settings.global_get(["webcam", "rotate90"]):
args += ["-rotate", "90"]
args += "jpeg:-"
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
pic, _ = p.communicate(pic)

if not self.cameraActive:
self.cameraConnected()
return pic
Expand Down