有没有办法在不读取整个文件的情况下推断文件的图像格式?
有没有一种好方法可以查看图像的格式,而无需将整个文件读入内存?
显然,这会因格式的不同而有所不同(我对 TIFF 文件特别感兴趣),但是什么样的过程有助于确定文件的图像格式而无需读取整个文件?
奖励:如果图像是 Base64 编码的字符串怎么办? 在解码之前有什么可靠的方法来推断它吗?
Is there a good way to see what format an image is, without having to read the entire file into memory?
Obviously this would vary from format to format (I'm particularly interested in TIFF files) but what sort of procedure would be useful to determine what kind of image format a file is without having to read through the entire file?
BONUS: What if the image is a Base64-encoded string? Any reliable way to infer it before decoding it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大多数图像文件格式在开头都有唯一的字节。 unix
file
命令查看文件的开头以查看它包含的数据类型。 请参阅关于文件中的幻数和magicdb.org。Most image file formats have unique bytes at the start. The unix
file
command looks at the start of the file to see what type of data it contains. See the Wikipedia article on Magic numbers in files and magicdb.org.当然有。 就像其他人提到的那样,大多数图像都以某种“魔法”开始,它总是会转换为某种 Base64 数据。 以下是几个示例:
位图将以
Qk3
开头Jpeg 以
/9j/
开头 GIF 以
R0l
开头(即零作为第二个字符)。等等。 获取不同的图像类型并找出它们编码的内容并不难。 请小心,因为有些拥有不止一件魔法,因此您需要在 B64“翻译代码”中考虑它们。
Sure there is. Like the others have mentioned, most images start with some sort of 'Magic', which will always translate to some sort of Base64 data. The following are a couple examples:
A Bitmap will start with
Qk3
A Jpeg will start with
/9j/
A GIF will start with
R0l
(That's a zero as the second char).And so on. It's not hard to take the different image types and figure out what they encode to. Just be careful, as some have more than one piece of magic, so you need to account for them in your B64 'translation code'.
在 *nix 命令行上执行
file
或读取文件的初始字节。 大多数文件的前几个字节都有一个唯一的标头。 例如,TIFF 的标头如下所示:For more information on the TIFF file format specifically if you'd like to know what those bytes stand for, go here.
Either
file
on the *nix command-line or reading the initial bytes of the file. Most files come with a unique header in the first few bytes. For example, TIFF's header looks something like this:For more information on the TIFF file format specifically if you'd like to know what those bytes stand for, go here.
TIFF 将以 II 或 MM(Intel 字节排序或 Motorolla)开头。
TIFF 6 规范可以在此处下载,但并非如此太难遵循
TIFFs will begin with either II or MM (Intel byte ordering or Motorolla).
The TIFF 6 specification can be downloaded here and isn't too hard to follow