Skip to content

Instantly share code, notes, and snippets.

@blippy
Created February 28, 2016 11:06
Show Gist options
  • Save blippy/a385dc77f9d74e4876d5 to your computer and use it in GitHub Desktop.
Save blippy/a385dc77f9d74e4876d5 to your computer and use it in GitHub Desktop.
Reducing opacity of an image using python an PIL
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from PIL import Image, ImageEnhance"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"im = Image.open(\"/home/mcarter/cubie/home/mcarter/redact/docs/pics/2014-02.jpg\", \"r\")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"im.show()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def ReduceOpacity(im, opacity):\n",
" \"\"\"\n",
" Returns an image with reduced opacity.\n",
" Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879\n",
" \"\"\"\n",
" assert opacity >= 0 and opacity <= 1\n",
" if im.mode != 'RGBA':\n",
" im = im.convert('RGBA')\n",
" else:\n",
" im = im.copy()\n",
" alpha = im.split()[3]\n",
" alpha = ImageEnhance.Brightness(alpha).enhance(opacity)\n",
" im.putalpha(alpha)\n",
" return im"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"im1 = ReduceOpacity(im, 0.001)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"im1.show()"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ImportError",
"evalue": "No module named 'mython'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-46-5ec39334f3b5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mmython\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mImportError\u001b[0m: No module named 'mython'"
]
}
],
"source": [
"import mython"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment