Created
January 10, 2019 01:31
-
-
Save Melraidin/8a4b92fefe2a327da61674e17d094523 to your computer and use it in GitHub Desktop.
Decode and re-encode a PNG for timing purposes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"image/png" | |
"io/ioutil" | |
"time" | |
) | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
func main() { | |
data, err := ioutil.ReadFile("depth.png") | |
check(err) | |
for i := 0; i < 10; i++ { | |
start := time.Now() | |
reencode(data) | |
fmt.Printf("Decode-encode cycle took %s.\n", time.Since(start)) | |
} | |
err = ioutil.WriteFile("re-encoded.png", reencode(data).Bytes(), 0666) | |
check(err) | |
} | |
func reencode(data []byte) *bytes.Buffer { | |
img, err := png.Decode(bytes.NewReader(data)) | |
check(err) | |
buf := &bytes.Buffer{} | |
err = png.Encode(buf, img) | |
check(err) | |
return buf | |
} |
import copy
import cStringIO
import io
import sys
import time
import png_header_injector
ITERATIONS = 10
def main(input_path):
use_header_injector(input_path)
def use_header_injector(input_path):
with open(input_path, "rb") as png:
inp = png.read(-1)
for i in range(ITERATIONS):
img_data = cStringIO.StringIO(copy.copy(inp))
out = io.BytesIO()
start = time.time()
png_header_injector.replace_text(
img_data, out, {"Comment": "Test comment data."})
end = time.time()
print(
"Header injector total time: %0.4f seconds." % (end - start))
if __name__ == "__main__":
main(sys.argv[1])
Timing:
$ python png-injector.py ~/dev/go-tests/zed-depth.png
Header injector total time: 0.0007 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Header injector total time: 0.0001 seconds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above Go code: