Python PIL - 切り抜き、拡大縮小、保存時にカラープロファイルをタグなしRGBに変更する 質問する

Python PIL - 切り抜き、拡大縮小、保存時にカラープロファイルをタグなしRGBに変更する 質問する

PIL で切り抜き、拡大縮小、保存するとドキュメント プロファイルが変更される理由がわかりません。カラー プロファイルとして sRGB を持つイメージと、RGB のタグが解除されたイメージでテストしました。

def scale(self, image):
    images = []

    image.seek(0)

    try:
        im = PIL.open(image)
    except IOError, e:
        logger.error(unicode(e), exc_info=True)

    images.append({"file": image, "url": self.url, "size": "original"})

    for size in IMAGE_WEB_SIZES:
        d = cStringIO.StringIO()
        try:
            im = crop(image, size["width"], size["height"])
            im.save(d, "JPEG")
            images.append({"file": d, "url": self.scale_url(size["name"]), "size": size})
        except IOError, e:
            logger.error(unicode(e), exc_info=True)
            pass

    return images

PIL を使用して、元の画像と同じカラー プロファイルでスケールされたバージョンを保存しようとしています。

編集:これによるとそれは可能であるはずだhttp://comments.gmane.org/gmane.comp.python.image/3215しかし、PIL 1.1.7を使用してもまだ動作しません

ベストアンサー1

PIL には icc_profile を読み取る関数と、icc_profile で保存する方法があります。そこで、ファイルを開いて icc_profile を取得しました。

try:
    im1 = PIL.open(image)
    icc_profile = im1.info.get("icc_profile")

保存時に再度ファイルに追加します。

im.save(d, "JPEG", icc_profile=icc_profile)

完全なコードは次のとおりです。

def scale(self, image):
    images = []

    image.seek(0)

    try:
        im1 = PIL.open(image)
        icc_profile = im1.info.get("icc_profile")
    except IOError, e:
        logger.error(unicode(e), exc_info=True)

    images.append({"file": image, "url": self.url, "size": "original"})

    for size in IMAGE_WEB_SIZES:
        d = cStringIO.StringIO()
        try:
            im = crop(image, size["width"], size["height"])

            im.save(d, "JPEG", icc_profile=icc_profile)
            images.append({"file": d, "url": self.scale_url(size["name"]), "size": size})
        except IOError, e:
            logger.error(unicode(e), exc_info=True)
            pass

    return images

タグ付き(ICC プロファイル付き)とタグなしの jpeg 画像の両方でテストしました。

おすすめ記事