Python编码:打开/读取图像文件,解码图像,重新编码Imag

2024-04-25 05:07:10 发布

您现在位置:Python中文网/ 问答频道 /正文

注:我对编码/解码不太了解,但在遇到这个问题之后,这些词对我来说已经完全是行话了。

问题: 我有点困惑。我在对图像进行编码/解码,将图像存储为django模型中的TextField,环顾堆栈溢出,我发现我可以从ascii中解码图像(我想还是二进制的?无论open('file', 'wb')使用什么作为编码。我假设默认的ascii)为latin1,并将其存储在一个没有问题的数据库中。

问题来自于从latin1解码数据创建图像。当试图写入文件句柄时,我得到一个UnicodeEncodeError表示ascii编码失败。

我认为问题是当以二进制数据(rb)方式打开文件时,它不是正确的ascii编码,因为它包含二进制数据。然后,我将二进制数据解码为latin1,但是当转换回ascii(尝试写入文件时自动编码)时,它失败了,原因不明。

我的猜测是,当解码到latin1时,原始二进制数据被转换成其他数据,然后当试图编码回ascii时,它无法识别曾经的原始二进制数据。(尽管原始数据和解码数据具有相同的长度)。 或者问题不在于对latin1的解码,而在于我试图对二进制数据进行ascii编码。在这种情况下,我将如何编码latin1 数据返回到图像。

我知道这很让人困惑,但我对这一切都很困惑,所以我无法很好地解释。如果有人能回答这个问题,可能有一个谜语大师。

一些要可视化的代码:

>>> image_handle = open('test_image.jpg', 'rb')
>>> 
>>> raw_image_data = image_handle.read()
>>> latin_image_data = raw_image_data.decode('latin1')
>>> 
>>> 
>>> # The raw data can't be processed by django 
... # but in `latin1` it works
>>> 
>>> # Analysis of the data
>>> 
>>> type(raw_image_data), len(raw_image_data)
(<type 'str'>, 2383864)
>>> 
>>> type(latin_image_data), len(latin_image_data)
(<type 'unicode'>, 2383864)
>>> 
>>> len(raw_image_data) == len(latin_image_data)
True
>>> 
>>> 
>>> # How to write back to as a file?
>>> 
>>> copy_image_handle = open('new_test_image.jpg', 'wb')
>>> 
>>> copy_image_handle.write(raw_image_data)
>>> copy_image_handle.close()
>>> 
>>> 
>>> copy_image_handle = open('new_test_image.jpg', 'wb')
>>> 
>>> copy_image_handle.write(latin_image_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
>>> 
>>> 
>>> latin_image_data.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
>>> 
>>> 
>>> latin_image_data.decode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

Tags: 数据in图像image编码datarawascii
2条回答

与普通/疼痛文本文件不同,图像文件没有任何编码,显示的数据是图像的二进制等价物的视觉表示。就像上面@cameron-f在问题评论中说的,这基本上是胡言乱语,任何编码都会破坏图像文件,所以不要尝试。

但这并不意味着失去了所有的希望。这里有一种方法,我通常把一个图像变成一个字符串,然后再变成一个图像。

from base64 import b64decode, b64encode

image_handle = open('test_image.jpg', 'rb')

raw_image_data = image_handle.read()

encoded_data = b64encode(raw_image_data)
compressed_data = zlib.compress(encoded_image, 9) 

uncompressed_data = zlib.decompress(compressed_data)
decoded_data = b64decode(uncompressed_data)

new_image_handle = open('new_test_image.jpg', 'wb')

new_image_handle.write(decoded_data)
new_image_handle.close()
image_handle.close()


# Data Types && Data Size Analysis
type(raw_image_data), len(raw_image_data)
>>> (<type 'str'>, 2383864)

type(encoded_image), len(encoded_image)
>>> (<type 'str'>, 3178488)

type(compressed_data), len(compressed_data)
>>> (<type 'str'>, 2189311)

type(uncompressed_data), len(uncompressed_data)
>>> (<type 'str'>, 3178488)

type(decode_data), len(decode_data)
>>> (<type 'str'>, 2383864)



# Showing that the conversions were successful
decode_data == raw_image_data
>>> True

encoded_data == uncompressed_data
>>> True

UnicodeEncodeError正在弹出,因为jpeg是二进制文件,而ASCII编码用于纯文本文件中的纯文本。

纯文本文件可以使用通用文本编辑器创建,如notepad for Windows或nano for Linux。大多数将使用ASCII或Unicode编码。当文本编辑器读取ASCII文件时,它将获取一个字节,比如01100001(dec中的97),并找到相应的glyph“a”。

因此,当文本编辑器试图读取jpg时,它将获取相同的字节01100001并获取“a”,但由于文件包含显示照片的信息,因此文本将只是jibberish。尝试在记事本或nano中打开jpeg。

至于编码,这里有一个解释:What is the difference between encode/decode?

相关问题 更多 >