Use Image.toString instead of Image.toBlob
This commit is contained in:
parent
13dad2ec26
commit
c345f35ed0
2 changed files with 11 additions and 11 deletions
|
@ -32,7 +32,7 @@ function image_loader.encode_png(rgb, alpha, depth)
|
||||||
rgba[torch.gt(rgba, 1.0)] = 1.0
|
rgba[torch.gt(rgba, 1.0)] = 1.0
|
||||||
end
|
end
|
||||||
local im = gm.Image():fromTensor(rgba, "RGBA", "DHW")
|
local im = gm.Image():fromTensor(rgba, "RGBA", "DHW")
|
||||||
return im:depth(depth):format("PNG"):toBlob(9)
|
return im:depth(depth):format("PNG"):toString(9)
|
||||||
else
|
else
|
||||||
if depth < 16 then
|
if depth < 16 then
|
||||||
rgb = rgb:clone():add(clip_eps8)
|
rgb = rgb:clone():add(clip_eps8)
|
||||||
|
@ -44,17 +44,17 @@ function image_loader.encode_png(rgb, alpha, depth)
|
||||||
rgb[torch.gt(rgb, 1.0)] = 1.0
|
rgb[torch.gt(rgb, 1.0)] = 1.0
|
||||||
end
|
end
|
||||||
local im = gm.Image(rgb, "RGB", "DHW")
|
local im = gm.Image(rgb, "RGB", "DHW")
|
||||||
return im:depth(depth):format("PNG"):toBlob(9)
|
return im:depth(depth):format("PNG"):toString(9)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function image_loader.save_png(filename, rgb, alpha, depth)
|
function image_loader.save_png(filename, rgb, alpha, depth)
|
||||||
depth = depth or 8
|
depth = depth or 8
|
||||||
local blob, len = image_loader.encode_png(rgb, alpha, depth)
|
local blob = image_loader.encode_png(rgb, alpha, depth)
|
||||||
local fp = io.open(filename, "wb")
|
local fp = io.open(filename, "wb")
|
||||||
if not fp then
|
if not fp then
|
||||||
error("IO error: " .. filename)
|
error("IO error: " .. filename)
|
||||||
end
|
end
|
||||||
fp:write(ffi.string(blob, len))
|
fp:write(blob)
|
||||||
fp:close()
|
fp:close()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
@ -136,13 +136,13 @@ end
|
||||||
local function test()
|
local function test()
|
||||||
torch.setdefaulttensortype("torch.FloatTensor")
|
torch.setdefaulttensortype("torch.FloatTensor")
|
||||||
local a = image_loader.load_float("../images/lena.png")
|
local a = image_loader.load_float("../images/lena.png")
|
||||||
local blob, len = image_loader.encode_png(a)
|
local blob = image_loader.encode_png(a)
|
||||||
local b = image_loader.decode_float(ffi.string(blob, len))
|
local b = image_loader.decode_float(blob)
|
||||||
assert((b - a):abs():sum() == 0)
|
assert((b - a):abs():sum() == 0)
|
||||||
|
|
||||||
a = image_loader.load_byte("../images/lena.png")
|
a = image_loader.load_byte("../images/lena.png")
|
||||||
blob, len = image_loader.encode_png(a)
|
blob = image_loader.encode_png(a)
|
||||||
b = image_loader.decode_byte(ffi.string(blob, len))
|
b = image_loader.decode_byte(blob)
|
||||||
assert((b:float() - a:float()):abs():sum() == 0)
|
assert((b:float() - a:float()):abs():sum() == 0)
|
||||||
end
|
end
|
||||||
--test()
|
--test()
|
||||||
|
|
6
web.lua
6
web.lua
|
@ -186,11 +186,11 @@ function APIHandler:post()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local name = uuid() .. ".png"
|
local name = uuid() .. ".png"
|
||||||
local blob, len = image_loader.encode_png(x, alpha)
|
local blob = image_loader.encode_png(x, alpha)
|
||||||
self:set_header("Content-Disposition", string.format('filename="%s"', name))
|
self:set_header("Content-Disposition", string.format('filename="%s"', name))
|
||||||
self:set_header("Content-Type", "image/png")
|
self:set_header("Content-Type", "image/png")
|
||||||
self:set_header("Content-Length", string.format("%d", len))
|
self:set_header("Content-Length", string.format("%d", #blob))
|
||||||
self:write(ffi.string(blob, len))
|
self:write(blob)
|
||||||
else
|
else
|
||||||
if not x then
|
if not x then
|
||||||
self:set_status(400)
|
self:set_status(400)
|
||||||
|
|
Loading…
Reference in a new issue