Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More code cleanup & reformatting #33

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 186 additions & 110 deletions gridsplitter/slice.py

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions waznexserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
IMAGE_FOLDER = os.path.join(DATA_FOLDER, 'images')
DOWNSIZED_FOLDER = os.path.join(DATA_FOLDER, 'downsized')
THUMBNAIL_FOLDER = os.path.join(DATA_FOLDER, 'thumbnails')
ALLOWED_EXTENSIONS = {'png', 'PNG',
'jpg', 'JPG',
'jpeg', 'JPEG',
'gif', 'GIF'}
ALLOWED_EXTENSIONS = {'png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF'}
FILE_NAME_DT_FORMAT = '%Y-%m-%dT%H%M%S'
PRETTY_DT_FORMAT = '%m/%d/%Y %I:%M:%S %p'
ENABLE_GRIDSPLITTER = True
GRIDSPLITTER_PYTHON = "/opt/waznexserver/env/bin/python"
GRIDSPLITTER_SLICER = "/opt/waznexserver/WaznexServer/gridsplitter/slice.py"
SPLIT_FOLDER = os.path.join(DATA_FOLDER, 'sliced')
GRIDSPLITTER_CELL_PREFIX = 'out-'
GRIDSPLITTER_COLOR = '2' # 0:red, 1:green or 2:blue
GRIDSPLITTER_COLOR = '2' # 0:red, 1:green or 2:blue
GRIDSPLITTER_CELL_WIDTH = '320'
GRIDSPLITTER_CELL_HEIGHT = '207'
GRIDSPLITTER_MIN_COLS = 2
Expand Down
4 changes: 2 additions & 2 deletions waznexserver/init_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def create_database():
statuses = [s for s in dir(models) if s.startswith('IMAGESTATUS_')]
for status in statuses:
id = getattr(models, status)
s = models.ImageStatus(id, status.split('_',1)[1])
s = models.ImageStatus(id, status.split('_', 1)[1])
db.session.add(s)

# Find and add all of the ImageLevels in models.py
levels = [l for l in dir(models) if l.startswith('IMAGELEVEL_')]
for level in levels:
id = getattr(models, level)
l = models.ImageLevel(id, level.split('_',1)[1])
l = models.ImageLevel(id, level.split('_', 1)[1])
db.session.add(l)

db.session.commit()
Expand Down
46 changes: 20 additions & 26 deletions waznexserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
class Base(DeclarativeBase):
pass


db = SQLAlchemy(model_class=Base)

# Image Levels (basic thumbnails, full grid)
IMAGELEVEL_NOTHING = -1
IMAGELEVEL_BASIC = 0
IMAGELEVEL_GRID = 1

class ImageLevel (db.Model):

class ImageLevel(db.Model):
id = db.Column(db.Integer, primary_key=True)
desc = db.Column(db.String(16))

def __init__(self, id, desc):
self.id = id
self.desc = desc

def __repr__(self):
return f'<id:{self.id} {self.desc}>'

Expand All @@ -35,14 +37,15 @@ def __repr__(self):
IMAGESTATUS_IN_WORK = 1
IMAGESTATUS_DONE = 2

class ImageStatus (db.Model):

class ImageStatus(db.Model):
id = db.Column(db.Integer, primary_key=True)
desc = db.Column(db.String(16))

def __init__(self, id, desc):
self.id = id
self.desc = desc

def __repr__(self):
return f'<id:{self.id} {self.desc}>'

Expand All @@ -59,52 +62,43 @@ def __init__(self, upload_dt, filename):
self.filename = filename
self.status = IMAGESTATUS_NEW
self.level = IMAGELEVEL_NOTHING

def __repr__(self):
return '<id:{} filename:{} status:{} level:{}>'.format(self.id,
self.filename,
self.status,
self.level)

return f'<id:{self.id} filename:{self.filename} status:{self.status} level:{self.level}>'

def get_thumbnail_path(self):
return os.path.join(app.config['THUMBNAIL_FOLDER'], self.filename)

def get_downsized_path(self):
return os.path.join(app.config['DOWNSIZED_FOLDER'], self.filename)

def get_image_path(self):
return os.path.join(app.config['IMAGE_FOLDER'], self.filename)

def get_split_path(self):
# Get the file name without extension
fn_parts = self.filename.split('.')
fn = ''.join(fn_parts[:-1])
return os.path.join(app.config['SPLIT_FOLDER'], fn)

