将存储在BytesIO中的图像保存在枕头中 [英] Saving an Image stored in BytesIO in pillow

查看:486
本文介绍了将存储在BytesIO中的图像保存在枕头中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像存储为Pillow的BytesIO,我需要将其保存到包含一些头信息(包含文本属性)的文件中,我需要针对该问题添加特定的信息.我需要根据某种图像压缩格式来表示字节.那有可能吗?如果是,该怎么办? 我还需要在文件中存储多个图像.

I have an image stored as BytesIO of Pillow and I need to save it to a file with some header information (containing textual attributes) that I need to add specific to my problem. I need the bytes to be represented according to some image compression format. Would that be possible? If yes, how it can be done? I also need to store more than one image in the file.

推荐答案

对于PNG,JPEG和大多数常见格式,在文件中存储多个图像是有问题的.一种选择是TIFF-不确定是否适合您?

Storing more than one image in a file is problematic for PNG, JPEG and the most of the common formats. One option for that is TIFF - not sure if that works for you?

以下是您至少可以在PNG中存储一些其他文本的方法:

Here's how you can store some additional text in a PNG at least:

#!/usr/bin/env python3

from PIL.PngImagePlugin import Image, PngInfo

# Create empty metadata and add a couple of text strings
metadata = PngInfo()
metadata.add_text("Key1:","Value1")
metadata.add_text("Key2:","Value2")

# Create red image and save with metadata embedded
im = Image.new('RGB',(64,64),'red')
im.save("result.png", pnginfo=metadata)

如果用pngcheck进行检查,您将看到:

If you check that with pngcheck you will see:

pngcheck -7v result.png

示例输出

File: result.png (200 bytes)
  chunk IHDR at offset 0x0000c, length 13
    64 x 64 image, 24-bit RGB, non-interlaced
  chunk tEXt at offset 0x00025, length 12, keyword: Key1:
    Value1
  chunk tEXt at offset 0x0003d, length 12, keyword: Key2:
    Value2
  chunk IDAT at offset 0x00055, length 95
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x000c0, length 0
No errors detected in result.png (5 chunks, 98.4% compression).


以下是在单个TIFF文件中保存3张图像和一条注释的方法:


Here's how to save 3 images and a comment in a single TIFF file:

from PIL import Image 
from PIL.TiffImagePlugin import ImageFileDirectory_v2, TiffTags 

# Create a structure to hold meta-data
ifd = ImageFileDirectory_v2() 
ifd[270] = 'Some Funky Comment' 
ifd.tagtype[270] = TiffTags.ASCII 

# Create red image and save with metadata embedded 
im1 = Image.new('RGB',(50,50),'red') 
im2 = Image.new('RGB',(64,64),'green') 
im3 = Image.new('RGB',(80,80),'blue') 
im1.save("result.tif", append_images[im2,im3], save_all=True, tiffinfo=ifd)

并使用以下命令进行确认:

And check that with:

tiffinfo -v result.tif

示例输出

TIFF Directory at offset 0x8 (8)
  Image Width: 50 Image Length: 50
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 50
  Planar Configuration: single image plane
  ImageDescription: Some Funky Comment
TIFF Directory at offset 0x1e08 (7688)
  Image Width: 64 Image Length: 64
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 64
  Planar Configuration: single image plane
  ImageDescription: Some Funky Comment
TIFF Directory at offset 0x4eb8 (20152)
  Image Width: 80 Image Length: 80
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 80
  Planar Configuration: single image plane
  ImageDescription: Some Funky Comment

然后您可以像这样使用 ImageMagick 在命令行上提取图像.

You can then extract the images on the command-line with ImageMagick like this.

要提取第一张图像:

magick result.tif[0] first.png

要提取最后一张图像:

magick result.tif[-1] last.png

要提取所有三个图像:

magick result.tif image-%d.png

结果

-rw-r--r--  1 mark  staff  457 21 Jan 08:11 image-0.png
-rw-r--r--  1 mark  staff  458 21 Jan 08:11 image-1.png
-rw-r--r--  1 mark  staff  460 21 Jan 08:11 image-2.png 

注意:如果正在运行v6 ImageMagick ,请使用convert代替上面的magick.

Note: Use convert in place of magick above if you are running v6 ImageMagick.

关键字:Python,PIL,图像处理,多幅图像,TIF,注释,tiffinfo,IFD,PNG tEXt.

Keywords: Python, PIL, image processing, multiple images, TIF, comment, tiffinfo, IFD, PNG tEXt.

这篇关于将存储在BytesIO中的图像保存在枕头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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