Skip to main content

Python library for converting PDF and images to Zebra Programming Language (ZPL)

Project description

CI Documentation Status PyPi Package License

Zebrafy is a Python 3 library for converting PDF and images to and from Zebra Programming Language (ZPL) graphic fields (^GF).

Zebrafy consists of three conversion tools:

  • ZebrafyImage — convert an image into valid ZPL

  • ZebrafyPDF — convert a PDF into valid ZPL

  • ZebrafyZPL — convert valid ZPL graphic fields into images or PDF

If you want more control over the resulting ZPL data, ZebrafyImage and ZebrafyPDF support the following optional parameters:

Parameter

Description

compression_type

ZPL graphic field compression type (default "A")

  • "A" — ASCII hexadecimal (most compatible)

  • "B" — Base64 Binary

  • "C" — Z64 compressed binary (best compression)

invert

Invert the black and white in the image/PDF output. (True or False, default False)

dither

Dither the result instead of hard limit on black pixels. (True or False, default True)

threshold

Black pixel threshold for image without dithering (0-255, default 128)

width

Width of the image in the resulting ZPL, 0 to use original image/PDF width (default 0)

height

Height of the image in the resulting ZPL, 0 to use original image/PDF height (default 0)

pos_x

Pixel x position of the graphic field in resulting ZPL (default 0)

pos_y

Pixel y position of the graphic field in resulting ZPL (default 0)

complete_zpl

Add ZPL header and footer or only get the ZPL graphic field output (True or False, default True)

Getting Started

Installation

pip install zebrafy

Dependencies

Pip handles all dependencies automatically. This library is built on top of:

Example Usage

Image to ZPL Graphic Field with ZebrafyImage

Convert image bytes into a complete ZPL string and save to file:

from zebrafy import ZebrafyImage

with open("source.png", "rb") as image:
    zpl_string = ZebrafyImage(image.read()).to_zpl()

with open("output.zpl", "w") as zpl:
    zpl.write(zpl_string)

Example usage with optional parameters:

from zebrafy import ZebrafyImage

with open("source.png", "rb") as image:
    zpl_string = ZebrafyImage(
        image.read(),
        compression_type="C",
        invert=True,
        dither=False,
        threshold=128,
        width=720,
        height=1280,
        pos_x=100,
        pos_y=100,
        complete_zpl=True,
    ).to_zpl()

with open("output.zpl", "w") as zpl:
    zpl.write(zpl_string)

Alternatively, ZebrafyImage also accepts PIL Image as the image parameter instead of image bytes:

from PIL import Image
from zebrafy import ZebrafyImage

pil_image = Image.new(mode="RGB", size=(100, 100))
zpl_string = ZebrafyImage(pil_image).to_zpl()

with open("output.zpl", "w") as zpl:
    zpl.write(zpl_string)

PDF to ZPL Graphic Field with ZebrafyPDF

Convert PDF bytes into a complete ZPL string and save to file:

from zebrafy import ZebrafyPDF

with open("source.pdf", "rb") as pdf:
    zpl_string = ZebrafyPDF(pdf.read()).to_zpl()

with open("output.zpl", "w") as zpl:
    zpl.write(zpl_string)

ZebrafyPDF conversion supports the same optional parameters as ZebrafyImage conversion:

from zebrafy import ZebrafyPDF

with open("source.pdf", "rb") as pdf:
    zpl_string = ZebrafyPDF(
        pdf.read(),
        compression_type="C",
        invert=True,
        dither=False,
        threshold=128,
        width=720,
        height=1280,
        pos_x=100,
        pos_y=100,
        complete_zpl=True,
    ).to_zpl()

with open("output.zpl", "w") as zpl:
    zpl.write(zpl_string)

ZPL to PDF or Images with ZebrafyZPL

Convert all graphic fields from a valid ZPL file to PIL Images and save to image files:

from zebrafy import ZebrafyZPL

with open("source.zpl", "r") as zpl:
    pil_images = ZebrafyZPL(zpl.read()).to_images()
    for count, pil_image in enumerate(pil_images):
        pil_image.save(f"output_{count}.png", "PNG")

Convert all graphic fields from a valid ZPL file to PDF bytes and save to PDF file:

from zebrafy import ZebrafyZPL

with open("source.zpl", "r") as zpl:
    pdf_bytes = ZebrafyZPL(zpl.read()).to_pdf()

with open("output.pdf", "wb") as pdf:
    pdf.write(pdf_bytes)

Contributing and Issues

Contributions and bug reports are welcome and can be submitted on the GitHub page.

The project does not yet have a well-defined scope, and I’m open to new feature requests. Features currently in consideration are:

  • HTML to ZPL conversion by implementing standard HTML elements into ZPL commands

  • Extract text from a PDF to render it as a native ZPL command instead of graphic field

License

This source is released under the GNU Lesser General Public License v3.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zebrafy-1.0.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

zebrafy-1.0.1-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file zebrafy-1.0.1.tar.gz.

File metadata

  • Download URL: zebrafy-1.0.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for zebrafy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1b48df0cab6ea1880f4e1b3247a6572642b7e05211ecb9de793b06f438650359
MD5 efc63ed11be6d6e609276ef25f5df90b
BLAKE2b-256 a4f3c5be5929b0d024a00a1eb0a19d56b32939b10db937608fe3e578a3ee26e9

See more details on using hashes here.

File details

Details for the file zebrafy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: zebrafy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for zebrafy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9aec9fb389dda9cf7846465727a82c1e38a4ad90e59fc07913c1f60199ce513c
MD5 b27c5eaa07ecd96ae0314d605c7d4886
BLAKE2b-256 04c53cb6c538a0ad3f7af7c6d6c9f0bf02f2fcd169941ac960464cbca2c1cd66

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page