Micheal
Micheal

Reputation: 21

How to generate barcode in python as response of image object

Views.py:

def Bar(request):
    payload = json.loads(request.body.decode('utf-8'))
    a=payload["donor_n_key"]
    ean = barcode.get('code128', a, writer=ImageWriter())
    filename = ean.save('ean13')
    image = ean.render()
    return HttpResponse(image,content_type="image/png")

Here i have downloaded the barcode image but I am unable to open that image.I am getting the error is windows photo viewer can't open this picture because either photo viewer does not support this file format.

I am new to this django restframework.Please help me Anyone.

Upvotes: 0

Views: 2272

Answers (1)

Antu
Antu

Reputation: 2303

In Your View.py

Import This

import barcode
from barcode.writer import ImageWriter


def Bar(request): 
    lineCode = '1234567891234'
    barCodeImage = barcode.get('ean13', lineCode, writer=ImageWriter())
    filename = barCodeImage.save(item_name.png)
    return HttpResponse(image,content_type="image/png")

It Will Generates Image and save it your project root folder

More help visit this link PyBarcode

Upvotes: 1

Related Questions