Skip to content

Commit e8bbfaa

Browse files
committed
Added PyCUPS printer module based on PR #113 from @oelegeirnaert
1 parent f4a008e commit e8bbfaa

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

INSTALL.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ In a terminal, enter the following commands
5656
sudo apt install python3-dev python3-pip virtualenv
5757
sudo apt install qt5-default pyqt5-dev pyqt5-dev-tools # for PyQt5-GUI
5858
sudo apt install gphoto2 libgphoto2-dev # to use gphoto2
59+
sudo apt install libcups2-dev # to use pycups
5960
```
6061

6162
If you want to use the gphoto2-cffi bindings you have to install the following packages:

photobooth/defaults.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ enable = True
4242
pdf = False
4343
# Ask for confirmation before printing
4444
confirmation = True
45-
# Printer module to use (PyQt5)
45+
# Printer module to use (PyQt5, PyCUPS)
4646
module = PyQt5
4747
# Paper width in mm
4848
width = 148

photobooth/printer/PrinterPyCups.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# Photobooth - a flexible photo booth software
5+
# Copyright (C) 2019 Balthasar Reuter <photobooth at re - web dot eu>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Affero General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Affero General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Affero General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
# This class was contributed by
21+
# @oelegeirnaert (https://github.com/oelegeirnaert)
22+
# see https://github.com/reuterbal/photobooth/pull/113
23+
24+
import logging
25+
import os
26+
27+
try:
28+
import cups
29+
except ImportError:
30+
logging.error('pycups is not installed')
31+
cups = None
32+
33+
from PIL import ImageQt
34+
35+
from . import Printer
36+
37+
38+
class PrinterPyCups(Printer):
39+
40+
def __init__(self, page_size, print_pdf=False):
41+
42+
self._conn = cups.Connection() if cups else None
43+
self._counter = 0
44+
45+
if os.access('/dev/shm', os.W_OK):
46+
logging.debug('Storing temp files to "/dev/shm/print.jpg"')
47+
self._tmp_filename = '/dev/shm/print.jpg'
48+
else:
49+
logging.debug('Storing temp files to "/tmp/print.jpg"')
50+
self._tmp_filename = '/tmp/print.jpg'
51+
52+
if self._conn is not None:
53+
self._printer = self._conn.getDefault()
54+
55+
logging.info('Using printer "%s"', self._printer)
56+
57+
if print_pdf:
58+
logging.error('Printing to PDF not supported with pycups')
59+
60+
def print(self, picture):
61+
62+
if isinstance(picture, ImageQt.ImageQt):
63+
picture.save(self._tmp_filename)
64+
else:
65+
picture.save(self._tmp_filename, format='JPEG')
66+
67+
if self._conn is not None:
68+
self._conn.printFile(self._printer, self._tmp_filename,
69+
'photobooth %d' % self._counter, {})

photobooth/printer/PrinterPyQt5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def print(self, picture):
5353

5454
logging.info('Printing picture')
5555

56-
picture = picture.scaled(self._printer.paperRect().size(),
56+
picture = picture.scaled(self._printer.pageRect().size(),
5757
QtCore.Qt.KeepAspectRatio,
5858
QtCore.Qt.SmoothTransformation)
5959

photobooth/printer/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
# Available printer modules as tuples of (config name, module name, class name)
2121
modules = (
22-
('PyQt5', 'PrinterPyQt5', 'PrinterPyQt5'), )
22+
('PyQt5', 'PrinterPyQt5', 'PrinterPyQt5'),
23+
('PyCUPS', 'PrinterPyCups', 'PrinterPyCups'))
2324

2425

2526
class Printer:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def get_mo_files(basedir, installdir):
188188
'Pillow',
189189
'gpiozero',
190190
'gphoto2',
191+
'pycups'
191192
],
192193

193194
# List additional groups of dependencies here (e.g. development

0 commit comments

Comments
 (0)