Skip to content

Instantly share code, notes, and snippets.

@marcrasi
Created February 26, 2020 05:23
Show Gist options
  • Save marcrasi/e497229dcfdbbd43a7c23252084d71e2 to your computer and use it in GitHub Desktop.
Save marcrasi/e497229dcfdbbd43a7c23252084d71e2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "tif_loader.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "swift",
"display_name": "Swift"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "kZRlD4utdPuX",
"colab_type": "code",
"colab": {}
},
"source": [
"// Helper function for downloading the tif.\n",
"import Foundation\n",
"import FoundationNetworking\n",
"func download(from sourceString: String, to destinationString: String) {\n",
" let source = URL(string: sourceString)!\n",
" let destination = URL(fileURLWithPath: destinationString)\n",
" let data = try! Data.init(contentsOf: source)\n",
" try! data.write(to: destination)\n",
"}\n",
"\n",
"// Thanks to https://www.nightprogrammer.org/development/multipage-tiff-example-download-test-image-file/ for this example.\n",
"download(\n",
" from: \"http://www.nightprogrammer.org/wp-uploads/2013/02/multipage_tiff_example.tif\",\n",
" to: \"multipage_tiff_example.tif\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "sz13-SHy6zhR",
"colab_type": "code",
"colab": {}
},
"source": [
"import Python\n",
"let Image = Python.import(\"PIL.Image\")\n",
"let np = Python.import(\"numpy\")\n",
"let im = Image.open(\"multipage_tiff_example.tif\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4jeXkaw67X_f",
"colab_type": "code",
"colab": {}
},
"source": [
"import TensorFlow\n",
"func tensor(image: PythonObject, frame: Int) -> Tensor<UInt8>? {\n",
" image.seek(frame)\n",
" return Tensor(numpy: np.array(image))\n",
"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BsoQwKip7iUL",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"outputId": "d1eadb7d-ec4f-4f66-9166-d586a5692ed3"
},
"source": [
"tensor(image: im, frame: 0)?.shape"
],
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"▿ Optional<TensorShape>\n",
" ▿ some : [600, 800, 3]\n",
" ▿ dimensions : 3 elements\n",
" - 0 : 600\n",
" - 1 : 800\n",
" - 2 : 3\n"
]
},
"metadata": {
"tags": []
},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "PHBb7yBI8al0",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"outputId": "d760479f-cf4a-4da6-91c0-8fa753907eac"
},
"source": [
"tensor(image: im, frame: 1)?.shape"
],
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"▿ Optional<TensorShape>\n",
" ▿ some : [600, 800, 3]\n",
" ▿ dimensions : 3 elements\n",
" - 0 : 600\n",
" - 1 : 800\n",
" - 2 : 3\n"
]
},
"metadata": {
"tags": []
},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Kdz6FVTT8dEz",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment