Title: Make a GIF play exactly once with Python Wand
Tags: python;python-3.x;gif;animated-gif;wand
Question: I can create an animated GIF like this:

```from wand.image import Image

with Image() as im:
    while i_need_to_add_more_frames():
        im.sequence.append(Image(blob=get_frame_data(), format='png'))
        with im.sequence[-1] as frame:
            frame.delay = calculate_how_long_this_frame_should_be_visible()
    im.type = 'optimize'
    im.format = 'gif'
    do_something_with(im.make_blob())
```

However, an image created like this loops indefinitely. This time, I want it to loop once, and then stop. I know that I could use ```convert```'s ```-loop``` parameter if I were using the commandline interface. However, I was unable to find how to do this using the Wand API. 

What method should I call, or what field should I set, to make the generated GIF loop exactly once?



Here is another answer: You'll need to use ```ctypes``` to bind the wand library to the correct C-API method.
Luckily this is straightforward.

```import ctypes
from wand.image import Image
from wand.api import library

# Tell Python about the C-API method.
library.MagickSetImageIterations.argtypes = (ctypes.c_void_p, ctypes.c_size_t)

with Image() as im:
    while i_need_to_add_more_frames():
        im.sequence.append(Image(blob=get_frame_data(), format='png'))
        with im.sequence[-1] as frame:
            frame.delay = calculate_how_long_this_frame_should_be_visible()
    im.type = 'optimize'
    im.format = 'gif'
    # Set the total iterations of the animation.
    library.MagickSetImageIterations(im.wand, 1)
    do_something_with(im.make_blob())
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:07/06/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<python><python-3.x><gif><animated-gif><wand>', 'Title': 'Make a GIF play exactly once with Python Wand', 'ViewCount': 278, 'id': 51217433, 'user_AboutMe': None, 'user_AccountId': 7746382.0, 'user_DisplayName': 'Maya', 'user_Reputation': 1350.0, 'user_Views': 183.0, 'user_id': 5863987.0}}