Most viewed recipes tagged "pil"http://code.activestate.com/recipes/tags/pil/views/2017-04-10T21:14:36-07:00ActiveState Code RecipesConversion of PIL Image and numpy array (Python)
2011-03-05T03:47:56-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/
<p style="color: grey">
Python
recipe 577591
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>The utility function that converts PIL image to numpy array and vice versa.</p>
Random Maze Generator (Python)
2012-12-14T09:09:22-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578356-random-maze-generator/
<p style="color: grey">
Python
recipe 578356
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>Random maze generator using depth-first search.</p>
<p>It starts the maze path from a random cell and there is no exit defined but actually any 2 cells on the path (white cells) can be assigned to be entry and exit locations. (I could just add code to randomly select 2 white cells and change their colors to something else but I thought it looks better this way.)</p>
Draw Text To Image (Python)
2015-07-13T18:04:30-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579013-draw-text-to-image/
<p style="color: grey">
Python
recipe 579013
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>PIL does not allow resizing default bitmap font.
This code resizes it by itself.</p>
<p>The disadvantage is slow speed but the advantage is it becomes possible to set color of each pixel of text foreground and background.</p>
<p>As an example I set foreground to reverse colors and background to grayscale.</p>
EXIF-date-based JPEG files rename using PIL (Python)
2009-02-10T07:28:48-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576646-exif-date-based-jpeg-files-rename-using-pil/
<p style="color: grey">
Python
recipe 576646
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/exif/">exif</a>, <a href="/recipes/tags/jfif/">jfif</a>, <a href="/recipes/tags/jpeg/">jpeg</a>, <a href="/recipes/tags/jpg/">jpg</a>, <a href="/recipes/tags/photo/">photo</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/rename/">rename</a>).
Revision 4.
</p>
<p>Rename JPEG files according to EXIF-date using PIL [library].</p>
<p>If global variable CREATE_HARDLINK is set, script creates Windows (XP) batch file
for creating hardlink version of source files.</p>
<p>PIL available here: <a href="http://www.pythonware.com/products/pil/" rel="nofollow">http://www.pythonware.com/products/pil/</a></p>
Image to ASCII Art Converter (Python)
2016-09-25T21:24:12-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580702-image-to-ascii-art-converter/
<p style="color: grey">
Python
recipe 580702
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>ASCII Art Generator (Image to ASCII Art Converter)</p>
<p>Input file maybe JPG, PNG, GIF etc.
Output file name maybe output.txt etc.</p>
Planet Terrain Heightmap Generator (Python)
2010-03-10T16:35:37-08:00Shea Kauffmanhttp://code.activestate.com/recipes/users/4168682/http://code.activestate.com/recipes/576929-planet-terrain-heightmap-generator/
<p style="color: grey">
Python
recipe 576929
by <a href="/recipes/users/4168682/">Shea Kauffman</a>
(<a href="/recipes/tags/creation/">creation</a>, <a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/heightmap/">heightmap</a>, <a href="/recipes/tags/landscape/">landscape</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/planet/">planet</a>, <a href="/recipes/tags/planted/">planted</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/terrain/">terrain</a>, <a href="/recipes/tags/world/">world</a>).
Revision 9.
</p>
<p>The process is simply:
1. Take a plane
2. Cut out a shape
3. Make it a little taller
4. Repeat</p>
<ul>
<li>Similar to the <a href="http://freespace.virgin.net/hugo.elias/models/m_landsp.htm">spherical landscape</a> algorithm by Hugo Elias.</li>
<li>I found a combination of Ovals and Triangles to produce the best results.</li>
</ul>
JPG files redater by EXIF data (Python)
2014-12-11T10:07:39-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/550811-jpg-files-redater-by-exif-data/
<p style="color: grey">
Python
recipe 550811
by <a href="/recipes/users/186902/">Michal Niklas</a>
(<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/exif/">exif</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/jpeg/">jpeg</a>, <a href="/recipes/tags/jpg/">jpg</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 6.
</p>
<p>Iterates through a directory, reading the EXIF data from each jpg/jpeg file.
Parses the date/time from EXIF data and:</p>
<ol>
<li>if it differs from file modification date/time then changes file date/time</li>
<li>moves file to <code>YYYY/YYYY_MM_DD</code> directory</li>
</ol>
Dynamical Billiards Simulation (Python)
2010-10-30T06:22:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577445-dynamical-billiards-simulation/
<p style="color: grey">
Python
recipe 577445
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>It simulates reflections of a ball on a billiards table that has one or more circular obstacles.
(This can also be thought as a 2d ray-tracing.)</p>
<p>Most of the time the path of the ball would be chaotic (meaning, if another ball started from any slightly different location or direction then its path would be very different after a short while). </p>
<p>See Wikipedia for more info:
<a href="http://en.wikipedia.org/wiki/Dynamical_billiards" rel="nofollow">http://en.wikipedia.org/wiki/Dynamical_billiards</a></p>
Image Projection Onto Sphere (Python)
2016-07-31T22:50:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580695-image-projection-onto-sphere/
<p style="color: grey">
Python
recipe 580695
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/imaging/">imaging</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Image projection onto sphere using Equirectangular map projection.</p>
<p>Download the test image from the Wikipedia page!</p>
Perlin Noise Generator (Python)
2013-02-23T05:23:24-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578470-perlin-noise-generator/
<p style="color: grey">
Python
recipe 578470
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>Perlin Noise Generator using Bilinear Interpolation.</p>
Metro Spinner for Tkinter (Python)
2017-04-10T21:14:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580777-metro-spinner-for-tkinter/
<p style="color: grey">
Python
recipe 580777
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/canvas/">canvas</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/rotation/">rotation</a>, <a href="/recipes/tags/spinner/">spinner</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>I create a rotating image class <em>RotatingIcon</em> inspired and based on this code:</p>
<p><a href="http://stackoverflow.com/questions/15736530/python-tkinter-rotate-image-animation" rel="nofollow">http://stackoverflow.com/questions/15736530/python-tkinter-rotate-image-animation</a></p>
<p>Features:</p>
<ul>
<li>Methods to stop and start the animation</li>
<li>The animation automically stops when the window is not mapped, and the animation continues when the window is mapped again</li>
<li>Time setting to control the speed of the animation</li>
<li>All the formats accepted for PIL could be used. XBM format is automatically converted to Tk Bitmap. The advantage of Bitmats is the possibility to change the color of the foreground.</li>
</ul>
<p>I added 6 different styles of spinners with different sizes.</p>
<p>I used fontawesome.io for the icon generation.</p>
<p>For more metro widgets see here:</p>
<p><a href="https://code.activestate.com/recipes/580729-metro-ui-tkinter/" rel="nofollow">https://code.activestate.com/recipes/580729-metro-ui-tkinter/</a></p>
Bilinear Interpolation (Python)
2013-02-23T04:00:52-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578469-bilinear-interpolation/
<p style="color: grey">
Python
recipe 578469
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Creates a surface from a grid of random numbers using Bilinear Interpolation.</p>
True-color Mandelbrot Fractal (Python)
2013-01-14T02:07:47-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578415-true-color-mandelbrot-fractal/
<p style="color: grey">
Python
recipe 578415
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>True-color Mandelbrot Fractal.
It uses |x|, |y|, |z|, |angle(z)| values of the final iteration point for coloring.</p>
Pad images to power-of-two dimensions. (Python)
2008-08-12T18:46:40-07:00Martin Wilsonhttp://code.activestate.com/recipes/users/4166376/http://code.activestate.com/recipes/576416-pad-images-to-power-of-two-dimensions/
<p style="color: grey">
Python
recipe 576416
by <a href="/recipes/users/4166376/">Martin Wilson</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/imaging/">imaging</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/opengl/">opengl</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/power/">power</a>, <a href="/recipes/tags/power_of_two/">power_of_two</a>, <a href="/recipes/tags/python_imaging_library/">python_imaging_library</a>, <a href="/recipes/tags/two/">two</a>).
Revision 4.
</p>
<p>Pads any amount of images each to power-of-two dimensions, useful for OpenGL programming.</p>
Random Multi-Maze Generator (Python)
2012-12-14T11:23:29-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578378-random-multi-maze-generator/
<p style="color: grey">
Python
recipe 578378
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Randomly creates mazes which have multiple paths to solve.
Entry and exit points for each path can be assigned arbitrarily.
(I never seen this kind of maze anywhere before but I think calling them multi-maze should be okay.)</p>
Putting watermark to images in batch (Python)
2009-06-20T18:05:08-07:00hasanatkazmihttp://code.activestate.com/recipes/users/4168973/http://code.activestate.com/recipes/576818-putting-watermark-to-images-in-batch/
<p style="color: grey">
Python
recipe 576818
by <a href="/recipes/users/4168973/">hasanatkazmi</a>
(<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/watermark/">watermark</a>).
</p>
<p>Being a novice photographer, I usually have many pictures - sometimes in hundreds. While posting on Flickr or other online sources, it is very important to put watermark on all of them, like signature or email.
This is a small self explanatory snippet which will place watermark image on right bottom corner. You just provide source folder and it will scan all photos in that folder (not sub folders) and will produce watermarked photos in the destination folder.
It worked for me with jpg, it should work for all those formats which PIL can work with.</p>
Write a PNG image in native Python (Python)
2010-10-28T12:03:52-07:00Campbell Bartonhttp://code.activestate.com/recipes/users/4168177/http://code.activestate.com/recipes/577443-write-a-png-image-in-native-python/
<p style="color: grey">
Python
recipe 577443
by <a href="/recipes/users/4168177/">Campbell Barton</a>
(<a href="/recipes/tags/alpha/">alpha</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/save/">save</a>).
</p>
<p>This is a simple PNG writing function, intended to be as small as possible.
It only supports RGBA 32bit PNGs and the input should be a buffer of rgba bytes.</p>
<p>Tested for python 2 and 3.</p>
mouse click to crop many large photos quickly [Python, PIL, pygame] (Python)
2012-01-09T03:18:01-08:00Elliot Hallmarkhttp://code.activestate.com/recipes/users/4180468/http://code.activestate.com/recipes/578005-mouse-click-to-crop-many-large-photos-quickly-pyth/
<p style="color: grey">
Python
recipe 578005
by <a href="/recipes/users/4180468/">Elliot Hallmark</a>
(<a href="/recipes/tags/crop/">crop</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/jpeg/">jpeg</a>, <a href="/recipes/tags/mouse/">mouse</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/png/">png</a>).
</p>
<p>Waste no mouse clicks making multiple crops on many image files. Through pygame interface with pan, zoom and next/previous image. Saves files at new resolution and serialized names in a seperate folder. the mainloop() and helper functions are easy to reuse, but I include a cruddy text based interface if needed. if not, comment out most of __main__().</p>
<p>Requires PIL (python imaging library) and pygame.</p>
Image Converter (Python)
2015-01-18T19:14:38-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579007-image-converter/
<p style="color: grey">
Python
recipe 579007
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Converts an image from one format to another using PIL.</p>
Knight's Tour Map using Warnsdorff's Algorithm (Python)
2012-12-18T01:05:51-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578386-knights-tour-map-using-warnsdorffs-algorithm/
<p style="color: grey">
Python
recipe 578386
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Solves Knight's Tour Problem using Warnsdorff's Algorithm for every square on the chessboard of arbitrary size.</p>
<p>It colors each initial square with a color that depends on the final square coordinates.
(This is a method used to create fractals.)
Many different formulas maybe used for coloring (based on abs/rel x/y/dist/ang).</p>
<p>Calculating each tour in full creates a highly chaotic image (and takes hours).
So I added maxItPercent to cut-off tours earlier.
(Using %10 creates an image that has both ordered and chaotic regions.)</p>