lunes, 20 de octubre de 2008

Fix low contrast photos using python (PIL)

In this post i explain how to fix low contrast images using the equalization functions of the python image library PIL.

Suppose that you have a low contrast image like this one:


Now lets equalize it using python:

import Image,import ImageOps

im = Image.open('lowContrast.jpg')
im_eq = ImageOps.equalize(im)
im_eq.save('lowContrast_eq.jpg')


And the result is:


Now suppose that you have the same problem but only in one of the channels. Like in this one that has low contrast in the blue band.


No problem:

import Image,import ImageOps

im = Image.open('colorProblem.jpg')
r,g,b = im.split()
b = ImageOps.equalize(b)
im_e = Image.merge('RGB', (r,g,b))
im_e.save('colorProblem_beq.jpg')


The result:

domingo, 19 de octubre de 2008

force download django

When you serve a document,image,file from a Web server, you might want to immediately prompt the user to save the file directly to the user's disk, without opening it in the browser.

You can use the content-disposition header to override the default behavior.

The code below shows how-to do this in python/django:

import mimetypes, os

def download_view(request, filename):
    file = open(filename,"r")
    mimetype = mimetypes.guess_type(filename)[0]
    if not mimetype: mimetype = "application/octet-stream"

    response = HttpResponse(file.read(), mimetype=mimetype)
    response["Content-Disposition"]= "attachment; filename=%s" % os.path.split(filename)[1]
    return response

jueves, 13 de diciembre de 2007

Change title, description and other meta tags on Joomla 1.5

Today when i was finishing a component for Joomla 1.5, i had to change the title and the meta info generated by Joomla, i was unable to find how to do this on the Joomla site or google, so i had to read the source code to find out. At the moment this is particular useful because there is no SEO extension available for joomla 1.5

Well how we do this? first get the document object:

$document =& JFactory::getDocument();

Second use this document methods to change the title, meta description or any other meta. Be aware that change the description must be done with setDescription if you use setMetaData you will get the meta description twice, the original and the new one.


/**
* Sets the title of the document
*
* @param string $title
* @access public
*/
function setTitle($title)


/**
* Sets or alters a meta tag.
*
* @param string $name Value of name or http-equiv tag
* @param string $content Value of the content tag
* @param bool $http_equiv META type "http-equiv" defaults to null
* @return void
* @access public
*/
function setMetaData($name, $content, $http_equiv = false)


/**
* Sets the description of the document
*
* @param string $title
* @access public
*/
function setDescription($description)


Example:
$document =& JFactory::getDocument();
$document->setTitle($document->getTitle() . ' | ' . "testTitle");
$document->setMetaData("keywords", "key1, key2");
$document->setDescription("a description");


Example output:


<title>sitename | testTitletitle></title>
.....
<meta name="keywords" content="key1, key2"/>
.....
<meta name="description" content="a description"/>


Well this is my first entry in this Blog, I pretend do this kind of post, mini HOWTOs about particular things i learn this day.
I hope this info be useful, See You Soon!