Generate a thumbnail on the fly for a particular task's image
(self, request, pk=None, project_pk=None, image_filename="")
| 28 | |
| 29 | class Thumbnail(TaskNestedView): |
| 30 | def get(self, request, pk=None, project_pk=None, image_filename=""): |
| 31 | """ |
| 32 | Generate a thumbnail on the fly for a particular task's image |
| 33 | """ |
| 34 | task = self.get_and_check_task(request, pk) |
| 35 | image_path = task.get_image_path(image_filename) |
| 36 | if not os.path.isfile(image_path): |
| 37 | raise exceptions.NotFound() |
| 38 | |
| 39 | try: |
| 40 | thumb_size = int(self.request.query_params.get('size', 512)) |
| 41 | if thumb_size < 1: |
| 42 | raise ValueError() |
| 43 | |
| 44 | quality = int(self.request.query_params.get('quality', 75)) |
| 45 | if quality < 0 or quality > 100: |
| 46 | raise ValueError() |
| 47 | |
| 48 | center_x = float(self.request.query_params.get('center_x', '0.5')) |
| 49 | center_y = float(self.request.query_params.get('center_y', '0.5')) |
| 50 | if center_x < -0.5 or center_x > 1.5 or center_y < -0.5 or center_y > 1.5: |
| 51 | raise ValueError() |
| 52 | |
| 53 | draw_points = self.request.query_params.getlist('draw_point') |
| 54 | point_colors = self.request.query_params.getlist('point_color') |
| 55 | point_radiuses = self.request.query_params.getlist('point_radius') |
| 56 | |
| 57 | points = [] |
| 58 | i = 0 |
| 59 | for p in draw_points: |
| 60 | coords = list(map(float, p.split(","))) |
| 61 | if len(coords) != 2: |
| 62 | raise ValueError() |
| 63 | |
| 64 | points.append({ |
| 65 | 'x': coords[0], |
| 66 | 'y': coords[1], |
| 67 | 'color': hex2rgb(point_colors[i]) if i < len(point_colors) else (255, 255, 255), |
| 68 | 'radius': float(point_radiuses[i]) if i < len(point_radiuses) else 1.0, |
| 69 | }) |
| 70 | |
| 71 | i += 1 |
| 72 | |
| 73 | zoom = float(self.request.query_params.get('zoom', '1')) |
| 74 | if zoom < 0.1 or zoom > 10: |
| 75 | raise ValueError() |
| 76 | |
| 77 | except ValueError: |
| 78 | raise exceptions.ValidationError("Invalid query parameters") |
| 79 | |
| 80 | with Image.open(image_path) as img: |
| 81 | if img.mode != 'RGB': |
| 82 | img = normalize(img) |
| 83 | img = img.convert('RGB') |
| 84 | w, h = img.size |
| 85 | thumb_size = min(max(w, h), thumb_size) |
| 86 | |
| 87 | # Move image center |
nothing calls this directly
no test coverage detected