question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Write single image series in append mode

See original GitHub issue

Hi @cgohlke

Thanks for the awesome package 😃

I’m trying to write chunks of a video to tiff using the append mode (append=True). When I load the tiff file I get the right number of pages, but if I try to memmap it I only get one chunk at a time. In fact, each imsave call appends a new series to the tiff file. Is it possible to write in append mode and extend the existing series, so that in the end I can memmap the entire video?

Minimal example:

import numpy as np
import tifffile

save_path = 'test_append.tif'

# create 1000 frmes, 50x50 image size
video = np.random.randn(1000, 50, 50)

# write 100 frames at a time:
len_chunk = 100
for i in range(10):
     tifffile.imsave(save_path, video[i * len_chunk:(i+1) * len_chunk], append=True)

tif = tifffile.TiffFile(save_path)
print(len(tif.pages)) # correctly prints 10000
tif.close()

video0 = tifffile.memmap(save_path, series=0)
print(video0.shape) # prints (100, 50, 50)

video1 = tifffile.memmap(save_path, series=1)
print(video1.shape) # prints (100, 50, 50)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cgohlkecommented, Sep 24, 2020

I actually know the total number of frames. In this case, is it possible to write chunks of frames at once to the same series (not frame by frame)?

If you don’t need the tifffile metadata, try:

with TiffWriter('video.tif') as tif:
    for chunk_of_frames in video:
        tif.save(chunk_of_frames, contiguous=True, metadata=None)

Otherwise provide a generator of frames (not tested):

def frames():
    for chunk_of_frames in video:
        for frame in chunk_of_frames:
            yield frame

imwrite('video.tif', frames(), shape=video.shape, dtype=video.dtype)
0reactions
alejoe91commented, Sep 24, 2020

The first solution did the trick 😃

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.Series.append — pandas 1.5.2 documentation
General function to concatenate DataFrame or Series objects. Notes. Iteratively appending to a Series can be more computationally intensive than a single ......
Read more >
Python append to a file - GeeksforGeeks
Append and Read ('a+'): Open the file for reading and writing. When the file is opened in append mode in Python, the handle...
Read more >
Python Write to File – Open, Read, Append, and Other File ...
The "a" mode allows you to open a file to append some content to it. And we want to add a new line...
Read more >
MATLAB imwrite - Write image to graphics file - MathWorks
Draw a series of plots, capture them as images, and write them into one animated GIF ... In "append" mode, imwrite adds a...
Read more >
Python Program to Append to a File - Programiz
Open the file in append 'a' mode, and write to it using write() method. Inside write() method, a string "new text" is passed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found