Skip to content

Commit 076be81

Browse files
authored
Add files via upload
1 parent 13a2880 commit 076be81

3 files changed

Lines changed: 55 additions & 40 deletions

File tree

examples/upc-pygame.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# Constants
1515
NUM_BARCODES = 12
1616
BARCODE_SIZE = 1
17-
WIDTH, HEIGHT = pygame.display.Info().current_w, pygame.display.Info().current_h
1817

1918
# Initialize Pygame and Display
2019
pygame.init()
20+
WIDTH, HEIGHT = pygame.display.Info().current_w, pygame.display.Info().current_h
2121
screen = pygame.display.set_mode((WIDTH, HEIGHT))
2222
pygame.display.set_caption("PyUPC-EAN Demo - {}".format(upcean.__version__))
2323
pygame.display.toggle_fullscreen()
@@ -36,19 +36,21 @@
3636

3737
# Barcode and Position Initialization Functions
3838
def create_barcode():
39-
barcode = upcean.oopfuncs.barcode()
40-
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
41-
barcode.type = barcode_type
42-
43-
# Assign code based on type
44-
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
45-
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
39+
while(True):
40+
barcode = upcean.oopfuncs.barcode()
41+
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
42+
barcode.type = barcode_type
43+
# Assign code based on type
44+
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
45+
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
46+
if(barcode.validate_checksum()):
47+
break;
4648
barcode.code = barcode.fix_checksum()
4749
return barcode
4850

4951
def generate_barcode_image(barcode):
5052
barcode.size = BARCODE_SIZE
51-
barcode_img = barcode.validate_draw_barcode().convert("RGBA")
53+
barcode_img = barcode.validate_draw_barcode()[1].convert("RGBA")
5254
barcode_img = barcode_img.rotate(random.randint(0, 360), Image.BICUBIC, True)
5355
return pygame.image.fromstring(barcode_img.tobytes(), barcode_img.size, barcode_img.mode)
5456

examples/upc-pygame2.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@
3535

3636
# Barcode and Position Initialization Functions
3737
def create_barcode():
38-
barcode = upcean.oopfuncs.barcode()
39-
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
40-
barcode.type = barcode_type
41-
42-
# Assign code based on type
43-
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
44-
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
38+
while(True):
39+
barcode = upcean.oopfuncs.barcode()
40+
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
41+
barcode.type = barcode_type
42+
# Assign code based on type
43+
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
44+
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
45+
if(barcode.validate_checksum()):
46+
break;
4547
barcode.code = barcode.fix_checksum()
4648
return barcode
4749

4850
def generate_barcode_image(barcode):
4951
barcode.size = BARCODE_SIZE
50-
barcode_img = barcode.validate_draw_barcode().convert("RGBA")
52+
barcode_img = barcode.validate_draw_barcode()[1].convert("RGBA")
5153
barcode_img = barcode_img.rotate(random.randint(0, 360), Image.BICUBIC, True)
5254
return pygame.image.fromstring(barcode_img.tobytes(), barcode_img.size, barcode_img.mode)
5355

examples/upc-pysdl2.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from PIL import Image
1212
import PIL
1313
import upcean
14+
import ctypes
1415

1516
# Constants
1617
NUM_BARCODES = 12
@@ -35,43 +36,56 @@
3536

3637
# Barcode and Position Initialization Functions
3738
def create_barcode():
38-
barcode = upcean.oopfuncs.barcode()
39-
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
40-
barcode.type = barcode_type
41-
42-
# Assign code based on type
43-
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
44-
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
39+
while(True):
40+
barcode = upcean.oopfuncs.barcode()
41+
barcode_type = random.choice(["upca", "upce", "ean13", "ean8", "itf14"])
42+
barcode.type = barcode_type
43+
# Assign code based on type
44+
code_length = {"upca": 11, "upce": 7, "ean13": 12, "ean8": 7, "itf14": 13}[barcode_type]
45+
barcode.code = str(random.randint(0, 10**code_length - 1)).zfill(code_length)
46+
if(barcode.validate_checksum()):
47+
break;
4548
barcode.code = barcode.fix_checksum()
4649
return barcode
4750

