a
    ¦ˆcž  ã                   @   sÄ   d Z ddlZddlZddlZddlmZ G dd„ dƒZej 	e
¡Zej ed¡Zej ed¡Zeedd	ej ¡ d
d�Ze ¡  ed eje ¡  ¡ ¡ƒ ejdd� ¡ Zed eej d ¡ƒ dS )a  
Introduction to Traits
======================

To make the tutorial more practical, let's imagine that you are
writing some tools to organize and process image data, perhaps
to help you develop a machine learning algorithm, or to document
your research.  This image data might come from a camera, a scanning
electron microscope, or from some more exotic source.  No matter the
source, the image will have data associated with it, such as who
produced the image, the equipment that was used, the date and time
that the image was taken, and so forth.

In this tutorial, we'll consider an example where we have a collection
of greyscale SEM images, each of them stored in a file, along with other
information, such as the sample that was imaged, the size of the scanned
region, and the operator who took the image.

We would like to read in the image and provide a number of standard
analysis steps to the user, as well as making the raw image data
available as a NumPy array.

The sample code here shows how you might create a class in standard
object-oriented Python with NumPy for analysis and Pillow for image
loading.  Experiment with the code in the example to see how you can use it.

In the subsequent steps of the tutorial, we'll look at how we can use Traits
to simplify and improve the code here.
é    N)ÚImagec                   @   s4   e Zd ZdZddd„Zdd„ Zdd„ Zddd„ZdS )r   z  An SEM image stored in a file. ©çñhãˆµøä>r   c                 C   s0   || _ || _|| _|| _|| _| j\| _| _d S )N)ÚfilenameÚ	sample_idÚoperatorÚdate_acquiredÚ	scan_sizeÚ
scan_widthÚscan_height)Úselfr   r   r   r   r	   © r   ún/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/examples/introduction/0_introduction.pyÚ__init__3   s    zImage.__init__c                 C   sL   t  | j¡ d¡}t |¡| _| jjdkrB| j| j	 | jj | _
nd| _
dS )z Read the image from disk. ÚLr   N)ÚPILImageÚopenr   ÚconvertÚnpÚarrayÚimageÚsizer   r
   Ú
pixel_area)r   Z	pil_imager   r   r   Ú
read_image?   s    ÿzImage.read_imagec                 C   s   t j| jdddd�\}}|S )z0 Compute the normalized histogram of the image. é   )r   r   T)ÚbinsÚrangeZdensity)r   Ú	histogramr   )r   Úhistr   r   r   r   r   L   s    ü
zImage.histogramr   éÿ   c                 C   s   | j |k| j |k@ S )z) Compute a threshold mask for the array. )r   )r   ÚlowÚhighr   r   r   Ú	thresholdV   s    zImage.thresholdN)r   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r   r   r   r   r   0   s    ÿ

r   Zimageszsample_0001.pngZHannesZ0001r   )r   r   r   r   r	   z!The maximum intensity of {} is {}éÈ   )r    u9   The area with intensity greater than 200 is {:0.3f} ÂµmÂ²g   ¢”mB)r&   ÚdatetimeÚosÚnumpyr   ZPILr   r   ÚpathÚdirnameÚ__file__Zthis_dirÚjoinZ	image_dirr   Útodayr   r   ÚprintÚformatr   r   Zargmaxr"   ÚsumZpixel_countr   r   r   r   r   Ú<module>   s8   /û	
þÿÿÿ