多帧tif图片 matlab,如何使用Python PIL创建多帧.tif图像

new_Image是您加载的内容,即读取-如果没有多个帧,则不能在下一帧上移动。

目前,PIL不支持创建多帧gif,因此您需要切换到pillow这样做-在GitHub上有一些示例代码。

更新

花时间进一步调查,发现了一个bug修复版本images2gif,所以I所做的是使用pip install images2gif,然后从here下载bug修复版本,并重写由pip安装的版本,但您可以下载该bug修复版本并将其与开发文件放在同一目录中。

然后我创建了一个CreateGif.py文件:# coding: utf-8

from __future__ import print_function # Python 2/3 compatibility

import glob

from PIL import Image

from images2gif import writeGif

DELAY=0.75 # How long between frames

FRAMES = [] # Empty list of frames

FIRST_SIZE = None # I am going to say that the first file is the right size

OUT_NAME = "test.gif" # Name to save to

filelist = glob.glob("*.jpg") # For this test I am just using the images in the current directory in the order they are

for fn in filelist: # For each name in the list

img = Image.open(fn) # Read the image

if FIRST_SIZE is None: # Don't have a size

FIRST_SIZE = img.size # So use this one

if img.size == FIRST_SIZE: # Check the current image size if it is OK we can use it

print ("Adding:", fn) # Show some progress

FRAMES.append(img) # Add it to our frames list

else:

print ("Discard:", fn, img.size, "<>", FIRST_SIZE) # You could resize and append here!

print("Writing", len(FRAMES), "frames to", OUT_NAME)

writeGif(OUT_NAME, FRAMES, duration=DELAY, dither=0)

print("Done")

在my测试目录中运行此命令导致:F:\Uploads>python CreateGif.py

Adding: Hidden.jpg

Adding: NewJar.jpg

Adding: P1000063.JPG

Adding: P1000065.JPG

Adding: P1000089.JPG

Discard: WiFi_Virgin.jpg (370, 370) <> (800, 600)

Writing 5 frames to test.gif

Done

并制作:qkn2L.gif

现在是TiFF文件

首先我安装了tiffile,pip install -U tiffile,我已经安装了numpy,然后:# coding: utf-8

from __future__ import print_function # Python 2/3 compatibility

import glob

from PIL import Image

import tifffile

import numpy

def PIL2array(img):

""" Convert a PIL/Pillow image to a numpy array """

return numpy.array(img.getdata(),

numpy.uint8).reshape(img.size[1], img.size[0], 3)

FRAMES = [] # Empty list of frames

FIRST_SIZE = None # I am going to say that the first file is the right size

OUT_NAME = "test.tiff" # Name to save to

filelist = glob.glob("*.jpg") # For this test I am just using the images in the current directory in the order they are

# Get the images into an array

for fn in filelist: # For each name in the list

img = Image.open(fn) # Read the image

if FIRST_SIZE is None: # Don't have a size

FIRST_SIZE = img.size # So use this one

if img.size == FIRST_SIZE: # Check the current image size if it is OK we can use it

print ("Adding:", fn) # Show some progress

FRAMES.append(img) # Add it to our frames list

else:

print ("Discard:", fn, img.size, "<>", FIRST_SIZE) # You could resize and append here!

print("Writing", len(FRAMES), "frames to", OUT_NAME)

with tifffile.TiffWriter(OUT_NAME) as tiff:

for img in FRAMES:

tiff.save(PIL2array(img), compress=6)

print("Done")

它生成了一个很好的tiff文件,但不幸的是,SO查看器没有显示windows至少认为可以接受的多页tiff文件,如下所示。Gsy5Q.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值