Introduce image request query parameter for filtering and selection (#2)

* Add selection ability using image query parameter

the selections is the index of the list of images,
where 0 is the first image. `os.listdir` is asumed
to always sort the list the same.

- if no selection is provided, take random image
  from image collection (like main)
- if 1 selection is provided, return the nth image
  from collection
- if 2 or more selections are provided, take random
  item from the selection and return the image at
  that index.

* Handle bad selection input

- Nondigit selections returns HTTP 401
- exclude indexes out of range (0--collection length)
This commit is contained in:
Anders Englöf Ytterström 2024-09-15 20:46:02 +02:00 committed by GitHub
parent 90638fd72a
commit b859fece88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

18
wsgi.py
View file

@ -21,7 +21,23 @@ def get_cropped_image(x, y, grey=False, retries=0):
"""crops a random image from collection"""
if retries > 10:
return None
im_src = random.choice(os.listdir("./images"))
options = os.listdir("./images")
try:
selection = list(
filter(
lambda i: i in range(0, len(options)),
map(int, request.args.getlist("image")),
)
)
except ValueError:
return None
match len(selection):
case 0:
im_src = random.choice(options)
case 1:
im_src = options[selection[0]]
case _:
im_src = options[random.choice(selection)]
im = Image.open(f"images/{im_src}")
out = BytesIO()
max_x, max_y = im.size