0

Background: I'm trying to make an FNSKU label for Amazon using python, I'm manipulating the images too much so I don't have an option to use the labels from seller central, I need to create them somehow.

Problem: the labels Amazon gives are nice and small: https://i.stack.imgur.com/hlgPA.jpg

The labels I generate are too big: https://i.stack.imgur.com/8ME1X.png

I've tried resizing the image and using barcode fonts but it all ends up not scanning, how can I code barcode that will look like the first example?

These are the modules I tried:

reportlab:

from reportlab.pdfgen import canvas 
from reportlab.graphics.barcode import code128 
from reportlab.lib.units import mm 
c = canvas.Canvas("BRC.pdf") 
c.setPageSize(57*mm, 32*mm) barcode = code128.Code128("X001SB7OYL", 
barHeight=.9*inch,barWidth = 1.2) 
barcode.drawOn(c, 2*mm, 20*mm) c.showPage() c.save()

-- Resizing the code here to be smaller didn't work, the code was not readable.

I also used python-barcode with a code that looks like this:

code128(u'X001SB7OYL', writer=ImageWriter())

Same problem again, once I resize it it does not get scanned.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
MarinaF
  • 35
  • 1
  • 2
  • 13
  • can you please give your code? – hypadr1v3 Aug 13 '18 at 11:20
  • I used 2 different modules to try and create the basic form of the barcode: from reportlab.pdfgen import canvas from reportlab.graphics.barcode import code128 from reportlab.lib.units import mm c = canvas.Canvas("BRC.pdf") c.setPageSize(57*mm, 32*mm) barcode = code128.Code128("X001SB7OYL", barHeight=.9*inch,barWidth = 1.2) barcode.drawOn(c, 2*mm, 20*mm) c.showPage() c.save() -- Resizing the code here to be smaller didn't work, the code was not readable. I also used python-barcode with a code that looks like this: code128(u'X001SB7OYL', writer=ImageWriter()) – MarinaF Aug 13 '18 at 11:25
  • which modules are you using and could you please edit your post and add the code? – hypadr1v3 Aug 13 '18 at 11:28
  • Once resize the images created by either module (reportlab or python-barcode) it is no longer readable, the barcode fonts I found were also not readable to begin with. – MarinaF Aug 13 '18 at 11:30
  • PLEASE edit your post with the code as the code in the comment is impossible to read – hypadr1v3 Aug 13 '18 at 11:33
  • Just edited the original post. – MarinaF Aug 13 '18 at 11:40
  • umm... you just deleted the links to the pictures with it. – hypadr1v3 Aug 13 '18 at 11:42
  • ok, should be good now. Thanks for the help! – MarinaF Aug 13 '18 at 11:55

3 Answers3

0

If I understand the problem, this is the solution. This code cuts horizontal sections of the image.

from PIL import Image
from numpy import array, delete
from copy import deepcopy


def img_to_mat(img):
    return array(img)


def mat_to_img(mat, encoding='RGB'):
    """
    input: zeros((h, w, 3), dtype=uint8)
    return: img
    """
    return Image.fromarray(mat, encoding)


def cut(img, h1, h2):
    """ cut lines from h1 to h2 of img """
    mat = deepcopy(img_to_mat(img))

    for i in range(h2 - h1):
        mat = delete(mat, h1, axis=0)

    return mat_to_img(mat)


# Load image
path = 'gallery\\bar2.png'
Im = Image.open(path)

# cut from up to down
Im = cut(Im, 200, 240)
Im = cut(Im, 50, 170)

Im.save(path.split('.')[0] + '-new.' + path.split('.')[1])
Im.show()

the example values are tuned for your particular example image.

Omar Cusma Fait
  • 313
  • 1
  • 11
  • I need more than cutting the image since the width needs to be resized as well, once I start changing the width the image becomes unreadable. – MarinaF Aug 13 '18 at 12:47
  • Ok now I get it, then the problem must be that not all the bars are resized evenly, right? – Omar Cusma Fait Aug 13 '18 at 13:01
0

There are two problems with your generated version of the barcode:

  • its proportions are different from the desired shape - you can correct that by choosing a smaller bar height (and maybe changing the height of the drawable). I don't know what affects the distance between the barcode and the text below it, it is also too large.

  • it is overall wider than the desired one. You say if you make it smaller by changing parameters, it becomes not scannable. Could you post the result with a drawable width of about 37mm? If the problem is with imprecise placement of the lines due to the limited resolution, you could work around this by rendering the image large initially (but proprotionate, so it isn't too tall and the text is close to the barcode) and then using a library that has a decent anti-aliased scale-down function (e.g., ImageMagick) to scale it to the exact size you want.

Leo K
  • 5,189
  • 3
  • 12
  • 27
0

You can remove sections of your original Image like Omar had stated.

A much simpler way to do this would be:

file = 'barcode.png' # path to saved barcode

img = Image.open(file)

img1 = img.crop((0, 0, 402, 50)) # crop image into the 3 required sections
img2 = img.crop((0, 170, 402, 200))
img3 = img.crop((0, 240, 402, 270))

image = Image.new('RGB', (402, 110))

image.paste(img1, (0, 0, 402, 50)) # stich the cropped images together
image.paste(img2, (0, 50, 402, 80))
image.paste(img3, (0, 80, 402, 110))

image.thumbnail((222, 222), Image.NEAREST)
image.save(file)
weakit
  • 216
  • 3
  • 7
  • It only makes the image shorter but there is still a problem with the width, changing the height of the image doesn't matter and it will still scan - but adjusting he width makes it unreadable. – MarinaF Aug 13 '18 at 12:46
  • What's the width you need the Image to be? – weakit Aug 13 '18 at 12:53
  • It should be the width of the first smaller picture, bu e problem is that sizing down the width doesn't work, the image won't scan. – MarinaF Aug 13 '18 at 13:19
  • Check now if the barcode can be read. The text below the barcode will be unreadable but the barcode should scan just fine. – weakit Aug 14 '18 at 15:09