问题
I have below snippet which creates a gif. This snippet is derived from hints here. The snippet creates an empty frame and frames have irregular delay between them. Notice delay between nothingness and first frame, and then second frame quickly appears. Why this happens?
I had to create Image of particular size before hand because, wand is not resizing canvas according to maximum sized sequence. I have to find a way to sort that out as well.
def render(self):
# map anim gif
wand = Image2(width=205,height=223)
#create frames
for each_frame in self._tree_dot_frames:
img = Image2(blob=each_frame)
print('Image width: {} height: {}'.format(img.width,img.height))
wand.sequence.append(img)
#set frame rate
for cursor in range(len(self._tree_dot_frames)):
with wand.sequence[cursor] as frame:
print('sequence width: {} height: {}'.format(frame.width,frame.height))
frame.delay = 100
#wand.merge_layers('merge')
wand.format = 'gif'
wand.save(filename='animated.gif')
print('No of frames: {} gif width: {} height: {}'.format(len(wand.sequence),wand.width,wand.height))
self._tree_dot_blob = wand.make_blob()
return self._tree_dot_blob
Output:
Image width: 175 height: 136
Image width: 205 height: 223
sequence width: 205 height: 223
sequence width: 175 height: 136
No of frames: 3 gif width: 205 height: 223
PS: I have another issue with similar snippet here, not to be confused with this issue.
Update 1:
Below is the output if I create wand object without predefined size. The animation seems ok, but canvas is of first sequence (which is smaller), so final gif is cropped, now showing 2nd sequence fully.
#wand = Image2(width=205,height=223)
wand = Image2()
Update 2:
Below is the output if with above change, I also feed the sequence in reverse so that now wand would take 1st sequence size. Now final gif size is ok, but as you can see, the bigger frame is always staying in background which is now the new problem.
#create frames
for each_frame in reversed(self._tree_dot_frames):
img = Image2(blob=each_frame)
print('Image width: {} height: {}'.format(img.width,img.height))
wand.sequence.append(img)
Kindly help as I am getting stuck if going in any of above directions.
Desired solution would be one which takes max size of fed frames, and accommodates all frames in that size / something like below should do (I have shown only 1st 2 frames data in problem above)
回答1:
As per hints given in solution here, I came up with below modified snippet which seems to solve the issue. Apparantly my requirement requires MagickSetImageDispose
or MagickExtentImage
which are not yet implemented in wand. I am very thank ful to emcconville for this help and request him to add his comments if any needed for below solution. I am still exploring. In case of any further issue on this problem, I will get back.
def render(self):
wand = Image2()
#get max frame height, width
frameSizeList = []
tempImgList = []
for each_frame in self._tree_dot_frames:
img = Image2(blob=each_frame)
frameSizeList.append((img.width,img.height))
tempImgList.append(img)
optimalFrameSize = (max(frameSizeList,key=itemgetter(1)))
#print('Max Frame size detected: ',optimalFrameSize)
#create frames
for each_frame in tempImgList:
newImg = Image2(width=optimalFrameSize[0], height=optimalFrameSize[1], background=Color('WHITE'))
newImg.composite(each_frame,0,0)
wand.sequence.append(newImg)
#set frame rate
for cursor in range(len(self._tree_dot_frames)):
with wand.sequence[cursor] as frame:
print('sequence width: {} height: {}'.format(frame.width,frame.height))
frame.delay = 100
#wand.merge_layers('merge')
wand.format = 'gif'
wand.save(filename='animated.gif')
print('No of frames: {} gif width: {} height: {}'.format(len(wand.sequence),wand.width,wand.height))
self._tree_dot_blob = wand.make_blob()
return self._tree_dot_blob
来源:https://stackoverflow.com/questions/50390985/wand-creates-empty-frame-and-irregular-frame-rate