yuraic
June 30, 2015, 10:27am
1
Hello,
I tried an example to bytestream through TImage from TCanvas , but the output PNG is extremely large - about 36Mb. Is it possible to reduce the size?
Thanks!
from ROOT import *
from array import array
from ctypes import *
c=TCanvas('c','c', 546, 462)
f1=TF1('f1', 'sin(x)/x', 0, 10)
f1.Draw()
x=array('l', [0])
y=array('i', [0])
img=TImage.Create()
img.FromPad(c)
img.GetImageBuffer(x,y, TImage.kPng)
s=string_at(x[0], y[0])
of=file('boo.png', 'wb')
for i in s:
of.write(s)
of.close()
couet
June 30, 2015, 11:01am
2
When I save the canvas you created as a png file (from the File) menu I get only a 16KB file.
yuraic
June 30, 2015, 12:24pm
3
Did you check size of the binary file produced by the script - boo.png ?
“SaveAs” should be fine. But what I really need is to read a bytestream from TImage memory. This is needed to serve dynamically created image by Cherrypy server without saving it to the actual file.
yuraic
June 30, 2015, 1:02pm
5
Yes, on my Mac it is also 2.7 Mb,
but on lxplus or our server, it is 44 Mb
But even 2.7 Mb is quite large especially if we know the size can be lowered down to 17 Kb. As you pointed out above.
couet
June 30, 2015, 1:15pm
6
The png creation from the Menu is done by TImageDump.
root.cern.ch/root/html534/TImageDump.html
May be you can look there how it proceeds.
yuraic
June 30, 2015, 3:35pm
7
Ok, I think I found the error - the loop at the end is not redundant. The below code works just fine and PNG size is 6kb only.
[code]#!/usr/bin/env python
from ROOT import *
from array import array
from ctypes import *
c=TCanvas(‘c’,‘c’, 546, 462)
f1=TF1(‘f1’, ‘sin(x)/x’, 0, 10)
f1.Draw()
x=array(‘l’, [0])
y=array(‘i’, [0])
img=TImage.Create()
img.FromPad©
img.GetImageBuffer(x,y, TImage.kPng)
s=string_at(x[0], y[0])
of=file(‘boo.png’, ‘w’)
of.write(s)
of.close()[/code]
A small update.
On lxplus the above code produces about 10% larger PNG if compared to SaveAs method. I.e. 6708 bytes from GetImageBuffer vs 6089 bytes from SaveAs. The header and footer seem to be the same.
In hex format,
header - 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 (.PNG…IHDR)
footer - 49 45 4e 44 ae 42 60 82 (IEND.B`.)
What is strange that the code does not work on OSX (Yosemite 10.10.3). The output PNG file is 1682 bytes only and contains just an empty TCanvas.