Skip to content

Instantly share code, notes, and snippets.

@tischi
Last active January 16, 2020 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tischi/2b3e87f23694471d07bc959432115b5c to your computer and use it in GitHub Desktop.
Save tischi/2b3e87f23694471d07bc959432115b5c to your computer and use it in GitHub Desktop.
Hacking with ImJoy to just get some of the concepts....
<docs lang="markdown">
[TODO: write documentation for this plugin.]
</docs>
<config lang="json">
{
"name": "Batch Blur",
"type": "window",
"tags": [],
"ui": "",
"version": "0.1.0",
"cover": "",
"description": "[TODO: describe this plugin with one sentence.]",
"icon": "extension",
"inputs": null,
"outputs": null,
"api_version": "0.1.7",
"env": "",
"permissions": [],
"requirements": ["https://unpkg.com/spectre.css/dist/spectre.min.css"],
"dependencies": ["https://gist.githubusercontent.com/tischi/2b3e87f23694471d07bc959432115b5c/raw/ImJoyGaussianBlurHack.imjoy.html"],
"defaults": {"w": 20, "h": 10}
}
</config>
<script lang="javascript">
class ImJoyPlugin {
async setup() {
const p = await api.getPlugin("Gaussian Blur")
window.batch_process = async function(){
const ret = await api.showFileDialog({type:"directory",title:"Please select a directory with images"})
//api.alert("You selected: " + ret.path)
api.showMessage("Processing...")
const image_path = "image.png"
const output_path = "blurred_image_tmp.png"
const sigma = 10
// TODO: the p.process function would have to take ret.path as an input and do the looping over all images
const out = await p.process(image_path, output_path, sigma)
api.alert(out.sigma)
api.showMessage("...done!")
}
}
async run(ctx) {
}
}
api.export(new ImJoyPlugin())
</script>
<window lang="html">
<div>
<button class="btn" onclick="batch_process()">Batch Blur</button>
</div>
</window>
<style lang="css">
</style>
<docs lang="markdown">
[TODO: write documentation for this plugin.]
</docs>
<config lang="json">
{
"name": "Gaussian Blur",
"type": "native-python",
"version": "0.1.0",
"description": "[TODO: describe this plugin with one sentence.]",
"tags": [],
"ui": "",
"cover": "",
"inputs": null,
"outputs": null,
"flags": [],
"icon": "extension",
"api_version": "0.1.7",
"env": "",
"permissions": [],
"requirements": ["scikit-image"],
"dependencies": []
}
</config>
<script lang="python">
from imjoy import api
from skimage import data
from skimage.filters import gaussian
from skimage.io import imsave
from skimage.io import imread
import base64
class ImJoyPlugin():
def setup(self):
api.log('initialized')
def run(self, ctx):
api.alert('hello world.')
image = data.camera()
blurred_image = gaussian( image, 10 )
imsave("blurred-image.png",blurred_image)
imsave("image.png",image)
self.compare("image.png", "blurred-image.png")
def process(self, image_path, output_path, sigma=10):
image = imread(image_path)
blurred_image = gaussian(image, sigma)
imsave(output_path, blurred_image)
api.showMessage("Blurring done!")
return {"out":output_path,"sigma":sigma}
def display(self, image_path):
with open(image_path, 'rb') as f:
data = f.read()
result = base64.b64encode(data).decode('ascii')
imgurl = 'data:image/png;base64,' + result
api.createWindow(type='imjoy/image', w=12, h=15, data={"src": imgurl})
def compare(self, image_path_a, image_path_b):
api.createWindow(type='imjoy/image-compare', w=12, h=15,
data={
"first": self.encode( image_path_a ),
"second": self.encode( image_path_b ) })
def encode(self, image_path):
with open(image_path, 'rb') as f:
data = f.read()
result = base64.b64encode(data).decode('ascii')
imgurl = 'data:image/png;base64,' + result
return imgurl
api.export(ImJoyPlugin())
</script>
@tischi
Copy link
Author

tischi commented Dec 15, 2019

This is not production ready code.
In fact, the batching does not even work yet.
It was just for me to get into it....and see how things work conceptually.

@fjug
Copy link

fjug commented Dec 15, 2019

Lovely, thanks! Would be awesome if the Hackathon gdoc would also contain the link to this gist (we will eventually transfer all the gdoc info to an imagej.net Hackathon wiki page...)

@tischi
Copy link
Author

tischi commented Dec 16, 2019

done

@oeway
Copy link

oeway commented Jan 16, 2020

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment