def _add_roi(project_id, stack_id, user_id, x_min, x_max, y_min, y_max, z, zoom_level, rotation_cw): """ Add a new ROI database object and return it. """ # Calculate ROI center and extent cx = (x_max + x_min) * 0.5 cy = (y_max + y_min) * 0.5 cz = z width = abs(x_max - x_min) height = abs(y_max - y_min) # Create a new ROI class instance roi = RegionOfInterest() roi.user_id = user_id roi.editor_id = user_id roi.project_id = project_id roi.stack_id = stack_id roi.zoom_level = zoom_level roi.location_x = cx roi.location_y = cy roi.location_z = cz roi.width = width roi.height = height roi.rotation_cw = rotation_cw roi.save() # Create cropped image, if wanted if settings.ROI_AUTO_CREATE_IMAGE: file_name, file_path = create_roi_path(roi.id) create_roi_image(request.user, project_id, roi.id, file_path) return roi