how to set height and width if writing to file like object? #91

Open
opened 2020-10-01 12:28:06 +02:00 by israelcoper · 2 comments
israelcoper commented 2020-10-01 12:28:06 +02:00 (Migrated from github.com)

Here's my code:

def barcode_to_html(order_id, box_num):

    from barcode import Code128
    from barcode.writer import ImageWriter
    from io import BytesIO
    import base64

    file_like_object = BytesIO()

    writer = ImageWriter()
    writer.set_options({ 'module_height': 10.0 })  ## NOT WORKING

    value = f'{order_id}-{box_num}'

    Code128(value, writer=writer).write(file_like_object)

    encoded = base64.b64encode(file_like_object.getvalue()).decode("ascii")

    return f'<img src="/?originalUrl=https%3A%2F%2Fcodeberg.org%2F%26%2334%3Bdata%3Aimage%2Fpng%3Bbase64%2C%7Bencoded%7D%26%2334%3B%2520%2520%2F%26gt%3B%26%2339%3B%253C%2Fcode">
Here's my code: ``` def barcode_to_html(order_id, box_num): from barcode import Code128 from barcode.writer import ImageWriter from io import BytesIO import base64 file_like_object = BytesIO() writer = ImageWriter() writer.set_options({ 'module_height': 10.0 }) ## NOT WORKING value = f'{order_id}-{box_num}' Code128(value, writer=writer).write(file_like_object) encoded = base64.b64encode(file_like_object.getvalue()).decode("ascii") return f'<img src="/?originalUrl=https%3A%2F%2Fcodeberg.org%2F%26%2334%3Bdata%3Aimage%2Fpng%3Bbase64%2C%7Bencoded%7D%26%2334%3B%2520%2520%2F%26gt%3B%26%2339%3B%2560%2560%2560%253C%2Fdiv">
jhummer commented 2020-12-04 08:57:34 +01:00 (Migrated from github.com)

I had this issue aswell, its not totally clear in the documentation but you can pass the options to .write() instead:

Code128(value, writer=writer).write(file_like_object, options={'<option>': <value>})

I had this issue aswell, its not totally clear in the documentation but you can pass the options to .write() instead: `Code128(value, writer=writer).write(file_like_object, options={'<option>': <value>})`
avmakesh commented 2022-02-02 11:15:32 +01:00 (Migrated from github.com)

Use the options this way ,

     from barcode import ITF
     from io import BytesIO
     stream = BytesIO()
     options = {
             "module_width": 0.1,
             "module_height": 2,
             "quiet_zone": 0.1,
             "write_text": False,
             "text_distance": 2,
     }
     Code128(code_str, writer=writer1, narrow=1, wide=3).write(stream, options)
Use the options this way , from barcode import ITF from io import BytesIO stream = BytesIO() options = { "module_width": 0.1, "module_height": 2, "quiet_zone": 0.1, "write_text": False, "text_distance": 2, } Code128(code_str, writer=writer1, narrow=1, wide=3).write(stream, options)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
WhyNotHugo/python-barcode#91
No description provided.