def get_split_rel_path(self):
fn_parts = self.filename.split('.')
return fn_parts[-2]


class GridCell(db.Model):
id = db.Column(db.Integer, primary_key=True)
fk_grid_item = db.Column(db.Integer,
db.ForeignKey('grid_item.id'),
index=True)
fk_grid_item = db.Column(db.Integer, db.ForeignKey('grid_item.id'), index=True)
grid_item = db.relationship("GridItem")
filename = db.Column(db.String(16))
col = db.Column(db.Integer)
row = db.Column(db.Integer)

def __init__(self, grid_item, filename, col, row):
self.fk_grid_item = grid_item
self.filename = filename
self.col = col
self.row = row

def __repr__(self):
return '<id:{} part_of:{} filename: {} col:{} row:{}>'.format(self.id,
self.grid_item,
self.filename,
self.col,
self.row)
return f'<id:{self.id} part_of:{self.grid_item} filename: {self.filename} col:{self.col} row:{self.row}>'
101 changes: 46 additions & 55 deletions waznexserver/process_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import shutil
import subprocess
import sys
import traceback

from PIL import Image
Expand All @@ -18,25 +17,21 @@
def run_basic_transforms(grid_image):
try:
# Copy orig and create downsized version (1024x1024 max)
app.logger.info('Generating downsized image for ' +
grid_image.filename)
shutil.copy2(grid_image.get_image_path(),
grid_image.get_downsized_path())
app.logger.info('Generating downsized image for ' + grid_image.filename)
shutil.copy2(grid_image.get_image_path(), grid_image.get_downsized_path())
downs = Image.open(grid_image.get_downsized_path())
downs.thumbnail((1024,1024), Image.LANCZOS)
downs.thumbnail((1024, 1024), Image.LANCZOS)
downs.save(grid_image.get_downsized_path(), "JPEG")

# Copy orig and create thumbnail version
app.logger.info('Generating thumbnail for ' + grid_image.filename)
shutil.copy2(grid_image.get_image_path(),
grid_image.get_thumbnail_path())
shutil.copy2(grid_image.get_image_path(), grid_image.get_thumbnail_path())
thumb = Image.open(grid_image.get_thumbnail_path())
thumb.thumbnail((316,316), Image.LANCZOS)
thumb.thumbnail((316, 316), Image.LANCZOS)
thumb.save(grid_image.get_thumbnail_path(), "JPEG")

except Exception:
print("Error while performing basic transforms on {}".format(
grid_image.filename))
print(f"Error while performing basic transforms on {grid_image.filename}")
traceback.print_exc()
return False

Expand All @@ -48,66 +43,66 @@ def run_gridsplitter(grid_image):
if not config.GRIDSPLITTER_PYTHON or not config.GRIDSPLITTER_SLICER:
print("GridSplitter is not configured. Check your config.py.")
return False

# Check validity of GRIDSPLITTER_PYTHON
slicer_python = os.path.abspath(config.GRIDSPLITTER_PYTHON)
slicer_python = os.path.abspath(config.GRIDSPLITTER_PYTHON)
if not os.path.exists(slicer_python):
print('Aborting: Could not find GridSplitter Python.')
print(f'Tried: {slicer_python}')
return False

# Check validity of GRIDSPLITTER_SLICER
slicer = os.path.abspath(config.GRIDSPLITTER_SLICER)
if not os.path.exists(slicer):
print('Aborting: Could not find GridSplitter.')
print(f'Tried: {slicer}')
return False

# Run the splitter for this image
ret_val = subprocess.call([slicer_python,
slicer,
grid_image.get_image_path(),
config.GRIDSPLITTER_COLOR,
grid_image.get_split_path(),
config.GRIDSPLITTER_CELL_WIDTH,
config.GRIDSPLITTER_CELL_HEIGHT])
#if ret_val:
# TODO Give slicer.py return values meaningful assignments
ret_val = subprocess.call(
[
slicer_python,
slicer,
grid_image.get_image_path(),
config.GRIDSPLITTER_COLOR,
grid_image.get_split_path(),
config.GRIDSPLITTER_CELL_WIDTH,
config.GRIDSPLITTER_CELL_HEIGHT,
]
)
# if ret_val:
# TODO Give slicer.py return values meaningful assignments
# print "Unknown error slicing: %s" % (grid_image.filename,)
# return False

