Merge pull request #152 from python-escpos/fix/large-image-printing
Fix/large image printing
This commit is contained in:
commit
340a47d2f6
|
@ -204,6 +204,37 @@ Here you can download an example, that will print a set of common barcodes:
|
|||
|
||||
* :download:`barcode.bin </download/barcode.bin>` by `@mike42 <https://github.com/mike42>`_
|
||||
|
||||
Hint: preprocess printing
|
||||
-------------------------
|
||||
|
||||
Printing images directly to the printer is rather slow.
|
||||
One factor that slows down the process is the transmission over e.g. serial port.
|
||||
|
||||
Apart from configuring your printer to use the maximum baudrate (in the case of serial-printers), there is not much
|
||||
that you can do.
|
||||
However you could use the :py:class:`escpos.printer.Dummy`-printer to preprocess your image.
|
||||
This is probably best explained by an example:
|
||||
|
||||
.. code-block:: Python
|
||||
|
||||
from escpos.printer import Serial, Dummy
|
||||
|
||||
p = Serial()
|
||||
d = Dummy()
|
||||
|
||||
# create ESC/POS for the print job, this should go really fast
|
||||
d.text("This is my image:\n")
|
||||
d.image("funny_cat.png")
|
||||
d.cut()
|
||||
|
||||
# send code to printer
|
||||
p._raw(d.output)
|
||||
|
||||
This way you could also store the code in a file and print later.
|
||||
You could then for example print the code from another process than your main-program and thus reduce the waiting time.
|
||||
(Of course this will not make the printer print faster.)
|
||||
|
||||
|
||||
How to update your code for USB printers
|
||||
----------------------------------------
|
||||
|
||||
|
|
|
@ -56,7 +56,8 @@ class Escpos(object):
|
|||
"""
|
||||
pass
|
||||
|
||||
def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster"):
|
||||
def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster",
|
||||
fragment_height=1024):
|
||||
""" Print an image
|
||||
|
||||
You can select whether the printer should print in high density or not. The default value is high density.
|
||||
|
@ -76,9 +77,20 @@ class Escpos(object):
|
|||
:param high_density_vertical: print in high density in vertical direction *default:* True
|
||||
:param high_density_horizontal: print in high density in horizontal direction *default:* True
|
||||
:param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn`
|
||||
:param fragment_height: Images larger than this will be split into multiple fragments *default:* 1024
|
||||
|
||||
"""
|
||||
im = EscposImage(img_source)
|
||||
|
||||
if im.height > fragment_height:
|
||||
fragments = im.split(fragment_height)
|
||||
for fragment in fragments:
|
||||
self.image(fragment,
|
||||
high_density_vertical=high_density_vertical,
|
||||
high_density_horizontal=high_density_horizontal,
|
||||
impl=impl,
|
||||
fragment_height=fragment_height)
|
||||
return
|
||||
|
||||
if impl == "bitImageRaster":
|
||||
# GS v 0, raster format bit image
|
||||
|
|
|
@ -8,6 +8,12 @@ This module contains the image format handler :py:class:`EscposImage`.
|
|||
:license: GNU GPL v3
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import math
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
|
||||
|
@ -30,6 +36,9 @@ class EscposImage(object):
|
|||
else:
|
||||
img_original = Image.open(img_source)
|
||||
|
||||
# store image for eventual further processing (splitting)
|
||||
self.img_original = img_original
|
||||
|
||||
# Convert to white RGB background, paste over white background
|
||||
# to strip alpha.
|
||||
img_original = img_original.convert('RGBA')
|
||||
|
@ -88,3 +97,21 @@ class EscposImage(object):
|
|||
Convert image to raster-format binary
|
||||
"""
|
||||
return self._im.tobytes()
|
||||
|
||||
def split(self, fragment_height):
|
||||
"""
|
||||
Split an image into multiple fragments after fragment_height pixels
|
||||
|
||||
:param fragment_height: height of fragment
|
||||
:return: list of PIL objects
|
||||
"""
|
||||
passes = int(math.ceil(self.height/fragment_height))
|
||||
fragments = []
|
||||
for n in range(0, passes):
|
||||
left = 0
|
||||
right = self.width
|
||||
upper = n * fragment_height
|
||||
lower = min((n + 1) * fragment_height, self.height)
|
||||
box = (left, upper, right, lower)
|
||||
fragments.append(self.img_original.crop(box))
|
||||
return fragments
|
||||
|
|
Loading…
Reference in New Issue