1
0
Fork 0
mirror of synced 2024-06-20 20:00:15 +12:00

Use Image.toString instead of Image.toBlob

This commit is contained in:
nagadomi 2015-11-13 16:11:12 +09:00
parent 13dad2ec26
commit c345f35ed0
2 changed files with 11 additions and 11 deletions

View file

@ -32,7 +32,7 @@ function image_loader.encode_png(rgb, alpha, depth)
rgba[torch.gt(rgba, 1.0)] = 1.0
end
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
if depth < 16 then
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
end
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
function image_loader.save_png(filename, rgb, alpha, depth)
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")
if not fp then
error("IO error: " .. filename)
end
fp:write(ffi.string(blob, len))
fp:write(blob)
fp:close()
return true
end
@ -136,13 +136,13 @@ end
local function test()
torch.setdefaulttensortype("torch.FloatTensor")
local a = image_loader.load_float("../images/lena.png")
local blob, len = image_loader.encode_png(a)
local b = image_loader.decode_float(ffi.string(blob, len))
local blob = image_loader.encode_png(a)
local b = image_loader.decode_float(blob)
assert((b - a):abs():sum() == 0)
a = image_loader.load_byte("../images/lena.png")
blob, len = image_loader.encode_png(a)
b = image_loader.decode_byte(ffi.string(blob, len))
blob = image_loader.encode_png(a)
b = image_loader.decode_byte(blob)
assert((b:float() - a:float()):abs():sum() == 0)
end
--test()

View file

@ -186,11 +186,11 @@ function APIHandler:post()
end
end
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-Type", "image/png")
self:set_header("Content-Length", string.format("%d", len))
self:write(ffi.string(blob, len))
self:set_header("Content-Length", string.format("%d", #blob))
self:write(blob)
else
if not x then
self:set_status(400)