Skip to content

Commit

Permalink
Merge pull request #59 from imsuraj675/added-barcode
Browse files Browse the repository at this point in the history
Added Barcode image generation program
  • Loading branch information
Avdhesh-Varshney committed Jun 30, 2024
2 parents 654c776 + ea0c970 commit c88847e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
Binary file modified requirements.txt
Binary file not shown.
1 change: 1 addition & 0 deletions src/apps/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def dashboard():
pandas
pillow
pyperclip
python-barcode
pyttsx3
requests
setuptools
Expand Down
79 changes: 79 additions & 0 deletions src/apps/pages/programs/ImageGenerators/barcode_maker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from barcode.writer import ImageWriter
from barcode import EAN13, EAN8, UPCA
import streamlit as st
import base64

# Set barcode type
BARCODE_TYPE = {"EAN-13": [13, EAN13], "EAN-8": [8, EAN8], "UPCA": [12, UPCA]}


def gen_barcode():
box = st.container() # To keep everything inside one container
with box:
option = st.radio(
"Select type of Barcode", ["EAN-13", "EAN-8", "UPCA"], horizontal=True
)
num = st.text_input(
"Enter barcode number",
value="",
max_chars=BARCODE_TYPE[option][0],
placeholder=f"Enter {BARCODE_TYPE[option][0]} digits long barcode number",
)
button_div = st.empty() # So that when Generate Barcode is pressed, it will be replaced by Reset button

with button_div:
if st.button("Generate barcode"):
generate(num, box, option)
st.button("Reset barcode") # Resets everything


def generate(num, box, option):
with box:
if len(num) != BARCODE_TYPE[option][0] or not num.isnumeric():
st.warning(
f"Please enter a valid {option} barcode of {BARCODE_TYPE[option][0]} digits!!"
)
else:
# Included ImageWriter to save the image in png format
image_path = "assets/barcode"

my_code = BARCODE_TYPE[option][1](num, writer=ImageWriter())
my_code.save(image_path)

with open(f"{image_path}.png", "rb") as file:
image_data = file.read()

encoded_image = base64.b64encode(image_data).decode()

# For custom styling of image and button
st.markdown(
f"""
<style>
.button-container {{
display: flex;
justify-content: space-around; /* Adjust as needed: space-between, space-around, etc. */
}}
.styled-button {{
background-color: #ff4b4b; /* Default button background color */
color: white; /* Default button text color */
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s, color 0.3s; /* Transition effect for color change */
}}
.styled-button:hover {{
background-color: #ff3333; /* Button background color on hover */
color: white; /* Button text color on hover */
}}
</style>
<div style="display: flex; flex-direction: column; align-items: center;">
<img src="data:image/png;base64,{encoded_image}" alt="Your Image">
<br>
<a href="data:image/png;base64,{encoded_image}" download="barcode.png">
<button class="styled-button">Download Image</button>
</a>
</div>
""",
unsafe_allow_html=True,
)
5 changes: 4 additions & 1 deletion src/apps/pages/programs/imageProgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

def imagePrograms():
st.title('Image Generator Programs')
choice = st.selectbox('Select a program to execute', [None])
choice = st.selectbox('Select a program to execute', [None, "Barcode Generator"])

st.markdown('---')
if choice == "Barcode Generator":
from src.apps.pages.programs.ImageGenerators.barcode_maker import gen_barcode
gen_barcode()

imagePrograms()

0 comments on commit c88847e

Please sign in to comment.