Last active
May 25, 2018 06:55
-
-
Save csachs/a1ba66cd24dff06d9dc261a60a961325 to your computer and use it in GitHub Desktop.
tiff file feeder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy | |
from datetime import datetime | |
from flask import Blueprint, Flask, make_response, request | |
from tifffile import TiffFile | |
# BSD Licensed | |
bp = Blueprint('SimulatedCameraImageFeeder', __name__) | |
current_slice = 0 | |
@bp.route('/') | |
def image(): | |
width = int(request.args.get('width')) | |
height = int(request.args.get('height')) | |
depth = int(request.args.get('depth')) | |
channel = int(request.args.get('channel')) | |
x = float(request.args.get('x')) | |
y = float(request.args.get('y')) | |
z = float(request.args.get('z')) | |
depth_map = { | |
1: numpy.uint8, | |
2: numpy.uint16 | |
} | |
result = numpy.zeros((height, width,), depth_map[depth]) | |
global current_slice | |
if current_slice >= image_data.shape[0]: | |
current_slice = 0 | |
image = image_data[current_slice] | |
current_slice += 1 | |
result[:min(image.shape[0], result.shape[0]), :min(image.shape[1], result.shape[1])] = \ | |
image[:min(image.shape[0], result.shape[0]), :min(image.shape[1], result.shape[1])] | |
return make_response(result.tostring()) | |
def main(): | |
global image_data | |
if len(sys.argv) < 2: | |
print("Usage:\n%s file_to_serve.tiff <host:port>" % (sys.argv[0],)) | |
return | |
if len(sys.argv) < 3: | |
host_port = 'localhost:8555' | |
else: | |
host_port = sys.argv[2] | |
host, port = host_port.split(':') | |
# important: this is dependent on the layout of the tiff file | |
# this should work with tiff files saved with ImageJ | |
image_data = TiffFile(sys.argv[1]).pages[0].asarray() | |
app = Flask(__name__) | |
app.register_blueprint(bp) | |
app.run(debug=True, host=host, port=int(port)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Christian,
The simulating camera works great! I had to modify this tifffeeder.py script, however, to work with tifffile 0.14.0:
Line 35
Line 61