Package: zebrafy
Zebrafyis a Python 3 library for converting PDF and images to and fromZebra Programming Language (ZPL)graphic fields (^GF).Zebrafyconsists of three conversion tools:ZebrafyImage— convert an image into valid ZPLZebrafyPDF— convert a PDF into valid ZPLZebrafyZPL— convert valid ZPL graphic fields into images or PDFIf you want more control over the resulting ZPL data,ZebrafyImageandZebrafyPDFsupport the following optional parameters:ParameterDescriptioncompression_typeZPL graphic field compression type (default"A")"A"— ASCII hexadecimal (most compatible)"B"— Base64 Binary"C"— Z64 compressed binary (best compression)invertInvert the black and white in the image/PDF output. (TrueorFalse, defaultFalse)ditherDither the result instead of hard limit on black pixels. (TrueorFalse, defaultTrue)thresholdBlack pixel threshold for image without dithering (0-255, default128)widthWidth of the image in the resulting ZPL,0to use original image/PDF width (default0)heightHeight of the image in the resulting ZPL,0to use original image/PDF height (default0)pos_xPixel x position of the graphic field in resulting ZPL (default0)pos_yPixel y position of the graphic field in resulting ZPL (default0)complete_zplAdd ZPL header and footer or only get the ZPL graphic field output (TrueorFalse, defaultTrue)Getting StartedInstallationpip install zebrafyDependenciesPip handles all dependencies automatically. This library is built on top of:Pillow— Python Imaging Librarypypdfium2— Python 3 binding toPDFiumExample UsageImage to ZPL Graphic Field withZebrafyImageConvert image bytes into a complete ZPL string and save to file:fromzebrafyimportZebrafyImagewithopen("source.png","rb")asimage:zpl_string=ZebrafyImage(image.read()).to_zpl()withopen("output.zpl","w")aszpl:zpl.write(zpl_string)Example usage with optional parameters:fromzebrafyimportZebrafyImagewithopen("source.png","rb")asimage: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()withopen("output.zpl","w")aszpl:zpl.write(zpl_string)Alternatively,ZebrafyImagealso accepts PIL Image as the image parameter instead of
image bytes:fromPILimportImagefromzebrafyimportZebrafyImagepil_image=Image.new(mode="RGB",size=(100,100))zpl_string=ZebrafyImage(pil_image).to_zpl()withopen("output.zpl","w")aszpl:zpl.write(zpl_string)PDF to ZPL Graphic Field withZebrafyPDFConvert PDF bytes into a complete ZPL string and save to file:fromzebrafyimportZebrafyPDFwithopen("source.pdf","rb")aspdf:zpl_string=ZebrafyPDF(pdf.read()).to_zpl()withopen("output.zpl","w")aszpl:zpl.write(zpl_string)ZebrafyPDFconversion supports the same optional parameters asZebrafyImageconversion:fromzebrafyimportZebrafyPDFwithopen("source.pdf","rb")aspdf: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()withopen("output.zpl","w")aszpl:zpl.write(zpl_string)ZPL to PDF or Images withZebrafyZPLConvert all graphic fields from a valid ZPL file to PIL Images and save to image files:fromzebrafyimportZebrafyZPLwithopen("source.zpl","r")aszpl:pil_images=ZebrafyZPL(zpl.read()).to_images()forcount,pil_imageinenumerate(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:fromzebrafyimportZebrafyZPLwithopen("source.zpl","r")aszpl:pdf_bytes=ZebrafyZPL(zpl.read()).to_pdf()withopen("output.pdf","wb")aspdf:pdf.write(pdf_bytes)Contributing and IssuesContributions and bug reports are welcome and can be submitted on theGitHub 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 commandsExtract text from a PDF to render it as a native ZPL command instead of graphic fieldLicenseThis source is released under theGNU Lesser General Public License v3.0.Logo