def modify(self, function, *args, **kwargs):
""" Modify the image object using the given Image function.
This function supplies sequence support. """
if not gif_support or not self.gif:
self.object = function(self.object, *args, **kwargs)
else:
frames = []
duration = self.object.info.get("duration") / 1000
for frame in ImageSequence.Iterator(self.object):
frame_bytes = utils.convert_image_object(function(frame, *args, **kwargs))
frames.append(imageio.imread(frame_bytes, format="PNG"))
# Save the image as bytes and recreate the image object
image_bytes = imageio.mimwrite(imageio.RETURN_BYTES, frames, format=self.format, duration=duration)
self.object = Image.open(BytesIO(image_bytes))
self.gif_bytes = image_bytes
python类mimwrite()的实例源码
def gifwrite(name, img_buf):
print("Thread spawned"*7)
kargs = { 'quantizer':'nq' }
imageio.mimwrite(name + ".gif", img_buf, 'GIF-FI', **kargs)
# write additional data inside json
fp = open("../records/%s.json" % name, 'w')
fp.write("{\n")
fp.write(" \"id\": 123,");
fp.write(" \"gifUrl\": \"%s.gif\", \n" % name);
fp.write(" \"timestamp\": \"%s\", \n" %
str(datetime.datetime.now()))
fp.write(" \"tags\": [\"andrej\",\"hack\"]");
fp.write("}\n")
fp.close()
# main loop
def visualize(name):
for counter, file in enumerate(sorted(glob.glob(os.path.join(args.train_dir, '{}_*.hy'.format(name))), key=os.path.getmtime)[-args.n:]):
print (file)
f = h5py.File(file, 'r')
# I = np.zeros((args.h, args.num_frames * args.w, args.c))
generated_frames = f[f.keys()[0]]
_, _, h, w, c = generated_frames.shape
h_low = (h - args.h) / 2
h_high = (h + args.h) / 2
w_low = (w - args.w) / 2
w_high = (w + args.w) / 2
# Take only first set of frames from batch
II = []
if args.c == 1:
for j in range(args.num_frames):
I = np.reshape(generated_frames[0, j, h_low:h_high, w_low:w_high, 0], (args.h, args.w))
if (I < 1.0).all() and (I > -1.0).all():
print ('Image in [-1, 1]')
I = ((I + 1.0) / 2 * 255).astype(np.int32)
II.append(I)
else:
for j in range(args.num_frames):
I = np.reshape(generated_frames[0, j, h_low:h_high, w_low:w_high, 0:args.c], (args.h, args.w, args.c))
II.append(I)
# II = np.stack(II)
output_img_path = './outputs/{}_{}_{}.png'.format(args.output_prefix, name, str(counter))
print ('Writing image:', output_img_path)
print (len(II), II[0].shape)
imageio.mimwrite(output_img_path, II)
def save(self, path):
"""
This function ...
:param path:
:param fps:
:return:
"""
# Create and write the GIF file
imageio.mimwrite(path, self.frames, fps=self.fps)
# -----------------------------------------------------------------
def save(self, path):
"""
This function ...
:param path:
:param fps:
:return:
"""
# Create and write the GIF file
imageio.mimwrite(path, self.frames, fps=self.fps)
# -----------------------------------------------------------------
def _save_gif(traj, filename):
traj = np.asarray(traj, dtype='uint8')
return imageio.mimwrite(filename, traj, duration=0.1)