limboy
limboy

Reputation: 4089

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(
                        save_path,
                        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)
        continue

    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)
        else:
            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)
    else:
        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:

http://cl.ly/CgR9

this is converted by Flickr which is what should be:

http://www.flickr.com/photos/lzyy/6524414285/sizes/z/in/photostream/

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

Upvotes: 1

Views: 701

Answers (1)

jsbueno
jsbueno

Reputation: 110696

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:

http://www.cazabon.com/pyCMS/

Upvotes: 2

Related Questions