1

I'm trying to create a gif image from several (in this case only 5) png files. I can create the gif if I debug the code at the point the looping through the images occurs. Otherwise the execution appears to be too fast. Here is my code:

img, *imgs = [Image.open(f) for f in sorted(glob.glob(png_in))]

So I flattened the list comprehension and added and played around with the time.sleep(x):

ImageFile.LOAD_TRUNCATED_IMAGES = True
imgs = []
for f in sorted(glob.glob(png_in)):
    i = Image.open(f)
    time.sleep(1)
    imgs.append(i)

Then instead of using the PIL module, I switched to the imageio module:

images = []
png_list = sorted(glob.glob(png_in))
for filename in png_list:
    print(f"fn: {filename}")
    images.append(imageio.imread(filename))
    time.sleep(1)
imageio.mimsave(gif_file, images)

Any ideas where I may be going wrong? The error message I get, for example, is:

unknown element "blank"
    i = Image.open(f)
pymat
  • 1,090
  • 1
  • 23
  • 45
  • *"Too fast"* for **what** exactly please? What are you trying to do and what is the actual problem? Try checking the names in your list of files - you may be picking up dot-files or invalid files. – Mark Setchell Sep 20 '20 at 10:25
  • @MarkSetchell: by too fast, I mean that in pycharm simply running the script doesn't work and error messages get thrown in the for loop (as if opening the image takes too long). If I debug, add a bullit point where the for loop is, and then F8, all works accordingly. – pymat Sep 20 '20 at 10:48
  • Try inspecting `sorted(glob.glob(png_in))` – Mark Setchell Sep 20 '20 at 10:53
  • @MarkSetchell: it's just a list of strings (path and file name of each *.png). – pymat Sep 20 '20 at 11:02
  • @MarkSetchell: thanks Mark (see my answer below). – pymat Sep 21 '20 at 09:45

1 Answers1

1

Thanks to Mark Setchell....the problem I found was that the png's were not all there at the point the for loop was executed. I'm using openscad to create png files, and it's here I need to implement a time.sleep. In debug mode, because it's slower, openscad had time to create the png's and so you don't see this problem.

pymat
  • 1,090
  • 1
  • 23
  • 45