Render Image Montages
Note
This notebook is modified from how it appears in the ‘notebooks’ directory so it can render properly in the documentation. Primarily, this includes changed paths and mocked user interactions.
Imports
from misalign.model.project import MISProjectJSON
import misalign.canvas.canvas_rectangular as cr
Render Setup
###
mis_filepath="../../../example/project_a/project_a-relations-calibrated.mis.json"
mis_project=MISProjectJSON.load(mis_filepath)
auto_save_renders=False
###
mis_project.find_image_paths(mis_filepath,update=True)
print(mis_project)
A misalign project with:
Images:
image_a01.jpg
image_a02.jpg
image_a03.jpg
image_a04.jpg
image_a05.jpg
image_a06.jpg
image_a07.jpg
image_a08.jpg
image_a09.jpg
image_a10.jpg
Relations:
('image_a01.jpg', 'image_a02.jpg')
('image_a02.jpg', 'image_a03.jpg')
('image_a03.jpg', 'image_a04.jpg')
('image_a04.jpg', 'image_a05.jpg')
('image_a05.jpg', 'image_a06.jpg')
('image_a06.jpg', 'image_a07.jpg')
('image_a07.jpg', 'image_a08.jpg')
('image_a08.jpg', 'image_a09.jpg')
('image_a09.jpg', 'image_a10.jpg')
Calibration:
pixel : 600
length : 1
length_unit : mm
Project Path:
../../../example/project_a/project_a-relations-calibrated.mis.json
origin_relative_offsets=cr.rectangular_solve_project(
project=mis_project,
)
print("Origin-relative offsets")
display(origin_relative_offsets)
origin_relative_extents=cr.find_relative_extents_project(
project=mis_project,
origin_relative_offsets=origin_relative_offsets,)
canvas_extents, canvas_offsets=cr.resolve_extents(origin_relative_extents)
canvas_relative_offsets=cr.place_in_canvas(
image_names=mis_project.get_image_names(),
origin_relative_offsets=origin_relative_offsets,
canvas_extents=canvas_extents,
canvas_offsets=canvas_offsets)
print("Canvas-relative offsets")
display(canvas_relative_offsets)
Origin-relative offsets
{'image_a01.jpg': (0, 0),
'image_a02.jpg': (12, -1088),
'image_a03.jpg': (20, -2229),
'image_a04.jpg': (-109, -3287),
'image_a05.jpg': (-98, -4230),
'image_a06.jpg': (-93, -5090),
'image_a07.jpg': (-83, -6080),
'image_a08.jpg': (-74, -7089),
'image_a09.jpg': (-65, -8028),
'image_a10.jpg': (-62, -8750)}
Canvas-relative offsets
{'image_a01.jpg': (20, 0),
'image_a02.jpg': (8, 1088),
'image_a03.jpg': (0, 2229),
'image_a04.jpg': (129, 3287),
'image_a05.jpg': (118, 4230),
'image_a06.jpg': (113, 5090),
'image_a07.jpg': (103, 6080),
'image_a08.jpg': (94, 7089),
'image_a09.jpg': (85, 8028),
'image_a10.jpg': (82, 8750)}
Render Unblended
unblended_canvas=cr.render_unblended_project(
project=mis_project,
canvas_relative_offsets=canvas_relative_offsets,
canvas_extents=canvas_extents)
display(unblended_canvas)
if auto_save_renders:
unblended_save_fp=mis_filepath.replace(".mis.json","-unblend.png")
unblended_canvas.save(unblended_save_fp)
Render Blended
blended_canvas_dfe=cr.render_blended_project(
project=mis_project,
canvas_relative_offsets=canvas_relative_offsets,
canvas_extents=canvas_extents,
weight=cr.weight_dfe)
display(blended_canvas_dfe)
if auto_save_renders:
blended_save_fp=mis_filepath.replace(".mis.json","-blend.png")
blended_canvas_dfe.save(blended_save_fp)
Image Scale Bar
from misalign.calibration.scale_bar import image_with_scale_bar,scale_bar_calibrate,save_calibrated_image
from misalign.calibration.calibrate import CalibrationManual
from PIL.Image import Transpose
%matplotlib widget
## Select image, it can be either a filepath to an image or a PIL Image object.
###
# selected_image=r"../../../example/expected_result/set_a/a_mycanvas_blend.jpg"
selected_image=blended_canvas_dfe
# selected_image=unblended_canvas
###
## Tranpose selected PIL Image object if needed.
# selected_image=selected_image.transpose(Transpose.FLIP_LEFT_RIGHT)
# selected_image=selected_image.transpose(Transpose.FLIP_TOP_BOTTOM)
selected_image=selected_image.transpose(Transpose.ROTATE_90)
# selected_image=selected_image.transpose(Transpose.ROTATE_180)
# selected_image=selected_image.transpose(Transpose.ROTATE_270)
###
selected_calibration=mis_project.get_calibration()
#
# calibration_path=r"..\example\data\set_a\a_5x.miscal"
# cm=CalibrationManual()
# cm.load_calibration(calibration_path)
# selected_calibration=cm.distances
###
image_with_scale_bar(
image=selected_image,
scale_measurement="1mm",
calibration=selected_calibration,
loc="upper left")
### Code used to correct rendering for documentation generation. ###
# Runs scale calibration code in the same cells as plotting code for figure generation.
scale_bar_calibrate(scale_dpi=1000)
from IPython.display import clear_output
from matplotlib import pyplot as plt
clear_output()
plt.show()
### End ###
Note
No further cells are executed in this notebook in the documentation.
scaled_dpi=1000
# Increasing this DPI makes the above image smaller
# As a result, increasing the DPI makes the scale bar text/box larger relative to the image.
# Changing this DPI does not effect the resolution of the final image
#
scale_bar_calibrate(scale_dpi=scaled_dpi)
if type(selected_image) is str:
split_fp=selected_image.split(".")
scalebar_save_fp=split_fp[0]+"-scale."+split_fp[1]
else:
scalebar_save_fp=mis_filepath.replace(".mis.json","-scale.png")
save_calibrated_image(scalebar_save_fp,scale_dpi=scaled_dpi)