clementfarabet / graphicsmagick

A simple Lua wrapper to graphicsmagick.
127 stars 41 forks source link

Quality and ToBlob #9

Closed GSAir closed 10 years ago

GSAir commented 10 years ago

Is there an easy way to compress before sending toBlob?

I see how to do it with save, but I would have to load, save and reload.

Can we add:

-- Set quality:
   quality = quality or 85
   clib.MagickSetCompressionQuality(self.wand, quality) 

in the function toBlob? I am not sure if it will do the trick though.

GSAir commented 10 years ago

It seems to work.

It may me think that the quality should be restore after the save function in order to avoid some problems no?

clementfarabet commented 10 years ago

I just commited a patch.

I think it's fine to have the quality be a side effect (?).

More concerning is the format issue: if you load a png, then the internal format is set to png, so any subsequent call you'll do to toBlob() will use the png encoder. Worse: if you create an gm.Image(tensor) from a tensor, which I often do, then toBlob() returns an empty blob, because no format is set I guess. Any idea on these?

clementfarabet commented 10 years ago

Scratch that, I provide a function format(), which you can use to do exactly that... I forgot about that:

local i = gm.Image('image.png')
i:format('JPG')
local res = i:toString(90)
-- res will contain a jpeg, encoded with a quality of 90%

require 'image'
local img = image.lena()
local i = gm.Image(img, 'RGB', 'DHW')
-- image has no format at that point
i:format('JPG')
local res = i:toString(90)
-- res will contain a jpeg, encoded with a quality of 90%
GSAir commented 10 years ago

Seems perfect, thanks. I was thinking that the wand was global, but in fact it is for each image, the side effect shouldn't be a problem.

Res will be an ascii format then? And i:toBlob() will be the byte representation?