当前位置:网站首页>Apng2gif solutions to various problems

Apng2gif solutions to various problems

2022-07-07 23:41:00 github-3rr0r

In this paper 2020-10-21 Starting from my GitPages-apng2gif Solutions to various problems , Now transported to CSDN.

The cause of the matter

It's more than two years ago , There was an expression wrapped in Line It's on fire , After that, all major domestic platforms were bloodwashed , This expression pack is Menhera sauce .

 Insert picture description here
 Insert picture description here

Who can resist lovely things ? So at that time, I decided to ask for all their expressions , At first, it was carried by netizens , Collect in Baidu online disk and other places .

But over time ,Menhera The heat of the sauce also went down , Not many people collect it anymore , How many people like it as consistently as I do ?

At this time, my obsessive-compulsive disorder came , How uncomfortable it is if the collection is incomplete , So I decided to go in person Line collect .

On Line It needs magic , At that time, a little magic was used , And in the Chrome Found a plug-in on , Successfully downloaded all their expressions .

however !

Line The graph on is PNG Format , More precisely APNG Format , Such dynamic pictures are useless on wechat , Put it on, it's a static picture , Even the background is dark .

Wechat only supports GIF The moving picture of , I can't bear it , It has to be arranged !

The arduous transformation process

Tool finding stage

At first, it must be a batch conversion tool made by others , Keyword search apng2gif, Of course, there are a lot of blog posts and tools .

Tried a tool that is said to be very useful isparta, It took a long time to download . The conversion time is very long , But there are many problems in the result conversion , It can be played normally on the computer , But send it to wechat After playing it again, it will spend the screen .

 Insert picture description here

I think it may be the version bug, It took a long time to download other versions , The results are not easy to use , I can only give up this software .

Then I found an online conversion website , Many parameters need to be configured , Generated a try, the effect is actually good .

But this website is online after all , The degree of automation is not high , The network connection is also relatively slow , Batch processing must be a big project , I have to give up .

This is the first attempt .

Build your own wheels

One day later, I thought of it again , Decided to rethink how to do .

APNG Format has been born for so long , There must be a lot of information ?

This is the idea ,APNG It's a dynamic graph , It must be composed of many frames , Extract these frames , And then synthesize GIF Not to go ?

Looking for some information , Wrote a python Program , Exported a APNG Frames of .

It turned out that there was a big problem ,APNG Every frame of is not a complete picture , It's a change from the previous frame !

Finally, the synthesized things are very ghost animals , See the figure below :

 Insert picture description here

The mood is not beautiful , What strange thing did you synthesize for me with so much effort ?

And in the Github I found a letter written by my compatriots in Wan Wan python Program , Repeated debugging and operation can not get the correct results .

Later, there were ideas , Or the idea of analytic synthesis , Just put each frame in the corresponding coordinates , But after thinking about it, I gave up because it was too complicated .

This is the second attempt .

The stage of transforming the wheel

I saw the article pushed by qilai walnut official account these two days , There is a lovely moving picture , It is forwarded to the object , As a result, she called out so cute !

Although I have tried so many times , Also failed so many times , But this time we must succeed .

Discover new wheels

The idea is to first see if there are ready-made wheels , Of course there are !

I've tried, pip Medium apng2gif package , It doesn't work at all , Not even normal playback . So I went to find the source code , result ……

This is the one I referred to last time Github Source code on ?

give up !

however , I accidentally typed in the command line apng2gif, Forgot to add suffix , The result suggests that I can pass apt install ?

original Linux There are ready-made wheels available on ! hurriedly apt Came down .

The use of this program is very simple :

 Insert picture description here

I tried one APNG Moving graph , Derived GIF The effect is actually quite good ?

Send it to wechat immediately , The results are Play it once and you'll spend the screen , Sure enough, it's still unreliable .

Add a change to the wheel

But this one will spend the screen after playing it once , It seems that it only circulates once ? Then I just need to GIF It's OK to set the cycle of to keep cycling ?

Unfortunately, this tool does not have this option .

But now you can make wheels again , This time we only need to parse GIF Each frame , Then synthesize , Set when saving loop by 0 That's it !

Unfortunately, there are still problems in doing so .

One saving option is disposal, Set to 3 There will be no ghosting when , But there are some cases where only a part of the frame is displayed .

 Insert picture description here

disposal Set to 1 or 0 Although it will show complete , But there will be ghosting .

 Insert picture description here

And then it's about duration The strange question of , Even if the setting is new gif Of duration( Inter frame duration ) And the old duration equally , The final export is gif Of duration It will double , somehow ! It can be the same when set to half .

 Insert picture description here

Anyway, there are still problems .

Another way to add a round of conversion to the wheel

Since there is only one difference between this wheel and my needs loop The value of , I'll just modify that value directly ?

GIF There must be a specific bit in the file to store loop Value . Yes, I did GIF File format , Somehow , They reprint each other on the Internet , No direct information was found , Finally, find a detailed structure of the file in the code .

Through the analysis of , On the file 16 Binary search , find “NETSCAPE2.0”, Count back to the third 、 Just four bytes .

 Insert picture description here

Mark with a highlighter 01 Change to 00,loop From once to infinite cycle ( The following two pictures are one cycle and countless cycles ).

 Insert picture description here

 Insert picture description here

This plan is really effective !

Then the programming idea is also very simple , With 16 Read the file in hexadecimal mode , Then match and find “NETSCAPE2.0”, determine loop The position corresponding to the value , Then change the value directly to 0 Save it .

Modify the wheel

But I didn't follow the idea of the previous part to write the program , Don't ask , Asking is laziness .

Let's change our mind , since apng2gif This tool finally synthesized GIF, It must also be written loop position , We can directly modify the source code of the tool ?

It took a lot of effort from SourceForge Downloaded the source code , Because the download failed , Then switch the mirror .

It's easy to find the source code , Synthesize in code GIF Previous position , Just add a sentence of code :

num_loops = 0;

again make Generate executable files , Try the conversion again and find that the desired effect has been achieved .

make In the process “-lpng” The question of options , adopt apt install libpng-dev that will do :

sudo apt-get install libpng-dev

Write a batch program

So next , Carry out apng2gif The process can be turned into batch processing , This step uses shell or python Will do , I use it python( Because I wrote more than a thousand lines during this period shell Script , I don't want to write now shell 了 ).

The procedure is very simple , Scan all suffixes as .png The file of , call apng2gif The program switches to gif Under the folder .

import os

def trans(filename):
    os.system("./apng2gif "+filename+" gif/"+filename[:-4]+".gif")

if __name__ == "__main__":
    for filename in os.listdir('.'):
        if filename.endswith('.png'):
            trans(filename)

So I tossed for so long , The only really effective amount of code is to modify the wheel 1 sentence c Code and batch 7 sentence python Code (python The code can even be reduced to three sentences !)?

among solve the problem The way is not necessarily difficult , but The process of exploration It must be difficult and interesting .

 Insert picture description here

Keep curious It is the biggest power source of exploration and discovery .

 Insert picture description here

原网站

版权声明
本文为[github-3rr0r]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130555354493.html