# Run verification and sanity checks
if not verify_gridsplitter(grid_image):
return False

# Build Cell Grid
grid_dir = grid_image.get_split_path()
cells = [c for c in os.listdir(grid_dir) if\
c.startswith(config.GRIDSPLITTER_CELL_PREFIX)]
cells = [c for c in os.listdir(grid_dir) if c.startswith(config.GRIDSPLITTER_CELL_PREFIX)]
for cell in cells:
parts = cell.split('.')[0].split("-")
c = models.GridCell(grid_image.id, cell, int(parts[1]), int(parts[2]))
db.session.add(c)

app.logger.info("Successfully sliced: " + grid_image.filename)
return True


def verify_gridsplitter(grid_image):
grid_dir = grid_image.get_split_path()

# Find all of the cell images
cells = [c for c in os.listdir(grid_dir) if\
c.startswith(config.GRIDSPLITTER_CELL_PREFIX)]

cells = [c for c in os.listdir(grid_dir) if c.startswith(config.GRIDSPLITTER_CELL_PREFIX)]

# Verify rough count is within MIN and MAX
min_ct = config.GRIDSPLITTER_MIN_COLS *config.GRIDSPLITTER_MIN_ROWS
max_ct = config.GRIDSPLITTER_MAX_COLS *config.GRIDSPLITTER_MAX_ROWS
min_ct = config.GRIDSPLITTER_MIN_COLS * config.GRIDSPLITTER_MIN_ROWS
max_ct = config.GRIDSPLITTER_MAX_COLS * config.GRIDSPLITTER_MAX_ROWS
cell_ct = len(cells)
if (cell_ct < min_ct) or (cell_ct > max_ct):
print(("Cell count was out of range %d-%d:%d") % (min_ct,
max_ct,
cell_ct))
print(("Cell count was out of range %d-%d:%d") % (min_ct, max_ct, cell_ct))
return False
# Verify each cell is within MIN and MAX rows and cols
high_col = -1
Expand All @@ -116,33 +111,29 @@ def verify_gridsplitter(grid_image):
parts = cell.split('.')[0].split("-")
col = int(parts[1])
row = int(parts[2])
if col < 0 or col > config.GRIDSPLITTER_MAX_COLS or\
row < 0 or row > config.GRIDSPLITTER_MAX_ROWS:
print("Column or row count was incorrect")
if col < 0 or col > config.GRIDSPLITTER_MAX_COLS or row < 0 or row > config.GRIDSPLITTER_MAX_ROWS:
print("Column or row count was incorrect")
return False
if col > high_col:
high_col = col
if row > high_row:
high_row = row
if (high_col < (config.GRIDSPLITTER_MIN_COLS - 1)) or\
(high_row < (config.GRIDSPLITTER_MIN_ROWS - 1)):
print("Too few rows or columns found.")
if (high_col < (config.GRIDSPLITTER_MIN_COLS - 1)) or (high_row < (config.GRIDSPLITTER_MIN_ROWS - 1)):
print("Too few rows or columns found.")
return False

# TODO Verify images form an actual grid

return True


def process_new_images():
new_grids = db.session.query(models.GridItem).\
filter_by(status=models.IMAGESTATUS_NEW).\
order_by('upload_dt').all()
new_grids = db.session.query(models.GridItem).filter_by(status=models.IMAGESTATUS_NEW).order_by('upload_dt').all()

for g in new_grids:
db.session.add(g)
g.status = models.IMAGESTATUS_IN_WORK

try:
# Do basic image transforms
basic_result = run_basic_transforms(g)
Expand All @@ -152,7 +143,7 @@ def process_new_images():
else:
g.status = models.IMAGESTATUS_BAD
print("Basic Failed")

# Do advanced image transforms
if basic_result and config.ENABLE_GRIDSPLITTER:
gs_result = run_gridsplitter(g)
Expand All @@ -161,9 +152,9 @@ def process_new_images():
print("GridSplitter OK")
else:
# Uncomment to mark it bad
#g.status = models.IMAGESTATUS_BAD
# g.status = models.IMAGESTATUS_BAD
print("GridSplitter Failed")

if basic_result:
g.status = models.IMAGESTATUS_DONE

Expand Down
28 changes: 0 additions & 28 deletions waznexserver/tests/hammeruploads/hammerit.py

This file was deleted.

Loading