Imaging¶

6/27/2023¶

https://bits.csb.pitt.edu/files/bioimaging.slides.html¶

In [29]:
%%html
<script src="https://bits.csb.pitt.edu/asker.js/lib/asker.js"></script>
<style>
.reveal .highlight pre { font-size: 100%}
.reveal .slides>section>section.present { max-height: 100%; overflow-y: auto;}
</style>

<script>
$3Dmolpromise = new Promise((resolve, reject) => { 
    require(['https://3dmol.org/build/3Dmol.js'], function(){       
            resolve();});
});

require(['https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.js'], function(Ch){
 Chart = Ch;
});

$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');


//the callback is provided a canvas object and data 
var chartmaker = function(canvas, labels, data) {
  var ctx = $(canvas).get(0).getContext("2d");
     var dataset = {labels: labels,                     
    datasets:[{
     data: data,
     backgroundColor: "rgba(150,64,150,0.5)",
         fillColor: "rgba(150,64,150,0.8)",    
  }]};
  var myBarChart = new Chart(ctx,{type:'bar',data:dataset,options:{legend: {display:false},
        scales: {
            yAxes: [{
                ticks: {
                    min: 0,
                }
            }]}}});
};

$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>

Images¶

Images are composed of pixels (pix - picture, el - element).

In a grayscale image the pixel contains the intensity. Depending on the image format this may range from 0-1, 0-255, or be any floating point number.

In a color image a pixel is (usually) a triple (red,green,blue) of color values where each color intensity ranges from 0-255 (24-bit color).

In [30]:
%%html
<div id="imcol2" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');

    var divid = '#imcol2';
	jQuery(divid).asker({
	    id: divid,
	    question: "Which contains the most information?",
        answers: ['Gray','Color'],
		extra: ['A grayscale image with floating point pixels', 'A color image where pixel values range from 0 to 255'],
        server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>

A slightly irrelevant aside¶

Or, how to count with 16 fingers¶

It is very common for RGB color values to be represented in hexadecimal (e.g., HTML).

Hexadecimal number are usually indicated with a 0x or # prefix.

In hexadecimal, each digit can take one of 16 values (as opposed to 10). This means that each color can be represented with two digits (16*16 = 256)

https://www.youtube.com/watch?v=ffB0Je-xjKg

HexDec
A10
B11
C12
D13
E14
F15
In [3]:
%%html
<div id="hex1hs" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');

    var divid = '#hex1hs';
	jQuery(divid).asker({
	    id: divid,
	    question: "What is the decimal value of 0x1F?",
		answers: ['11','16','17','31','32'],
        server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>
In [4]:
%%html
<div id="hex2hs" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');

    var divid = '#hex2hs';
	jQuery(divid).asker({
	    id: divid,
	    question: "What is the decimal value of 0xfe?",
		answers: ['15','32','127','254','255'],
        server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>
In [5]:
%%html
<div id="hex3hs" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');

    var divid = '#hex3hs';
	jQuery(divid).asker({
	    id: divid,
	    question: "What color is #ff00ff?",
		answers: ['green','cyan','yellow','magenta','I failed art'],
        server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>

Answers¶

In [6]:
print(0x1f, 0xfe)
31 254

<div style="color: #ff00ff">Hello</div>

Hello

PIL - The Python Image Library¶

documentation

The Python Imaging Library provides the ability to read, write an manipulate images in all the common formats. Some key modules:

  • PIL.Image - primary Image object
  • PIL.ImageChops - channel operations, for blending two images
  • PIL.ImageEnhance - enhance your photos (sharpness, contrast, ...)
  • PIL.ImageFilter - filters (blur, smooth, sharpen, ...)
  • PIL.ImageOps - assorted operations (resizing, equalize, autocontrast, ...)
  • PIL.ImageStat - statistics on image (min, max, ...)

History¶

Despite being a popular package, PIL development stopped in 2011 with no new releases since 2009.

Pillow is a fork of PIL that is fully backwards compatible and updated (and is what you are actually using rather than the original PIL).

https://python-pillow.org

Setup...¶

In [7]:
!wget http://bits.csb.pitt.edu/images/image1.tif
--2023-06-27 07:51:40--  http://bits.csb.pitt.edu/images/image1.tif
Resolving bits.csb.pitt.edu (bits.csb.pitt.edu)... 136.142.4.139
Connecting to bits.csb.pitt.edu (bits.csb.pitt.edu)|136.142.4.139|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 921814 (900K) [image/tiff]
Saving to: ‘image1.tif.2’

image1.tif.2        100%[===================>] 900.21K  --.-KB/s    in 0.06s   

2023-06-27 07:51:40 (15.4 MB/s) - ‘image1.tif.2’ saved [921814/921814]

In [8]:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
%matplotlib inline
In [9]:
from PIL import Image
im = Image.open('image1.tif')
im
Out[9]: