-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar_gen.py
53 lines (44 loc) · 1.6 KB
/
bar_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import math
from barcode import Code128
from barcode.writer import ImageWriter
from PIL import Image
class BarGen():
def __init__(self,codes = []) -> None:
self.codes = codes
self.barcodes = []
self.pages = []
def clear(self):
self.codes = []
self.barcodes = []
self.pages = []
def clear_pages(self):
self.pages = []
def generate(self,start, end, ret = False):
for i in range(abs(end-start)+1):
self.codes.append(str(start+i))
if ret:
return self.codes
def render(self, ret = False):
for code in self.codes:
bc = Code128(str(code), writer=ImageWriter()).render()
bc.resize((512,280))
self.barcodes.append(bc)
if ret:
return self.barcodes
def render_pages(self, raws, colums, auto_render= False, ret = False):
if auto_render:
self.render()
max_in_page = raws*colums
pages_count = math.ceil(len(self.barcodes) /max_in_page)
i = 0
for page in range(pages_count):
dst = Image.new('RGB', (self.barcodes[0].width*colums, self.barcodes[0].height*raws))
for r in range(raws):
for c in range(colums):
if i+1 > len(self.barcodes):
break
dst.paste(self.barcodes[i], (c*self.barcodes[i].width, r*self.barcodes[i].height))
i += 1
self.pages.append(dst)
if ret:
return self.pages