Skip to content

Instantly share code, notes, and snippets.

@ahmadia
Last active September 15, 2015 13:33
Show Gist options
  • Save ahmadia/c1f8be119f3cb2d2b8e5 to your computer and use it in GitHub Desktop.
Save ahmadia/c1f8be119f3cb2d2b8e5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import PIL.Image\n",
"import numpy.testing as npt\n",
"from numba import jit"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def avg_transform(image):\n",
" m, n, c = image.shape\n",
" for i in range(m):\n",
" xi = image[i]\n",
" for j in range(n):\n",
" avg = xi[j].sum()/3\n",
" xi[j][:] = avg\n",
" return image"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def affine_avg_transform(image):\n",
" affine_avg = np.ones((3,3))/3\n",
" image[:] = image.dot(affine_avg)\n",
" return image"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fast_avg_transform = jit(avg_transform, nopython=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAIAAAAlV+npAAAAgklEQVR4nO3QQQkAIADAQLV/Z63g\nXiLcJRibe3BrvQ74iVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmB\nWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZwQFIjwFj\nyDtoQwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<PIL.Image.Image image mode=RGB size=100x50 at 0x10CAC52B0>"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = PIL.Image.new('RGB', (100, 50), (255, 0, 0))\n",
"image"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAIAAAAlV+npAAAAhUlEQVR4nO3QQQ0AIRDAwOM8rn8r\nWKAvQjKjoOmamY8z/+2Al5gVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWY\nFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFWxH\nkAFjY983dgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<PIL.Image.Image image mode=RGB size=100x50 at 0x10FCC8BA8>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image_data = np.asarray(image).copy()\n",
"PIL.Image.fromarray(avg_transform(image_data))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAIAAAAlV+npAAAAhUlEQVR4nO3QQQ0AIRDAwOM8rn8r\nWKAvQjKjoOmamY8z/+2Al5gVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWY\nFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFWxH\nkAFjY983dgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<PIL.Image.Image image mode=RGB size=100x50 at 0x10D7D3CF8>"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image_data = np.asarray(image).copy()\n",
"PIL.Image.fromarray(fast_avg_transform(image_data))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAIAAAAlV+npAAAAhUlEQVR4nO3QQQ0AIRDAwOM8rn8r\nWKAvQjKjoOmamY8z/+2Al5gVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWY\nFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFWxH\nkAFjY983dgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<PIL.Image.Image image mode=RGB size=100x50 at 0x10FCD9C88>"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image_data = np.asarray(image).copy()\n",
"PIL.Image.fromarray(affine_avg_transform(image_data))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"image = PIL.Image.new('RGB', (1280, 720), (255, 0, 0))\n",
"image_data = np.asarray(image).copy()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 25.3 ms per loop\n"
]
}
],
"source": [
"%%timeit\n",
"fast_avg_transform(image_data)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 95.8 ms per loop\n"
]
}
],
"source": [
"%%timeit\n",
"affine_avg_transform(image_data)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment