【问题标题】:Convert text file to tiff file in python在python中将文本文件转换为tiff文件
【发布时间】:2019-10-23 16:54:11
【问题描述】:

我正在使用以下代码将文本文件转换为 tiff,但是当文本文件内容以特殊字符开头时它不起作用。我不知道为什么它不起作用。你能请任何人帮我做这个任务吗

def main():
    image = text_image('/Users/administrator/Desktop/367062657_1.text')
    image.show()
    image.save('contentok.tiff')

def text_image(text_path, font_path=None):

    grayscale = 'L'
    # parse the file into lines
    with open(text_path) as text_file:
        lines = tuple(l.rstrip() for l in text_file.readlines())

    large_font = 20
    font_path = font_path or 'cour.ttf'  
    try:
        font = PIL.ImageFont.truetype(font_path, size=large_font)
    except IOError:
        font = PIL.ImageFont.load_default()
        print('Could not use chosen font. Using default.')
    pt2px = lambda pt: int(round(pt * 96.0 / 72))
    max_width_line = max(lines, key=lambda s: font.getsize(s)[0])
    test_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    max_height = pt2px(font.getsize(test_string)[1])
    max_width = pt2px(font.getsize(max_width_line)[0])
    height = max_height * len(lines) # perfect or a little oversized
    width = int(round(max_width + 5))  # a little oversized

    image = PIL.Image.new(grayscale, (width, height), color=PIXEL_OFF)
    draw = PIL.ImageDraw.Draw(image)
    vertical_position = 5
    horizontal_position = 5
    line_spacing = int(round(max_height * 1.0))
    for line in lines:
        draw.text((horizontal_position, vertical_position),
                  line, fill=PIXEL_ON, font=font)

        vertical_position += line_spacing
    c_box = PIL.ImageOps.invert(image).getbbox()
    image = image.crop(c_box)`enter code here`
    return image

错误:

错误: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf2 in 位置 18:无效的继续字节

【问题讨论】:

  • “不工作”是什么意思?有错误吗?文件是否生成但为空?它看起来与您的预期不同吗?
  • 错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf2 in position 18: invalid continuation byte

标签: python django python-3.x tiff


【解决方案1】:

你的问题在这里:

with open(text_path) as text_file:
    lines = tuple(l.rstrip() for l in text_file.readlines())

根据您在 cmets 中提到的错误,当其中的数据与 UTF-8 不兼容时,您正在将文本文件加载为文本(默认为 UTF-8)。

您应该使用与数据匹配的指定编码打开文件。见docs here

基本上这样的事情应该可以工作:

with open(text_path, encoding='windows-1255') as text_file:
    lines = tuple(l.rstrip() for l in text_file.readlines())

当然,windows-1255 只是我的一个猜测……你应该知道你的文件是如何编码的,take a look here 是一个可用值的列表

【讨论】:

  • 现在它正在生成这个错误:返回 self.font.getsize(text) UnicodeEncodeError: 'latin-1' codec can't encode character '\u05e2' in position 18: ordinal not in range(第256章)
  • @Gopal 再次,我不知道您的文件是如何编码的,其中包含哪些数据等。关键是您可能应该在相关函数中指定编码(或将所有内容都转换为 UTF-8在你开始之前)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 2016-11-24
  • 1970-01-01
  • 2020-06-12
  • 2020-11-14
  • 1970-01-01
  • 2020-01-03
相关资源
最近更新 更多