从String中写入python中的TIFF文件 [英] Write TIFF file in python from String

查看:120
本文介绍了从String中写入python中的TIFF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法从python中的String中轻松编写压缩的TIFF文件。我已经看过Python成像库 PIL 但是我需要编写一个非常具体的TIFF格式和PIL目前仅支持未压缩。我需要编写一个LZW压缩条纹TIFF,其中只有一行简单的文本。我宁愿不必从头开始写东西,但是如果我这样做的话。

I need a way to easily write a compressed TIFF file from a String in python. I already look at the Python Imaging Library PIL But I need to write a very specific TIFF format and PIL only supports uncompressed at the moment. I need to write a LZW compressed, striped TIFF with just one simple line of text in it. I would rather not have to write something from scratch, but if I do I do.

推荐答案

我在这里使用过这段代码过去,所以我可以说它有效。此脚本来自 1997 ,PIL还有 来实现压缩的TIFF写入。

I've used this code in the past, so I can say that it works. This script is from 1997, and PIL has yet to implement compressed TIFF writing.

# 
# use "tiffcp" to write compressed TIFF files.
#
# fredrik lundh (may 13, 1997)
#

import os, tempfile

# install standard driver
import Image, TiffImagePlugin 

LZW      = "lzw"
ZIP      = "zip"
JPEG     = "jpeg"
PACKBITS = "packbits"
G3       = "g3"
G4       = "g4"

def _save(im, fp, filename):

    # check compression mode
    try:
    compression = im.encoderinfo["compression"]
    except KeyError:
    # use standard driver
    TiffImagePlugin._save(im, fp, filename)
    else:
    # compress via temporary file
    if compression not in (LZW, ZIP, JPEG, PACKBITS, G3, G4):
        raise IOError, "unknown compression mode"
    file = tempfile.mktemp()
    im.save(file, "TIFF")
    os.system("tiffcp -c %s %s %s" % (compression, file, filename))
    try: os.unlink(file)
    except: pass

Image.register_save(TiffImagePlugin.TiffImageFile.format, _save)

if __name__ == "__main__":
    # test
    im = Image.open("/usr/iv/tip/images/lenna.ppm")
    im = im.point(lambda v: v >= 128 and 255, "1")
    im.save("lenna.tif", compression=G4)

此代码(afaict)只增加了使用标准PIL库编写压缩TIFF的功能,因此如果您已将文本写入PIL Image object,它应该很容易实现。

This code (afaict) just adds the ability to write compressed TIFFs using the standard PIL library, so if you've written your text to a PIL Image object, it should be really easy to implement.

这篇关于从String中写入python中的TIFF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