فهرست منبع

TODO: Global fixed to pass around completed, also change save_image() to output_image completed.

david 5 سال پیش
والد
کامیت
4f6bf01028
3فایلهای تغییر یافته به همراه31 افزوده شده و 37 حذف شده
  1. 2 2
      TODO.md
  2. 29 35
      dataLoad.py
  3. 0 0
      test.js

+ 2 - 2
TODO.md

@@ -8,6 +8,6 @@
 ## To Do:
 
 * Document Functions (COMPLETE)
-* Change from `global` to pass around
-* Change `save_image()` to `output_image`
+* Change from `global` to pass around (COMPLETE)
+* Change `save_image()` to `output_image()` (COMPLETE)
 * Move all the image stuff over to it's own

+ 29 - 35
dataLoad.py

@@ -64,26 +64,26 @@ def download(howhard, index):
     if(r.status_code == 200):
         with open(f'in/{howhard}_{index}.png', 'wb') as f:
             f.write(r.content)
-    # We did not get a 200 Okay, log this... Hmm maybe we need to make a log file?
-    print( f'{howhard}_{index}.png ' + str(r.status_code) )
+    else:
+        # We did not get a 200 Okay, log this... Hmm maybe we need to make a log file?
+        #print( f'{howhard}_{index}.png ' + str(r.status_code) )
+        raise ConnectionError('http://s0urce.io/client/img/word/{0}/{1} returned status_code {2}'.format(howhard, index, r.status_code))
 
-def img_point(x, y):
-    global pix
+def img_point(pix, x, y):
     """
     img_point, returns a pixel of an image,
     given the x and y on the image.
     """
     return pix[x, y]
 
-def img_avg(x, y):
-    global im, pal
+def img_avg(im, pal, pix, x, y):
     """
     img_avg, returns the average brightness 0-255,
     given the x and y on the image calls img_point,
     to get the individual rgb values to calculate,
     brightness.
     """
-    rgb = img_point(x,y)
+    rgb = img_point(pix, x,y)
     if(im.mode == 'P'):
         rgb = pal[rgb*3:(rgb+1)*3]
 
@@ -92,7 +92,7 @@ def img_avg(x, y):
 
     return int( ( rgb[0] + rgb[1] + rgb[2] ) / 3 )
 
-def is_set(x, y):
+def is_set(im, pal, pix, x, y):
     global INTENSITY
     """
     is_set, returns True or False of calculating,
@@ -105,11 +105,10 @@ def is_set(x, y):
     False means the brightness at the given x and y,
     is Greater Than which means its bright.
     """
-    avg = img_avg(x,y)
+    avg = img_avg(im, pal, pix, x,y)
     return (avg < INTENSITY)
 
-def scan_img():
-    global sx, sy, ex, ey, total
+def scan_img(im, pal, pix, size):
     """
     scan_img, looks at a image and looks for dark pixels,
     if it is a dark pixel record the number and resize the,
@@ -125,7 +124,7 @@ def scan_img():
 
     for y in range( 0,size[1] ):
         for x in range( 0,size[0] ):
-            pnt_is = is_set(x,y)
+            pnt_is = is_set(im, pal, pix, x,y)
             if (pnt_is):
                 total += 1
                 if x < sx:
@@ -152,20 +151,22 @@ def scan_img():
     #print (sx,ex,sy,ey)
     return(sx,sy,ex,ey,total)
 
-def save_image():
+def output_image(im, pal, pix, size):
     """
     For the size of the area we have reduced down to where the majority of dark pixels,
     are located, store all that into a list and return the list.
     
-    given start x, y and end x, y.
+    given image, palette, pixel for function passing.
     returns multiple strings in a list that are edited to use characters to represent,
     the dark and light pixels of the image.
     """
     result = []
+    ex = size[0]; sx = 0
+    ey = size[1]; sy = 0
     for y in range(sy,ey):
         s = ''
         for x in range(sx,ex):
-            if is_set(x,y):
+            if is_set(im, pal, pix, x,y):
                 s += ON
             else:
                 s += OFF
@@ -174,26 +175,21 @@ def save_image():
     return result
 
 def run(difficult):
-    global size, pix, im
     """
     run, represents a single execution of components to the image, (Actuall we do it 1 category at a time instead of just 1 single execution )
     those components do the following...                           (Each category has around 70 items so we standardize on 70, but           )
                                                                    (not all of the categories have 70 and thus we print a File does not exist)
     We open and load the image, and get it's size,
     then we scan_img for dark and light pixels, <-- This narrows the image down to just the majority of dark pixels
-    then from that we output the image line by line onto the screen after it has been save_image d into list form,
+    then from that we output the image line by line onto the screen after it has been output_image d into list form,
     Where we ask the user what the word is, and after that we save all that to a file in the data directory.
     """
     
     for x in range(0, 70):
         fname = f'in/{difficult}_{x}.png'
-
-# if os.path.exists() ...   :(
-        try:
-            with open(fname, 'r') as f:
-                f = f
-        except FileNotFoundError:
-            print("File does not exist")
+    
+        if not os.path.exists(fname):
+            print("Could not find '{0}'".format(fname))
             continue
 
         print(f"Loading: {fname}")
@@ -202,8 +198,7 @@ def run(difficult):
         size = im.size
         print(f"Size: {size[0]} x {size[1]}")
 
-        if(im.mode == 'P'):
-            pal = im.getpalette()
+        pal = im.getpalette()
 
         sx = 0
         ex = size[0]
@@ -211,17 +206,16 @@ def run(difficult):
         ey = size[1]
         total = 0
 
-        scan_img()
+        sx, sy, ex, ey, total = scan_img(im, pal, pix, size)
 
         print(f"Chars within ({sx}, {sy}) - ({ex}, {ey}) total {total} pixels")
 
-        img_s = save_image()
+        img_s = output_image(im, pal, pix, size)
         for l in img_s:
             print(l)
-        img_s.append(input('Word: '))
+        word = input('Word: ')
         with open(f'{DIR}/{difficult}_{x}.txt', 'w') as f:
-            for i_s in img_s:
-                f.write(f'{i_s}\n')
+            f.write('{0}\n'.format(word))
 
         print(f"Image saved to '{DIR}/{difficult}_{x}.txt' in byte string")
         # os.remove(f'{fname}')  # Grr No bad bean, keep file for error checking
@@ -233,19 +227,19 @@ if (DOWNLOAD == True):
     print("EASY")
     # time.sleep(5)
     for e in range(0, 62):
-        print(download('e', e))
+        download('e', e)
         # time.sleep(random.randint(10, 15))
 
     print("MEDIUM")
     # time.sleep(5)
     for m in range(0, 66):
-        print(download('m', m))
+        download('m', m)
         # time.sleep(random.randint(10, 15))
 
     print("HARD")
     # time.sleep(5)
     for h in range(0, 55):
-        print(download('h', h))
+        download('h', h)
         # time.sleep(random.randint(10, 15))
 
     # Img Processing
@@ -256,7 +250,7 @@ if (DOWNLOAD == True):
 # ----------------------------------------------------------------------------------------
 # All below was in a seperate dataJS.py file... but now I have fixed it so it's 1 script!
 # Do we really need to worry about all this right now? (I think we have enough bugs to begin with.)
-
+JSONME = 'false' # Do not execute
 if (JSONME.lower() != 'false'):
     print("Now exporting to JSON")
     print(f"Targeting file: '{JSONME}'")

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
test.js


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است