PIL convert picture leads to bad result

i take a photo via a DSLR, then ps it, and want to resize it using PIL. here is the core code

image = Image.open(img_obj, 'r')
for pic_size_name, pic_size_val in pic_sizes.items():
    width, height = [int(item) for item in pic_size_val.split('x')]
    img_width, img_height = image.size
    pic_save_path = os.path.join(
                        hash_val + '_' + pic_size_name + '.jpg'

    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGBA')

    if width > img_width and height > img_height:
        image.save(pic_save_path, "jpeg", quality=90)

    img = image.copy()
    if pic_size_name == 's' or pic_size_name == 'xs':
        dest_ratio = float(width) / height
        current_ratio = float(img_width) / img_height
        if dest_ratio > current_ratio:
            offset = int((img_height - img_width / dest_ratio) / 2)
            box = (0, offset, img_width, img_height - offset)
            offset = int((img_width - img_height * dest_ratio) / 2)
            box = (offset, 0, img_width - offset, img_height)
        img = img.crop(box)
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg", quality=90)
    elif pic_size_name == 'm':
        new_height = img_height * width / img_width
        img = img.resize((width, new_height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg", quality=90)
        img.thumbnail((width, height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg")

but the resize result is not so good.

this is converted by PIL:


this is converted by Flickr which is what should be:


am I using PIL wrong or there is some trick i don't know?


WHen downloading and looking at both images in GIMP, however, the difference is that the one in Flickr sported an embedded Color Profile, while the one generated by PIL did not. As I don't notice any contrast or sharpness differences, I suppose the resulting color difference is what you are troubled about.

You have to make your PIL workflow ppreservign any color profiles associated with the image - a quick google search brings up pyCMS which has got 4-5 lines examples in its frontpage. Most likely pyCMS will be what you are asking for:


Need Your Help

Retrieve specific mysql result row

php mysql function

I want to select all entries in a MySQL table, and randomly display a number of them.

For how long before standardisation was `string` available?

c++ history

C++ was formally standardised in 1998, but how far back can we find a class named string that looks like std::string does in C++2003 in a pre-standard C++ implementation?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.