from PIL import Image

"""
Explanation: Working with images in Python
Dealing with images in a command line fashion can be approach: 
 - using Python to do the image processing / manipulation itself
 - using Python to batch process the call of a third-party command (i.e. Image Magick) that will do the image manipulation
 - other?
Python Imaging Library (PIL or Pillow)
The PIL package or the more recent fork called Pillow seems to be a robust environement to work with images.
Start by installing Pillow package from your Anaconda prompt
conda install pillow
Once this is successfull import Pillow
End of explanation
"""


import urllib.request
local_loc = 'C:\data\local_image.tif'
urllib.request.urlretrieve('http://folk.uio.no/sebastcc/images/Mtg01_bl1_MOAB2_s023_scaled20perc.tif', local_loc)
im = Image.open(local_loc)

p=1

type(p)

type(im)

print(im.height,im.width)

im.mode?

im.show()

# split the image into individual bands
source = im.split()

R, G, B = 0, 1, 2

type(source)

type(source[R])

type(source[G])

source[R]

"""
Explanation: If you are downloading it from Internet, use the following command by modifying the local_loc to the folder you are interested in saving the image.
End of explanation
"""


def inv_pix(pix):
    inv = 255-pix
    return inv

source[R].point(inv_pix);

source[G].point(inv_pix);

source[B].point(inv_pix);

source[G].paste

source[G].paste

source[R].paste(source[R].point(inv_pix))

source[G].paste(source[G].point(inv_pix))

source[B].paste(source[B].point(inv_pix))

Image.merge??

im.mode

imnew = Image.merge(im.mode, source)

imnew.show()

imnew

imnew.save('C:\data\GitHub\PythonClub\Mtg01_bl1_MOAB2_s023_scaled20perc_inverted.tif')

"""
Explanation: The point() method can be used to translate the pixel values of an image (e.g. image contrast manipulation). In most cases, a function object expecting one argument can be passed to this method. Each pixel is processed according to that function:
End of explanation
"""


im = Image.open(t)
imfast = Image.eval(im,inv_pix)

"""
Explanation: Much faster solution using eval method from Image module (thanks to Gergely). No need to split the channels.
End of explanation
"""


convert

"""
Explanation: Calling Image Magick from Python
TBD
Image Magick : http://www.imagemagick.org/script/index.php
End of explanation
"""


