Programmatically generate video or animated GIF in Python?
Well, now I'm using ImageMagick. I save my frames as PNG files and then invoke ImageMagick's convert.exe from Python to create an animated GIF. The nice thing about this approach is I can specify a frame duration for each frame individually. Unfortunately this depends on ImageMagick being installed on the machine. They have a Python wrapper but it looks pretty crappy and unsupported. Still open to other suggestions.
How do I create an animated gif in Python using Wand?
The best examples are located in the unit-tests shipped with the code. wand/tests/sequence_test.py
for example.
For creating an animated gif with wand, remember to load the image into the sequence, and then set the additional delay/optimize handling after all frames are loaded.
from wand.image import Image
with Image() as wand:
# Add new frames into sequance
with Image(filename='1.png') as one:
wand.sequence.append(one)
with Image(filename='2.png') as two:
wand.sequence.append(two)
with Image(filename='3.png') as three:
wand.sequence.append(three)
# Create progressive delay for each frame
for cursor in range(3):
with wand.sequence[cursor] as frame:
frame.delay = 10 * (cursor + 1)
# Set layer type
wand.type = 'optimize'
wand.save(filename='animated.gif')
How to create an animated GIF from JPEGs in Android (development)
See this solution.
https://github.com/nbadal/android-gif-encoder
It's an Android version of this post.
http://www.jappit.com/blog/2008/12/04/j2me-animated-gif-encoder/
To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.
public byte[] generateGIF() {
ArrayList<Bitmap> bitmaps = adapter.getBitmapArray();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
for (Bitmap bitmap : bitmaps) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
To use this function, do the following then you can save the file into SDcard.
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/generate_gif/test.gif");
outStream.write(generateGIF());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
how to create gif animation from a stack of jpgs
Might want to look at GiftedMotion: http://www.onyxbits.de/giftedmotion
Creating animation (.gif) in R using .png files
You can use image_join
from magick to coerce a list of objects of class "magick-image" to a multi-frame image.
Here's how I might do it with your example:
library(purrr)
library(magick)
location <- c("A","B","C")
years <- c(2001,2002,2003)
df <- data.frame(loc = character(0), yr = integer(0), file = character(0))
for(l in seq_along(location)){
for(y in seq_along(years)){
loc <- location[l]
yr <- years[y]
png(paste0(loc,".",yr,".png"))
plot(rnorm(10))
dev.off()
}
}
df <- expand.grid(loc = location,
yr = years)
df$file = paste0(df$loc,".",df$yr,".png")
df
# loc yr file
# 1 A 2001 A.2001.png
# 2 B 2001 B.2001.png
# 3 C 2001 C.2001.png
# 4 A 2002 A.2002.png
# 5 B 2002 B.2002.png
# 6 C 2002 C.2002.png
# 7 A 2003 A.2003.png
# 8 B 2003 B.2003.png
# 9 C 2003 C.2003.png
locations <- unique(df$loc)
for(i in 1:length(locations)) {
images <- map(df$file[df$loc == locations[i]], image_read)
images <- image_join(images)
animation <- image_animate(images, fps = 1)
image_write(animation, paste0(locations[i], ".gif"))
}
Creating GIF Animations in R
Here is a way. Just follow the last example in help("gifski")
.
library(gifski)
makeplot <- function(){
p1 = hist(rnorm(1000,1,1), 10000)
p2 = hist(rnorm(1000,1,1), 10000)
p3 = hist(rnorm(1000,1,1), 10000)
}
set.seed(2021)
gif_file <- "~/tmp/ref.gif"
save_gif(makeplot(), gif_file, 1280, 720, res = 144)
utils::browseURL(gif_file)
Edit
To create a GIF file when the plots already exist must be done in two steps. First create the PNG files from the plots, then create the GIF animation.
set.seed(2021)
p1 <- hist(rnorm(1000,1,1), 10000)
p2 <- hist(rnorm(1000,1,1), 10000)
p3 <- hist(rnorm(1000,1,1), 10000)
plot_list <- list(p1, p2, p3)
png_path <- file.path(tempdir(), "frame%03d.png")
png(png_path)
lapply(plot_list, plot)
#[[1]]
#NULL
#
#[[2]]
#NULL
#
#[[3]]
#NULL
dev.off()
#RStudioGD
# 2
png_files <- sprintf(png_path, seq_along(plot_list))
gif_file2 <- "~/tmp/ref2.gif"
gifski(png_files, gif_file2)
#Inserting image 3 at 2.00s (100%)...
#Encoding to gif... done!
#[1] "/home/rui/tmp/ref2.gif"
utils::browseURL(gif_file2)
Final clean-up
unlink(gif_file)
unlink(gif_file2)
unlink(png_files)
Related Topics
How to Move Application's Window Between Virtual Desktops in Os X
Play Mp4 Using Mpmovieplayercontroller() in Swift
Set Uitextfield Placeholder Color Programmatically
Applewatch Messages Url Works Hard Coded But Not with Variables
Check If a Character Is Lowercase or Uppercase
Sktexture Nearest Filtering Mode Doesn't Work (Making Pixel Art)
How to Edit Swift Error Breakpoint
Sidebar Menu for MACos in Swiftui
Xcode 6, Swift and Mock Verification Without Exceptions
Solving System of Equations in Swift
Textfield Delegate Shouldchangecharactersinrange
Xcode 7 Run on MAC Osx 10.10 Yosemite
What Does "Arg = Exploded" Mean in Swift Crash Log
Insert a Comment Using the Youtube API and Alamofire
Google Sign-In via Firebase: Gidsignindelegate Does Not Conform to Viewcontroller