RedBottle13 commited on
Commit
87951a7
·
1 Parent(s): c84a261

update scaling

Browse files
Files changed (2) hide show
  1. app.py +30 -5
  2. images/dog.jpg +0 -0
app.py CHANGED
@@ -15,7 +15,10 @@ CHAR_LEN = len(CHAR_ARRAY)
15
  GRAYSCALE_LEVEL = CHAR_LEN / 256
16
 
17
  # Scaling factor to resize the image
18
- SCALE = 0.15
 
 
 
19
 
20
  # Character dimensions (width and height in pixels) used to match image aspect ratio to character aspect ratio
21
  CHAR_W = 6
@@ -30,11 +33,33 @@ def getChar(inputInt, gamma=1.8):
30
  return CHAR_ARRAY[math.floor(inputInt * GRAYSCALE_LEVEL)]
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def load_and_preprocess_image(image):
34
  """Resize and preprocess the input image, adjusting contrast and blurring for better ASCII conversion."""
35
  width, height = image.size
36
- # Resize image, adjusting aspect ratio to fit ASCII character dimensions
37
- im = image.resize((int(SCALE * width), int(SCALE * height * (CHAR_W / CHAR_H))))
 
 
 
 
 
 
 
 
38
 
39
  # Enhance contrast to bring out more detail in the ASCII representation
40
  im = ImageOps.equalize(im, mask=None)
@@ -127,7 +152,7 @@ demo = gr.Interface(
127
  examples=[
128
  ['images/building.jpg'],
129
  ['images/cat.webp'],
130
- ['images/mountain.webp'],
131
  ['images/people.jpg'],
132
  ['images/Northeastern_seal.png'],
133
  ['images/einstein.jpg'],
@@ -135,4 +160,4 @@ demo = gr.Interface(
135
 
136
  )
137
 
138
- demo.launch(share=True)
 
15
  GRAYSCALE_LEVEL = CHAR_LEN / 256
16
 
17
  # Scaling factor to resize the image
18
+ SCALE = 0.2
19
+
20
+ # Target number of characters per row
21
+ TARGET_WIDTH = 200
22
 
23
  # Character dimensions (width and height in pixels) used to match image aspect ratio to character aspect ratio
24
  CHAR_W = 6
 
33
  return CHAR_ARRAY[math.floor(inputInt * GRAYSCALE_LEVEL)]
34
 
35
 
36
+ # def load_and_preprocess_image(image):
37
+ # """Resize and preprocess the input image, adjusting contrast and blurring for better ASCII conversion."""
38
+ # width, height = image.size
39
+ # # Resize image, adjusting aspect ratio to fit ASCII character dimensions
40
+ # im = image.resize((int(SCALE * width), int(SCALE * height * (CHAR_W / CHAR_H))))
41
+
42
+ # # Enhance contrast to bring out more detail in the ASCII representation
43
+ # im = ImageOps.equalize(im, mask=None)
44
+
45
+ # # Apply a slight blur to reduce noise and simplify pixel values
46
+ # im = im.filter(ImageFilter.GaussianBlur(radius=0.5))
47
+
48
+ # return im
49
+
50
  def load_and_preprocess_image(image):
51
  """Resize and preprocess the input image, adjusting contrast and blurring for better ASCII conversion."""
52
  width, height = image.size
53
+
54
+ # Calculate the scaling factor to match the target character width (300 characters per row)
55
+ scale_factor = TARGET_WIDTH / width
56
+
57
+ # Resize the image, maintaining the aspect ratio considering the character dimensions
58
+ new_width = TARGET_WIDTH
59
+ new_height = int(scale_factor * height * (CHAR_W / CHAR_H)) # Adjust height to maintain aspect ratio
60
+
61
+ # Resize the image based on the calculated dimensions
62
+ im = image.resize((new_width, new_height))
63
 
64
  # Enhance contrast to bring out more detail in the ASCII representation
65
  im = ImageOps.equalize(im, mask=None)
 
152
  examples=[
153
  ['images/building.jpg'],
154
  ['images/cat.webp'],
155
+ ['images/dog.jpg'],
156
  ['images/people.jpg'],
157
  ['images/Northeastern_seal.png'],
158
  ['images/einstein.jpg'],
 
160
 
161
  )
162
 
163
+ demo.launch()
images/dog.jpg ADDED