Login

Tag "pil"

Snippet List

Yet Another Image Resizer

There's a whole range of examples online for resizing images in Django some of which are incredibly comprehensive with a wide variety of options. Here's my take on the task that serves as a simple drop in for when you don't want to include a separate app. - Only generates the resized image when first requested. - Handles maintaining proportions when specifying only a width or a height. - Makes use of PIL.ImageOps.fit for cropping without reinventing the wheel.

  • template
  • tag
  • image
  • pil
  • thumbnail
  • resize
Read More

Load Windows ICO files

PIL IcoImagePlugin is twelve year old and it can't handle recent Windows ICO files. Here is a function that handles all ICO versions and preserve transparency. Usage: # Load biggest icon from file image = load_icon('icon.ico') # Save third icon as PNG load_icon('icon.ico', 2).save('icon.png')

  • image
  • pil
  • ico
Read More
Author: dc
  • 2
  • 2

Image resize on demand

** Image on demand view ** I often post photos on photography fora. Most fora want you to place a link to a photo somewhere on the net, but different fora have different rules. Some fora want you to stick to a maximum of 800 pixels wide, some 700 pixel and some even strange values like 639 pixels. My own site uses 600 pixels so I end up resizing images all the time. Since I keep my originals with my gallery as well (hidden for public viewing) resizing on the fly would be a nice asset. I'm using my previous snippet to apply a slight unsharp mask for better web display of my photos. ** usage ** This snippet takes the url to my photo application which is a simple link using the pk of my photo table and adds 'width'.jpg to the end (some fora check if the link is an image based on extenstion) The view takes the width requested and creates the resized image from the original full size image or takes it from the cache for display on demand. To prevent a dozen directories I use a setting to specify which widths are allowed, providing room for several versions of the same image. Any improvements are appreciated since I'm still rather inexperienced in Python and Django.

  • image
  • pil
  • resize
  • pythonmagick
Read More

Unsharp Mask with PIL and PythonMagick

**A Magick PIL** I used to do my image conversions with ImageMagick and system calls back in my PHP days. With Django PIL is the obvious choice for most image stuff, but frustrated by the lack of a proper unsharp mask function for PIL I found some code in the bits and pieces of documentation for PythonMagick. (yes I know, Kevin Cabazon wrote PIL_usm, but I could not get it to work, probably due to my inexperience. Anyway, this code makes it easy to convert back and forth from PIL to PythonMagick (maybe not such a good idea on a memory tight high loaded production server, but no problem on my private server (Pentium-M @ 1.8 Ghz with 1 GB Mem.) **usage:** usm takes a PIL image object. Radius and sigma is in pixels, amount 1 compares to 100% in photoshop, threshold 0.004 ~ (1/256) compares to 1 in photoshop: I'm using r=1,s=0.5,a=0.8,t=0.016 for roughly 800x600 images created from 3000x2000 (6MP) images. Experiment for your own preferences.

  • image
  • pil
  • sharpen
  • thumbnails
  • pythonmagick
  • usm
Read More

Dynamic image generator

This is the complete image_processor.py module, allowing you to add an image containing an arbitrary piece of text. I use this to label the horizontal axis of a skills-matrix report. Credit www.renewtek.com for paying me to do this stuff!

  • image
  • pil
Read More

resize to thumbnail with scale-to-fill

**So you can upload rectangular pictures but still have square thumbnails.** "Scale to **fill**" instead of the out of the box "scale to **fit**" you get with `Image.thumbnail`

  • image
  • pil
  • thumbnail
  • resize
  • imagefield
Read More

Resize image on save

This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save). In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.

  • image
  • pil
  • resize
Read More

ImageURLField for forms

A URL field specifically for images, which can validate details about the filesize, dimensions and format of an image at a given URL, without having to read the entire image into memory. Requires [Python Imaging Library](http://www.pythonware.com/library/pil/). *4th October, 2008* - updated for 1.0 compatibility.

  • image
  • pil
  • validation
  • url
  • form
  • field
Read More

Function to create resized versions of an image from a URL and saving it to a local path

This is a really useful function I used to create the resized preview images you can see on the [homepage of my site](http://www.obeattie.com/). Basically, it takes the original URL of an image on the internet, creates a resized version of that image by fitting it into the constraints specified (doesn't distort the image), and saves it to the MEDIA_ROOT with the filename you specify. For example, I use this by passing it the URL of a Flickr Image and letting it resize it to the required size, and then saving it to a local path. It then returns the local path to the image, should you need it. I however just construct a relative URL from the image_name and save that to the database. This way, it's easy to display this image. Hope it's useful!

  • image
  • pil
  • thumbnail
  • resize
Read More

Sharpening images

Returns a sharpened copy of an image. Resizing down or thumbnailing your images with PIL.Image tends to make them blurry. I apply this snippet to such images in order to regain some sharpness.

  • image
  • pil
  • sharpen
  • thumbnail
Read More

12 snippets posted so far.