jk commited on
Commit
2f49e43
1 Parent(s): e0cd5b1

Emdedded in Gradio

Browse files
__pycache__/combine_bitmaps.cpython-311.pyc ADDED
Binary file (786 Bytes). View file
 
__pycache__/generate_text.cpython-311.pyc ADDED
Binary file (2.26 kB). View file
 
__pycache__/simulator.cpython-311.pyc ADDED
Binary file (3.42 kB). View file
 
app.py CHANGED
@@ -1,12 +1,36 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name, intensity):
4
- return "Hello " * intensity + name + "!"
5
 
6
  demo = gr.Interface(
7
- fn=greet,
8
- inputs=["text", "slider"],
9
- outputs=["text"],
10
  )
11
 
12
- demo.launch()
 
 
1
  import gradio as gr
2
+ from generate_text import generate_text_bitmap
3
+ from combine_bitmaps import combine_bitmaps
4
+ from simulator import simulate_bitmap
5
+ from PIL import Image
6
+
7
+
8
+ def get_path_from_object(files):
9
+ file_names = [f.name for f in files]
10
+ print(file_names)
11
+ return file_names[0]
12
+
13
+
14
+ def create_bitmap(linien_bitmap_object, text):
15
+ # generates bitmap from given text and saves it to tmp/font.bmp
16
+ generate_text_bitmap(text)
17
+
18
+ text_image = Image.open("tmp/font.bmp")
19
+ linien_image = Image.open(get_path_from_object(linien_bitmap_object))
20
+ combine_bitmaps(linien_image, text_image)
21
+
22
+ combined_bitmap = Image.open("tmp/combined_bitmap.bmp")
23
+
24
+ # adds pixels in betweeen and saves result to tmp/simulated_bitmap.bmp
25
+ simulate_bitmap(combined_bitmap)
26
+ return "tmp/simulated_bitmap.bmp"
27
 
 
 
28
 
29
  demo = gr.Interface(
30
+ fn=create_bitmap,
31
+ inputs=["files", "text"],
32
+ outputs=["file"],
33
  )
34
 
35
+ if __name__ == "__main__":
36
+ demo.launch(inbrowser=True)
combine_bitmaps.py CHANGED
@@ -1,18 +1,20 @@
1
  from PIL import Image
2
 
3
 
4
- def combine_bitmaps_horizontally(im1, im2):
5
- dst = Image.new('RGB', (im1.width + im2.width, im1.height))
6
  dst.paste(im1, (0, 0))
7
  dst.paste(im2, (im1.width, 0))
8
- return dst
9
 
10
 
 
11
  # Open the original .bmp file
12
  linien_image = Image.open("assets/21.bmp")
13
  text_image = Image.open("output/font_output.bmp")
14
 
15
  # Combine the two images
16
- combined_bitmap = combine_bitmaps_horizontally(linien_image, text_image)
17
 
18
  combined_bitmap.save("output/combined_output.bmp")
 
 
1
  from PIL import Image
2
 
3
 
4
+ def combine_bitmaps(im1, im2):
5
+ dst = Image.new("RGB", (im1.width + im2.width, im1.height))
6
  dst.paste(im1, (0, 0))
7
  dst.paste(im2, (im1.width, 0))
8
+ dst.save("tmp/combined_bitmap.bmp")
9
 
10
 
11
+ """
12
  # Open the original .bmp file
13
  linien_image = Image.open("assets/21.bmp")
14
  text_image = Image.open("output/font_output.bmp")
15
 
16
  # Combine the two images
17
+ combined_bitmap = combine_bitmaps(linien_image, text_image)
18
 
19
  combined_bitmap.save("output/combined_output.bmp")
20
+ """
generate_text.py CHANGED
@@ -41,3 +41,13 @@ draw = ImageDraw.Draw(new_image)
41
  draw_text(new_image, "Max-Weber-Platz", font, (255, 255, 255))
42
 
43
  new_image.save("output/font_output.bmp")
 
 
 
 
 
 
 
 
 
 
 
41
  draw_text(new_image, "Max-Weber-Platz", font, (255, 255, 255))
42
 
43
  new_image.save("output/font_output.bmp")
44
+
45
+
46
+ def generate_text_bitmap(text, width=128, height=24):
47
+ # Create new image
48
+ new_image = Image.new("RGB", (width, height))
49
+
50
+ # Draw text
51
+ draw_text(new_image, text, font, (255, 255, 255))
52
+
53
+ new_image.save("./tmp/font.bmp")
simulator.py CHANGED
@@ -1,20 +1,7 @@
1
  from PIL import Image
2
 
3
- # Open the original .bmp file
4
- image = Image.open("output/combined_output.bmp")
5
-
6
- width, height = image.size
7
-
8
- list_of_pixels = list(image.getdata())
9
-
10
 
11
- def get_pixel(x, y):
12
- if x < 0 or x >= width or y < 0 or y >= height:
13
- return print("Error: Pixel out of range")
14
- return list_of_pixels[x + y * width]
15
-
16
-
17
- def get_coordinates_from_index(index):
18
  if index < 0 or index >= width * height:
19
  return print("Error: Index out of range")
20
  x = index % width
@@ -22,12 +9,6 @@ def get_coordinates_from_index(index):
22
  return x, y