4851
def generate_barcode_image(barcode):
4952
barcode.size = BARCODE_SIZE
50-
barcode_img = barcode.validate_draw_barcode().convert("RGBA")
53+
barcode_img = barcode.validate_draw_barcode()[1].convert("RGBA")
5154
barcode_img = barcode_img.rotate(random.randint(0, 360), Image.BICUBIC, True)
5255
return barcode_img
5356

54-
def convert_pillow_to_sdl2_surface(image):
55-
"""Converts a Pillow Image to an SDL2 Surface"""
56-
image_data = image.tobytes()
57+
def pillow_to_sdl_texture(renderer: sdl2.ext.Renderer, pil_img):
58+
img = pil_img.convert("RGBA")
59+
w, h = img.size
60+
pitch = w * 4
61+
62+
buf = ctypes.create_string_buffer(img.tobytes())
5763
surface = sdl2.SDL_CreateRGBSurfaceWithFormatFrom(
58-
image_data, image.width, image.height, 32, image.width * 4, sdl2.SDL_PIXELFORMAT_RGBA32)
59-
return surface
64+
buf, w, h, 32, pitch, sdl2.SDL_PIXELFORMAT_RGBA32
65+
)
66+
if not surface:
67+
raise RuntimeError(sdl2.SDL_GetError().decode())
68+
69+
# NOTE: Texture wants (renderer, surface)
70+
tex = sdl2.ext.Texture(renderer, surface)
71+
72+
sdl2.SDL_FreeSurface(surface)
73+
return tex
6074

6175
def random_position():
6276
return [random.randint(0, WIDTH), random.randint(0, HEIGHT)]
6377

78+
# Renderer
79+
renderer = sdl2.ext.Renderer(window)
80+
running = True
81+
6482
# Initialize Barcodes, Images, Positions, and Directions
6583
barcodes = [create_barcode() for _ in range(NUM_BARCODES)]
6684
barcode_images = [generate_barcode_image(barcode) for barcode in barcodes]
67-
sdl_surfaces = [convert_pillow_to_sdl2_surface(image) for image in barcode_images]
85+
sdl_textures = [pillow_to_sdl_texture(renderer, image) for image in barcode_images]
6886
positions = [random_position() for _ in range(NUM_BARCODES)]
6987
directions = [(random.choice([-2, -1, 1, 2]), random.choice([-2, -1, 1, 2])) for _ in range(NUM_BARCODES)]
7088

71-
# Renderer
72-
renderer = sdl2.ext.Renderer(window)
73-
running = True
74-
7589
# Animation Loop
7690
while running:
7791
renderer.clear(0) # Clear with black background
@@ -90,11 +104,11 @@ def random_position():
90104
directions[i] = (random.choice([-2, -1, 1, 2]), random.choice([-2, -1, 1, 2]))
91105
barcodes[i] = create_barcode()
92106
barcode_images[i] = generate_barcode_image(barcodes[i])
93-
sdl_surfaces[i] = convert_pillow_to_sdl2_surface(barcode_images[i])
107+
sdl_textures[i] = pillow_to_sdl_texture(renderer, barcode_images[i])
94108

95109
# Convert position and draw on screen
96110
dst_rect = sdl2.SDL_Rect(pos[0], pos[1], barcode_images[i].width, barcode_images[i].height)
97-
renderer.copy(sdl_surfaces[i], None, dst_rect)
111+
renderer.copy(sdl_textures[i], None, dst_rect)
98112

99113
# Present the renderer
100114
renderer.present()
@@ -109,7 +123,4 @@ def random_position():
109123
if event.key.keysym.sym in {sdl2.SDLK_ESCAPE, sdl2.SDLK_q}:
110124
running = False
111125

112-
# Clean up and quit
113-
for surface in sdl_surfaces:
114-
sdl2.SDL_FreeSurface(surface)
115126
sdl2.ext.quit()

0 commit comments

Comments
 (0)