Usage
Installation
To use Zebrafy, first install it using pip:
(.venv) $ pip install zebrafy
ZebrafyPDF and ZebrafyImage Parameters
Parameter |
Description |
|---|---|
|
ZPL graphic field format type (default
|
|
Invert the black and white in the image/PDF output. ( |
|
Dither the result instead of hard limit on black pixels. ( |
|
Black pixel threshold for image without dithering ( |
|
Width of the image in the resulting ZPL, |
|
Height of the image in the resulting ZPL, |
|
Pixel x position of the graphic field in resulting ZPL (default |
|
Pixel y position of the graphic field in resulting ZPL (default |
|
Rotates the image by the specified degree ( |
|
Number of characters in graphic field content after which a line break is inserted (default |
|
Add ZPL header and footer or only get the ZPL graphic field output ( |
Additionally, ZebrafyPDF supports the following optional parameters:
Parameter |
Description |
|---|---|
|
Pixels per PDF canvas unit, defines resolution scaling of the PDF image (<72: compress, >72: stretch, default |
|
Split the PDF into separate ZPL labels for each page ( |
Conversions
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(),
format="Z64",
invert=True,
dither=False,
threshold=128,
width=720,
height=1280,
pos_x=100,
pos_y=100,
rotation=90,
string_line_break=80,
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, with the addition of the split_pages parameter to split the PDF pages:
from zebrafy import ZebrafyPDF
with open("source.pdf", "rb") as pdf:
zpl_string = ZebrafyPDF(
pdf.read(),
format="Z64",
invert=True,
dither=False,
threshold=128,
dpi=72,
width=720,
height=1280,
pos_x=100,
pos_y=100,
rotation=90,
string_line_break=80,
complete_zpl=True,
split_pages=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)