Please select To the mobile version | Continue to access the desktop computer version

Nujin

 Forget password?
 Register

WeChat login, one step

View: 2122|Reply: 0

[Technical Essence]Use PIL and imageio to deframe, compress and synthesize gif. The blogger uses PyCharm... the essence of technology

[Copy this link] Choose your language

3373

Threads

3373

Posts

372

Credits

打工仔

Rank: 2

Credits
372
Post time 2020-10-13 16:17:16 | Show all posts |Reading mode

Use PIL and imageio to deframe, compress and synthesize gif. The blogger uses the PyCharm tool.

1. Install imageio. File-->Settings-->roject:pro-->roject Interpreter-->click the "+" in the upper right corner to search for "imageio" installation.

  1. <font size="3">def getFrames(im):
  2.     #非动画
  3.     if False == im.is_animated :
  4.         return;
  5.     index=1
  6.     for frame in ImageSequence.Iterator(im):
  7.         frame=frame.convert('RGB')
  8.         frame.save("g%d.jpg" % index)

  9.         compressImg('g%d.jpg'% index)
  10.         index=index + 1
  11.     return index</font>
Copy the Code


2. Extract each frame of gif and save it in jpg format. Here, .convert('RGB') is used because frame.save cannot save pictures in png format, the same below.

  1. <font size="3">
  2. def compressImg(ImgName):
  3.     im=Image.open(ImgName)
  4.     im.convert('RGB')
  5.     if max(im.size[0], im.size[1]) > 128:
  6.         im.thumbnail((128, 128))
  7.     im.save('f-'+ImgName, quality=50)
  8. </font>
Copy the Code


3. Compress pictures.


4. Combine the previously opened jpg file into a gif.

  1. <font size="3">def compressGif(ind,dur):
  2.     images =[]
  3.     for i in range(1, ind):
  4.         images.append(imageio.imread('f-g%d.jpg' % i))
  5.     imageio.mimsave('c001.gif', images, duration=dur)</font>
Copy the Code


5. Calculate the frequency between frames, the obtained is milliseconds, and the duration is seconds, so divide by 1000.

  1. <ol class="hljs-ln" style="box-sizing: border-box; outline: 0px; list-style: none; border-collapse: collapse; overflow: hidden; color: rgb(56, 58, 66); font-family: &quot;Source Code Pro&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;Anonymous Pro&quot;, &quot;Droid Sans Mono&quot;, Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, sans-serif; white-space: pre;"><li style="box-sizing: border-box; outline: 0px; margin-left: 0px; list-style: none; height: 22px;"><div class="hljs-ln-code" style="box-sizing: border-box; outline: 0px; margin-left: 8px; float: left; height: 22px;"><div class="hljs-ln-line" style="box-sizing: border-box; outline: 0px;"><span class="hljs-function" style="box-sizing: border-box; outline: 0px;"><font size="3"><span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(166, 38, 164);">def</span> <span class="hljs-title" style="box-sizing: border-box; outline: 0px; color: rgb(64, 120, 242);">calDuration</span>(<span class="hljs-params" style="box-sizing: border-box; outline: 0px;">im</span>):</font></span></div></div></li><li style="box-sizing: border-box; outline: 0px; margin-left: 0px; list-style: none; height: 22px;"><div class="hljs-ln-numbers" style="box-sizing: border-box; outline: 0px; float: left; height: 22px; width: 24px; border-right: 1px solid rgb(197, 197, 197);"><div class="hljs-ln-line hljs-ln-n" data-line-number="2" style="box-sizing: border-box; outline: 0px; padding-right: 8px; text-align: right; overflow-wrap: normal;"></div></div><div class="hljs-ln-code" style="box-sizing: border-box; outline: 0px; margin-left: 8px; float: left; height: 22px;"><div class="hljs-ln-line" style="box-sizing: border-box; outline: 0px;"><font size="3">    <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(166, 38, 164);">return</span> (im.info)[<span class="hljs-string" style="box-sizing: border-box; outline: 0px; color: rgb(80, 161, 79);">'duration'</span>]/<span class="hljs-number" style="box-sizing: border-box; outline: 0px; color: rgb(152, 104, 1);">1000</span></font></div></div></li></ol>
Copy the Code
6,Remove pictures
  1. <font size="3">def removeImg(ind):
  2.     for i in range(1,ind):
  3.         af='f-g' + str(i) + '.jpg'
  4.         f='g' + str(i) + '.jpg'
  5.         if os.path.exists(af):
  6.             os.remove(af)
  7.         if os.path.exists(f):
  8.             os.remove(f)</font>
Copy the Code

7. Pictureslice 20180302150756601 before compression.

8. The compressed picture 2018030215093092

Reply

Use magic Report

 
Online Customer Service
点击这里给我发消息
Technical Advisory Hotline
Wechat:gwailee
QQ:395508196
EM:hi@niuresou.com

Scan WeChat, follow the public account to lead Nujinniu Stock Niu Ce

QQ|Baidu Statistics|Archives|Mobile|Black House|Search management|Game management|Nujin ( ICP证:沪B2-20210491 | ICP备案号:沪ICP备20013403号 ) Choose your language

GMT+8, 2021-6-23 11:41 , Processed in 2.511980 second(s), 3964 queries .

Powered by Nujin X3.4

© 2001-2017 Nujin Inc.

Quick reply To Top Back to list