ó
N'—^c           @  s  d  Z  d d l m Z d d l m Z d e f d „  ƒ  YZ e d k rd d l Z e ƒ  Z	 e
 e j d ƒ d	 k r e	 j e j d ƒ nˆ e d
 ƒ x{ e ry2 e j d	 d k rÅ e d ƒ Z n e d ƒ Z Wn e k
 rð e d ƒ Pn Xe	 j e j ƒ  ƒ e e	 j ƒ qš Wn  d S(   s?  PILdriver, an image-processing calculator using PIL.

An instance of class PILDriver is essentially a software stack machine
(Polish-notation interpreter) for sequencing PIL image
transformations.  The state of the instance is the interpreter stack.

The only method one will normally invoke after initialization is the
`execute' method.  This takes an argument list of tokens, pushes them
onto the instance's stack, and then tries to clear the stack by
successive evaluation of PILdriver operators.  Any part of the stack
not cleaned off persists and is part of the evaluation context for
the next call of the execute method.

PILDriver doesn't catch any exceptions, on the theory that these
are actually diagnostic information that should be interpreted by
the calling code.

When called as a script, the command-line arguments are passed to
a PILDriver instance.  If there are no command-line arguments, the
module runs an interactive interpreter, each line of which is split into
space-separated tokens and passed to the execute method.

In the method descriptions below, a first line beginning with the string
`usage:' means this method can be invoked with the token that follows
it.  Following <>-enclosed arguments describe how the method interprets
the entries on the stack.  Each argument specification begins with a
type specification: either `int', `float', `string', or `image'.

All operations consume their arguments off the stack (use `dup' to
keep copies around).  Use `verbose 1' to see the stack state displayed
before each operation.

Usage examples:

    `show crop 0 0 200 300 open test.png' loads test.png, crops out a portion
of its upper-left-hand corner and displays the cropped portion.

    `save rotated.png rotate 30 open test.tiff' loads test.tiff, rotates it
30 degrees, and saves the result as rotated.png (in PNG format).
iÿÿÿÿ(   t   print_function(   t   Imaget	   PILDriverc           B  s   e  Z d  Z d „  Z g  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d  „  Z# d! „  Z$ d" „  Z% d# „  Z& d$ „  Z' d% „  Z( d& „  Z) d' „  Z* d( „  Z+ d) „  Z, d* „  Z- d+ „  Z. d, „  Z/ RS(-   i    c         C  s   t  |  j ƒ  ƒ |  _ d S(   sP   usage: verbose <int:num>

        Set verbosity flag from top of stack.
        N(   t   intt   do_popt   verbose(   t   self(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt
   do_verbose<   s    c         C  s   |  j  j d | ƒ d S(   s+   Push an argument onto the evaluation stack.i    N(   t   stackt   insert(   R   t   item(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   pushG   s    c         C  s   |  j  d S(   s    Return the top-of-stack element.i    (   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   topK   s    c         C  s   g  |  _  d S(   s/   usage: clear

        Clear the stack.
        N(   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_clearQ   s    c         C  s   |  j  j d ƒ S(   sB   usage: pop

        Discard the top element on the stack.
        i    (   R   t   pop(   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyR   X   s    c         C  sC   t  |  d ƒ r% |  j d j ƒ  } n |  j d } |  j | ƒ d S(   s=   usage: dup

        Duplicate the top-of-stack item.
        t   formati    N(   t   hasattrR   t   copyR   (   R   t   dup(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_dup_   s    c         C  s,   |  j  d |  j  d g |  j  d |  _  d S(   sP   usage: swap

        Swap the top-of-stack item with the next one down.
        i   i    i   N(   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_swapj   s    c         C  s\   t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } |  j t j d | | f | ƒ ƒ d S(   s|   usage: new <int:xsize> <int:ysize> <int:color>:

        Create and push a greyscale image of given size and color.
        t   LN(   R   R   R   R   t   new(   R   t   xsizet   ysizet   color(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_news   s    c         C  s    |  j  t j |  j ƒ  ƒ ƒ d S(   so   usage: open <string:filename>

        Open the indicated image, read it, push the image on the stack.
        N(   R   R   t   openR   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_open}   s    c         C  sJ   |  j  ƒ  } |  j  ƒ  } t |  j  ƒ  ƒ } |  j t j | | | ƒ ƒ d S(   s~   usage: blend <image:pic1> <image:pic2> <float:alpha>

        Replace two images and an alpha with the blended image.
        N(   R   t   floatR   R   t   blend(   R   t   image1t   image2t   alpha(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_blend„   s    c         C  sD   |  j  ƒ  } |  j  ƒ  } |  j  ƒ  } |  j t j | | | ƒ ƒ d S(   s}   usage: composite <image:pic1> <image:pic2> <image:mask>

        Replace two images and a mask with their composite.
        N(   R   R   R   t	   composite(   R   R   R    t   mask(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_compositeŽ   s    c         C  sS   |  j  ƒ  } g  } x! | D] } | j |  j  ƒ  ƒ q W|  j t j | | ƒ ƒ d S(   s¸   usage: merge <string:mode> <image:pic1>
                        [<image:pic2> [<image:pic3> [<image:pic4>]]]

        Merge top-of stack images in a way described by the mode.
        N(   R   t   appendR   R   t   merge(   R   t   modet   bandlistt   band(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_merge˜   s
    c         C  s2   |  j  ƒ  } |  j  ƒ  } |  j | j | ƒ ƒ d S(   sd   usage: convert <string:mode> <image:pic1>

        Convert the top image to the given mode.
        N(   R   R   t   convert(   R   R(   t   image(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt
   do_convert¦   s    c         C  s   |  j  ƒ  d S(   sV   usage: copy <image:pic1>

        Make and push a true copy of the top image.
        N(   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_copy¯   s    c         C  sz   t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } |  j ƒ  } |  j | j | | | | f ƒ ƒ d S(   s«   usage: crop <int:left> <int:upper> <int:right> <int:lower>
                       <image:pic1>

        Crop and push a rectangular region from the current image.
        N(   R   R   R   t   crop(   R   t   leftt   uppert   rightt   lowerR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_crop¶   s    c         C  sS   |  j  ƒ  } t |  j  ƒ  ƒ } t |  j  ƒ  ƒ } |  j |  j | | | f ƒ ƒ d S(   st   usage: draft <string:mode> <int:xsize> <int:ysize>

        Configure the loader for a given mode and size.
        N(   R   R   R   t   draft(   R   R(   R   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_draftÃ   s    c         C  sQ   d d l  m } t | |  j ƒ  j ƒ  ƒ } |  j ƒ  } |  j | j | ƒ ƒ d S(   sm   usage: filter <string:filtername> <image:pic1>

        Process the top image with the given filter.
        iÿÿÿÿ(   t   ImageFilterN(   t   PILR8   t   getattrR   R2   R   t   filter(   R   R8   t   imageFilterR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_filterÍ   s    c         C  sZ   |  j  ƒ  j ƒ  } |  j | d ƒ |  j | d ƒ |  j | d ƒ |  j | d ƒ d S(   sg   usage: getbbox

        Push left, upper, right, and lower pixel coordinates of the top image.
        i   i   i   i    N(   R   t   getbboxR   (   R   t   bounding_box(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt
   do_getbbox×   s
    c         C  s8   |  j  ƒ  j ƒ  } |  j | d ƒ |  j | d ƒ d S(   sX   usage: extrema

        Push minimum and maximum pixel values of the top image.
        i   i    N(   R   t   extremaR   (   R   RA   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_getextremaâ   s    c         C  sM   t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } |  j ƒ  } |  j | j | | ƒ ƒ d S(   sl   usage: offset <int:xoffset> <int:yoffset> <image:pic1>

        Offset the pixels in the top image.
        N(   R   R   R   t   offset(   R   t   xofft   yoffR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_offsetë   s    c         C  sŽ   |  j  ƒ  } t |  j  ƒ  ƒ } t |  j  ƒ  ƒ } |  j  ƒ  } | j d k rg | j | | | f | ƒ n | j | | | f ƒ |  j | ƒ d S(   s±   usage: paste <image:figure> <int:xoffset> <int:yoffset>
                        <image:ground>

        Paste figure image into ground with upper left at given offsets.
        t   RGBAN(   R   R   R(   t   pasteR   (   R   t   figureRD   RE   t   ground(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_pasteõ   s    c         C  sP   t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } |  j ƒ  } |  j | j | | f ƒ ƒ d S(   sZ   usage: resize <int:xsize> <int:ysize> <image:pic1>

        Resize the top image.
        N(   R   R   R   t   resize(   R   R   R   R-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_resize  s    c         C  s8   t  |  j ƒ  ƒ } |  j ƒ  } |  j | j | ƒ ƒ d S(   s[   usage: rotate <int:angle> <image:pic1>

        Rotate image through a given angle
        N(   R   R   R   t   rotate(   R   t   angleR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_rotate  s    c         C  s)   |  j  ƒ  } |  j  ƒ  } | j | ƒ d S(   s]   usage: save <string:filename> <image:pic1>

        Save image with default options.
        N(   R   t   save(   R   t   filenameR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_save  s    c         C  s;   |  j  ƒ  } |  j  ƒ  } |  j  ƒ  } | j | d | ƒ d S(   sq   usage: save2 <string:filename> <string:options> <image:pic1>

        Save image with specified options.
        N(   R   RQ   t   None(   R   RR   t   optionsR-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_save2!  s    c         C  s   |  j  ƒ  j ƒ  d S(   sI   usage: show <image:pic1>

        Display and pop the top image.
        N(   R   t   show(   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_show+  s    c         C  sA   t  |  j ƒ  ƒ } t  |  j ƒ  ƒ } |  j ƒ  j | | f ƒ d S(   s‹   usage: thumbnail <int:xsize> <int:ysize> <image:pic1>

        Modify the top image in the stack to contain a thumbnail of itself.
        N(   R   R   R   t	   thumbnail(   R   R   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_thumbnail2  s    c         C  s8   |  j  ƒ  j ƒ  } |  j  ƒ  } |  j | j | ƒ ƒ d S(   sZ   usage: transpose <string:operator> <image:pic1>

        Transpose the top image.
        N(   R   R2   R   t	   transpose(   R   R[   R-   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_transpose;  s    c         C  s   |  j  |  j ƒ  j ƒ d S(   s]   usage: format <image:pic1>

        Push the format of the top image onto the stack.
        N(   R   R   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_formatF  s    c         C  s   |  j  |  j ƒ  j ƒ d S(   sY   usage: mode <image:pic1>

        Push the mode of the top image onto the stack.
        N(   R   R   R(   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_modeM  s    c         C  s5   |  j  ƒ  j } |  j | d ƒ |  j | d ƒ d S(   sV   usage: size <image:pic1>

        Push the image size on the stack as (y, x).
        i    i   N(   R   t   sizeR   (   R   R_   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_sizeT  s    c         C  s0   d d l  m } |  j | j |  j ƒ  ƒ ƒ d S(   sB   usage: invert <image:pic1>

        Invert the top image.
        iÿÿÿÿ(   t
   ImageChopsN(   R9   Ra   R   t   invertR   (   R   Ra   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_invert_  s    c         C  sE   d d l  m } |  j ƒ  } |  j ƒ  } |  j | j | | ƒ ƒ d S(   s   usage: lighter <image:pic1> <image:pic2>

        Pop the two top images, push an image of the lighter pixels of both.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   t   lighter(   R   Ra   R   R    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt
   do_lighterg  s    c         C  sE   d d l  m } |  j ƒ  } |  j ƒ  } |  j | j | | ƒ ƒ d S(   s}   usage: darker <image:pic1> <image:pic2>

        Pop the two top images, push an image of the darker pixels of both.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   t   darker(   R   Ra   R   R    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_darkerq  s    c         C  sE   d d l  m } |  j ƒ  } |  j ƒ  } |  j | j | | ƒ ƒ d S(   so   usage: difference <image:pic1> <image:pic2>

        Pop the two top images, push the difference image
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   t
   difference(   R   Ra   R   R    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_difference{  s    c         C  sE   d d l  m } |  j ƒ  } |  j ƒ  } |  j | j | | ƒ ƒ d S(   sr   usage: multiply <image:pic1> <image:pic2>

        Pop the two top images, push the multiplication image.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   t   multiply(   R   Ra   R   R    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_multiply…  s    c         C  sE   d d l  m } |  j ƒ  } |  j ƒ  } |  j | j | | ƒ ƒ d S(   sv   usage: screen <image:pic1> <image:pic2>

        Pop the two top images, superimpose their inverted versions.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   t   screen(   R   Ra   R    R   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt	   do_screen  s    c         C  so   d d l  m } |  j ƒ  } |  j ƒ  } t |  j ƒ  ƒ } t |  j ƒ  ƒ } |  j | j | | | | ƒ ƒ d S(   s   usage: add <image:pic1> <image:pic2> <int:offset> <float:scale>

        Pop the two top images, produce the scaled sum with offset.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   R   R   t   add(   R   Ra   R   R    t   scaleRC   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_add™  s    c         C  so   d d l  m } |  j ƒ  } |  j ƒ  } t |  j ƒ  ƒ } t |  j ƒ  ƒ } |  j | j | | | | ƒ ƒ d S(   s™   usage: subtract <image:pic1> <image:pic2> <int:offset> <float:scale>

        Pop the two top images, produce the scaled difference with offset.
        iÿÿÿÿ(   Ra   N(   R9   Ra   R   R   R   R   t   subtract(   R   Ra   R   R    Ro   RC   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_subtract¥  s    c         C  sW   d d l  m } t |  j ƒ  ƒ } |  j ƒ  } | j | ƒ } |  j | j | ƒ ƒ d S(   sK   usage: color <image:pic1>

        Enhance color in the top image.
        iÿÿÿÿ(   t   ImageEnhanceN(   R9   Rs   R   R   t   ColorR   t   enhance(   R   Rs   t   factorR-   t   enhancer(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_color³  s
    c         C  sW   d d l  m } t |  j ƒ  ƒ } |  j ƒ  } | j | ƒ } |  j | j | ƒ ƒ d S(   sQ   usage: contrast <image:pic1>

        Enhance contrast in the top image.
        iÿÿÿÿ(   Rs   N(   R9   Rs   R   R   t   ContrastR   Ru   (   R   Rs   Rv   R-   Rw   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_contrast¾  s
    c         C  sW   d d l  m } t |  j ƒ  ƒ } |  j ƒ  } | j | ƒ } |  j | j | ƒ ƒ d S(   sU   usage: brightness <image:pic1>

        Enhance brightness in the top image.
        iÿÿÿÿ(   Rs   N(   R9   Rs   R   R   t
   BrightnessR   Ru   (   R   Rs   Rv   R-   Rw   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_brightnessÉ  s
    c         C  sW   d d l  m } t |  j ƒ  ƒ } |  j ƒ  } | j | ƒ } |  j | j | ƒ ƒ d S(   sS   usage: sharpness <image:pic1>

        Enhance sharpness in the top image.
        iÿÿÿÿ(   Rs   N(   R9   Rs   R   R   t	   SharpnessR   Ru   (   R   Rs   Rv   R-   Rw   (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   do_sharpnessÔ  s
    c         C  sÅ   | j  ƒ  x´ t | ƒ d k rÀ |  j | d ƒ | d } |  j r] t d t |  j ƒ ƒ n  |  j ƒ  } t | t	 ƒ s~ q n  d | } t
 |  | ƒ s q q |  j ƒ  t |  | ƒ } | ƒ  q Wd S(   s'   Interpret a list of PILDriver commands.i    i   s   Stack: t   do_N(   t   reverset   lenR   R   t   printt   reprR   R   t
   isinstancet   strR   R   R:   (   R   t   listR   t   funcnamet   func(    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   executeá  s    

	

(0   t   __name__t
   __module__R   R   R   R   R   R   R   R   R   R   R   R"   R%   R+   R.   R/   R5   R7   R=   R@   RB   RF   RK   RM   RP   RS   RV   RX   RZ   R\   R]   R^   R`   Rc   Re   Rg   Ri   Rk   Rm   Rp   Rr   Rx   Rz   R|   R~   R‰   (    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyR   8   s\   										
		
	
						
	
				
		
					
									
	
	
	
	
						t   __main__Ni   i    s   PILDriver says hello.i   s   pildriver> s   
PILDriver says goodbye.(   t   __doc__t
   __future__R    R9   R   t   objectR   RŠ   t   syst   driverR   t   argvR‰   R‚   t   Truet   version_infot   inputt   linet	   raw_inputt   EOFErrort   splitR   (    (    (    sE   /tmp/pip-unpacked-wheel-ivxKtu/Pillow-3.4.2.data/scripts/pildriver.pyt   <module>)   s(   
ÿ ½	
	
