I was trying to create a gif using PIL as explained in:
http://www.pythoninformer.com/python-libraries/pillow/creating-animated-gif/
And the code they show to save a bunch of images "names" into a gif:
# Open all the frames
images = []
for n in names:
frame = Image.open(n)
images.append(frame)
# Save the frames as an animated GIF
images[0].save('anicircle.gif',
save_all=True,
append_images=images[1:],
duration=100,
loop=0)
However, when saving the gif it only saves one image, what I am doing wrong?
I am using PIL version 1.1.7 in python 2.7
I don't think there is anything wrong with your code. I would check what Pillow version you are using.
from PIL import Image
Image.PILLOW_VERSION
append_images was added in Pillow 3.4.0, so you will want to use at least that version.
Related
I've been trying to find the best way to convert a given GIF image to a sequence of BMP files using python.
I've found some libraries like Wand and ImageMagic but still haven't found a good example to accomplish this.
Reading an animated GIF file using Python Image Processing Library - Pillow
from PIL import Image
from PIL import GifImagePlugin
imageObject = Image.open("./xmas.gif")
print(imageObject.is_animated)
print(imageObject.n_frames)
Display individual frames from the loaded animated GIF file
for frame in range(0,imageObject.n_frames):
imageObject.seek(frame)
imageObject.show()
from wand.image import Image
with Image(filename="animation.gif") as img:
img.coalesce()
img.save(filename="frame%02d.bmp)
Use Image.coalesce() to rebuild optimized frames, and ImageMagick's "Percent Escapes" format (%02d) to write image frame as a separate BMP file.
In Imagemagick, which comes with Linux and can be installed for Windows or Mac OSX,
convert image.gif -coalesce image.bmp
the results will be image-0.bmp, image-1.bmp ...
Use convert for Imagemagick 6 or replace convert with magick for Imagemagick 7.
I am trying to create and save a GIF from a set of PNG files.
pics=[]
for plot_path in plot_paths:
img = Image.open(plot_path)
pics.append(img)
pics[0].save(save_dir+'/truestrain.gif', format='gif', save_all=True, append_images=pics[1:], duration=10, loop=0)
The output is a gif file, of the correct name, but only using the first PNG file, and 10 seconds long.
save_all=True should prompt it to use all of the images in append_images=pics[1:], but that doesn't seem to be working.
duration=10 should set the duration between frames as 10ms, seems to be interpreted as total time 10s (contrary to the Pillow documentation?)
I have seen a relevant previous post, which agrees with the method I am using, but still having problems (Saving an animated GIF in Pillow). I've also checked this follows the documentation (https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html).
So it turns out, the GIF is being created correctly. Neither windows Photos or VLC was able to play it for some reason. I downloaded an alternative GIF viewer and the file is as expected.
Your code works for me with your version 6.2.0. I just tested different settings for duration and can definitely see the difference:
from PIL import Image
assert Image.__version__ == "6.2.0"
frames = [Image.open(f"frame_{i:0>3}.png") for i in range(44)]
for duration in [10, 100, 1000]:
frames[0].save(f"result_{duration}.gif", format="gif", save_all=True, append_images=frames[1:], duration=duration, loop=0)
There is a limit to duration precision of the GIF format itself. But it should be one hundredth of a second, so your duration=10 should be fine.
The following code does not display the image lists.jpg (in current dir):
print(dir(Image)) displays components; im.size, im.filename, im.format all return correct values.
What have I not done to display this jpg file?
from PIL import Image
im = Image.open("lists.jpg")
im.show() # did not work - perhaps due to the environment Jupyter Notebooks
Solution: replaced module with another with immediate results.
from IPython.display import Image
Image(filename='lists.jpg')
I know it is quite late to post but I will do it for new readers.
This problem arises in case of Jupyter Notebooks. Using show() does not display the image. So discard calling show() like in the code below. This will display the image in the output of the cell.
from PIL import Image
im = Image.open("lists.jpg")
im
To display the image on screen:
from PIL import Image
im = Image.open("lists.jpg")
im.show()
See also http://pillow.readthedocs.io/en/4.0.x/reference/Image.html
I'm currently making a program that renders information to a buffer, and I want to save the information as an image file of some sort to my working directory. I've seen some examples using PIL, but that library isn't supported for python 3.x. Are there better alternatives?
First uninstall PIL than install Pillow
its a PIL's clone which works on python 3.x.
from PIL import Image
img = Image.open("test1.jpg") #jpg, png, etc.
pix = img.load()
print img.size #Get the width and height of the image for iterating over
print pix[15,15] #Get the RGBA Value of the a pixel of an image
pix[15, 15] = value # Set the RGBA Value of the image (tuple)
img.save("out.jpg") # Saves the modified pixels to image
I have just done some image processing using the python image library (PIL) and I can't get the save function to work. the whole code works fine but it just wont save the resulting image. The code is below:
im=Image.new("rgb",(200,10),"#ddd")
draw=Image.draw.draw(im)
draw.text((10,10),"run away",fill="red")
im.save("g.jpeg")
Saving gives an error as unknown extension and even removing the dot doesn't help.
Use .jpg:
im.save("g.jpg")
The image library determines what encoder to use by the extension, but in certain versions of PIL the JPEG encoder do not register the .jpeg extension, only .jpg.
Another possibility is that your PIL installation doesn't support JPEG at all; try saving the image as a PNG, for example.
Replace
draw=Image.draw.draw(im)
with
draw = ImageDraw.Draw(im)
and make sure the height of the new image is tall enough to accomodate the text.
import Image
import ImageDraw
im = Image.new("RGB", (200, 30), "#ddd")
draw = ImageDraw.Draw(im)
draw.text((10, 10), "run away", fill="red")
im.save("g.jpeg")
yields
please save with .jpg extention eg:
im.save("g.jpg")