Search code examples
pythonbarcode

Rotate image with python-barcode ImageWriter in bytesIO stream


I want to rotate the image I have saved in bytesIO with python-barcode ImageWriter but its optional fields do not have this option

img = barcode.get_barcode(barcode_type, barcode_text, writer=ImageWriter()) 

fp = io.BytesIO()
img.write(fp, options={'module_width':  0.1, 'module_height': 1, 'font_size': 4, 'text_distance': 0.5,
                       'quiet_zone': 1, 'write_text': True})

Solution

  • I handled it with the pillow package, first saved the created image in memory and then rotated it using the pil.

        from PIL import Image
        pil_img = Image.open(fp)
        pil_img = pil_img.rotate(90, expand=True)
        fp = io.BytesIO()
        pil_img.save(fp, format='JPEG')