23
 
24
 
25
- def set_pixel(x, y, c):
26
- if x < 0 or x >= width or y < 0 or y >= height:
27
- return print("Error: Pixel out of range")
28
- list_of_pixels[x + y * width] = c
29
-
30
-
31
  def get_new_coordinates(coordinates):
32
  x, y = coordinates
33
  # if x=0 then new_x= 2, if index=1 then new_x= 7, if index=2 then new_x= 12, if index=3 then new_x= 2, if index=4 then new_x= 7, if index=5 then new_x= 12
@@ -47,25 +28,56 @@ def increase_rgb_value(color):
47
  return r, g, b
48
 
49
 
50
- def draw_new_pixel(coordinates, color):
51
  x, y = coordinates
52
  color = increase_rgb_value(color)
53
- new_image.putpixel(coordinates, color)
54
- new_image.putpixel((x + 1, y), color)
55
- new_image.putpixel((x + 2, y), color)
56
- new_image.putpixel((x - 1, y), color)
57
- new_image.putpixel((x - 2, y), color)
 
 
 
 
 
58
 
59
- new_image.putpixel((x, y + 1), color)
60
- new_image.putpixel((x, y + 2), color)
61
- new_image.putpixel((x, y - 1), color)
62
- new_image.putpixel((x, y - 2), color)
63
 
64
- new_image.putpixel((x + 1, y + 1), color)
65
- new_image.putpixel((x - 1, y - 1), color)
66
- new_image.putpixel((x + 1, y - 1), color)
67
- new_image.putpixel((x - 1, y + 1), color)
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  print(list_of_pixels)
71
 
@@ -81,4 +93,4 @@ new_image = Image.new("RGB", (new_width, new_height))
81
  for index, color in enumerate(list_of_pixels):
82
  draw_new_pixel(get_new_coordinates(get_coordinates_from_index(index)), color)
83
 
84
- new_image.save("output/output.bmp")
 
1
  from PIL import Image
2
 
 
 
 
 
 
 
 
3
 
4
+ def get_coordinates_from_index(index, width, height):
 
 
 
 
 
 
5
  if index < 0 or index >= width * height:
6
  return print("Error: Index out of range")
7
  x = index % width
 
9
  return x, y
10
 
11
 
 
 
 
 
 
 
12
  def get_new_coordinates(coordinates):
13
  x, y = coordinates
14
  # if x=0 then new_x= 2, if index=1 then new_x= 7, if index=2 then new_x= 12, if index=3 then new_x= 2, if index=4 then new_x= 7, if index=5 then new_x= 12
 
28
  return r, g, b
29
 
30
 
31
+ def draw_new_pixel(image, coordinates, color):
32
  x, y = coordinates
33
  color = increase_rgb_value(color)
34
+ image.putpixel(coordinates, color)
35
+ image.putpixel((x + 1, y), color)
36
+ image.putpixel((x + 2, y), color)
37
+ image.putpixel((x - 1, y), color)
38
+ image.putpixel((x - 2, y), color)
39
+
40
+ image.putpixel((x, y + 1), color)
41
+ image.putpixel((x, y + 2), color)
42
+ image.putpixel((x, y - 1), color)
43
+ image.putpixel((x, y - 2), color)
44
 
45
+ image.putpixel((x + 1, y + 1), color)
46
+ image.putpixel((x - 1, y - 1), color)
47
+ image.putpixel((x + 1, y - 1), color)
48
+ image.putpixel((x - 1, y + 1), color)
49
 
 
 
 
 
50
 
51
+ def simulate_bitmap(image):
52
+ width, height = image.size
53
+
54
+ list_of_pixels = list(image.getdata())
55
+
56
+ # Calculate new Canvas size
57
+ new_width = width * 6
58
+ new_height = height * 6
59
+
60
+ # Create new image
61
+ new_image = Image.new("RGB", (new_width, new_height))
62
+
63
+ width, height = image.size
64
+ list_of_pixels = list(image.getdata())
65
+ for index, color in enumerate(list_of_pixels):
66
+ draw_new_pixel(
67
+ new_image,
68
+ get_new_coordinates(get_coordinates_from_index(index, width, height)),
69
+ color,
70
+ )
71
+ new_image.save("tmp/simulated_bitmap.bmp")
72
+ return new_image
73
+
74
+
75
+ """ # Open the original .bmp file
76
+ image = Image.open("output/combined_output.bmp")
77
+
78
+ width, height = image.size
79
+
80
+ list_of_pixels = list(image.getdata())
81
 
82
  print(list_of_pixels)
83
 
 
93
  for index, color in enumerate(list_of_pixels):
94
  draw_new_pixel(get_new_coordinates(get_coordinates_from_index(index)), color)
95
 
96
+ new_image.save("output/output.bmp") """
test.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def process_files(files):
4
+ file_names = [f.name for f in files]
5
+ return ",\n".join(file_names)
6
+
7
+ demo = gr.Interface(
8
+ process_files,
9
+ inputs='files',
10
+ outputs="textbox"
11
+ )
12
+
13
+ demo.launch()
tmp/combined_bitmap.bmp ADDED
tmp/font.bmp ADDED
tmp/simulated_bitmap.bmp ADDED