Dealing with images in a command line fashion can be approach:
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
In [ ]:
from PIL import Image
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.
In [ ]:
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)
In [ ]:
p=1
In [ ]:
type(p)
In [ ]:
type(im)
In [ ]:
print(im.height,im.width)
In [ ]:
im.mode?
In [ ]:
im.show()
In [ ]:
# split the image into individual bands
source = im.split()
In [ ]:
R, G, B = 0, 1, 2
In [ ]:
type(source)
In [ ]:
type(source[R])
In [ ]:
type(source[G])
In [ ]:
source[R]
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:
In [ ]:
def inv_pix(pix):
inv = 255-pix
return inv
In [ ]:
source[R].point(inv_pix);
In [ ]:
source[G].point(inv_pix);
In [ ]:
source[B].point(inv_pix);
In [ ]:
source[G].paste
In [ ]:
source[G].paste
In [ ]:
source[R].paste(source[R].point(inv_pix))
In [ ]:
source[G].paste(source[G].point(inv_pix))
In [ ]:
source[B].paste(source[B].point(inv_pix))
In [ ]:
In [ ]:
Image.merge??
In [ ]:
im.mode
In [ ]:
imnew = Image.merge(im.mode, source)
In [ ]:
imnew.show()
In [ ]:
imnew
In [ ]:
imnew.save('C:\data\GitHub\PythonClub\Mtg01_bl1_MOAB2_s023_scaled20perc_inverted.tif')
Much faster solution using eval
method from Image module (thanks to Gergely). No need to split the channels.
In [ ]:
im = Image.open(t)
imfast = Image.eval(im,inv_pix)
TBD
Image Magick : http://www.imagemagick.org/script/index.php
In [ ]:
convert