How to print a logo on labels using a Zebra printer and sending ZPL instructions to it

55,859

Solution 1

It sounds like you have some existing ZPL code, and all you want to do is add an image to it.

If that's the case, the easiest solution is probably to go to the Labelary online ZPL viewer, paste your ZPL into the viewer, click "Add image", and upload the image that you want to add to the ZPL.

This should modify your ZPL by adding the image ZPL commands that you need, and you can then tweak the position, etc.

Solution 2

Here is another option: I created my own image to .GRF converter in python. Feel free to use it.

from PIL import Image, ImageOps
import re
import itertools
import numpy as np

# Use: https://www.geeksforgeeks.org/round-to-next-greater-multiple-of-8/
def RoundUp(x, multiple_of = 8):
    return ((x + 7) & (-1 * multiple_of))

def image2grf(filePath, width = None, height = None, rotate = None):
    image = Image.open(filePath).convert(mode = "1")

    #Resize image to desired size
    if (width != None):
        size = (width, height or width)
        if (isinstance(size[0], float)):
            size = (int(size[0] * image.width), int(size[1] * image.height))

        #Size must be a multiple of 8
        size = (RoundUp(size[0]), RoundUp(size[1]))


        # image.thumbnail(size, Image.ANTIALIAS)
        image = image.resize(size)

    if (rotate != None):
        image = image.rotate(rotate, expand = True)

    image_asArray = np.asarray(np.asarray(image, dtype = 'int'), dtype = 'str').tolist()
    
    bytesPerRow = len(image_asArray[0])
    nibblesPerRow = bytesPerRow // 4
    totalBytes = nibblesPerRow * len(image_asArray)

    #Convert image to hex string
    hexString = "".join(
        format(int("".join(row[i*4:i*4 + 4]), 2) ^ 0xF, "x")
        for row in image_asArray
        for i in range(nibblesPerRow) 
    )

    #Compose data
    data = "~DGimage," + str(totalBytes // 2) + "," + str(nibblesPerRow // 2) + "," + hexString

    #Save image
    fileHandle = open(r"labelPicture.grf", "w")
    fileHandle.write(data)
    fileHandle.close()

if __name__ == '__main__':
    # image2grf(r"warning.bmp")
    image2grf(r"pallet_label_icons.png", rotate = 90)

Edit: I updated the code above to use my new conversion method, which produces better resolution GRF files

Solution 3

Just install ZebraDesigner, create a blank label, insert a image object to the template and add the required logo image.

Print to File this label (a *.prn file) and open the recently created file with Notepad++ (MS Notepad will ruin the data if opened and saved with). Find a huge string of seemingly random characters, and there is your image's data. Careful not to lose any of those characters, including the control ones, as the whole string is a textual representation of your image (as it would be if it were base64).

Tip0: Always have your ZPLII Programmer's Guide at hand, you'll need/want to check if ZebraDesigner sent the image to memory or directly to the printer buffer.

Tip1: Before adding the logo to the label and get the text, prepare the image making it greyscale (remember to check the printer's dithering configuration!) or, in my case, plain black and white (best result IMHO). The image can be colored, the ZebraDesigner will make it work for the printer converting the image to greyscale before conversion to commands and text.

Solution 4

I created a PHP script to convert PNG images to .GRF similar to Josh Mayberry's image2grf.py: https://gist.github.com/thomascube/9651d6fa916124a9c52cb0d4262f2c3f

It uses PHP's GD image function and therefore can work with all the file formats GD can open. With small modifications, the Imagick extension could be used but performance seems to be better with GD.

Share:
55,859
Léa Massiot
Author by

Léa Massiot

Updated on July 30, 2022

Comments

  • Léa Massiot
    Léa Massiot almost 2 years

    I would like send ZPL instructions to a Zebra printer (GK420t for now). I'm printing 50mm x 20mm labels. I would like a logo (small ~ 5mm x 5mm image) to be printed on the upper left corner of the label.

    I would like to know the steps I should follow to do this.

    I have been reading and trying a few things from the ZPL manual but I don't really understand how it works and couldn't find a working example.

    It looks like I have to "load" the image into the printer first (in a so-called "storage area"/DRAM?) and then print it.

    The .GRF file extension is mentioned many times in the manual. I couldn't find the tool to convert a .PNG or .BMP image into a .GRF file. I read that a .GRF file is an ASCII HEX representation of a graphic image... but it didn't help me do the work.

    I could print the logo on the labels using the "Zebra Setup Utilities", by "Downloading Fonts and Graphics", choosing any available .MMF file, adding a .BMP picture, downloading it [to the printer] and printing a test page. But until now, I couldn't do it using ZPL instructions.

    I am also wondering what are the best dimensions I should use given the fact that I need a small image ~5mm x 5mm to be printed on the labels. The image I printed is a 40px x 40px image. Also, if I have to make a .GRF file from an original image what should be the type of this file (.BMP, .PNG, .JPG)?

    Can you advise me how to proceed?