How to generate a image file from base 64 string in Lua? - image

I can get the base 64 string from WCF ,and need to convert it into image file by Lua?
Any one have idea about this?
thanks

You can do something like this with LuaSocket:
local ltn12 = require "ltn12"
local mime = require "mime"
mystring = "somedata"
myoutfile = "out.gif"
ltn12.pump.all(
ltn12.source.string(mystring),
ltn12.sink.chain(
mime.decode("base64"),
ltn12.sink.file(io.open(outfile,"w"))
)
)

There is a base64 C library for Lua at http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbase64.

If you're using luarocks:
luarocks install lbase64
Also check this out:
https://github.com/LuaDist/lbase64

Related

Retrieve bibtex data from crossref by sending DOI from matlab: translation from ruby

I want to retrieve bibtex data (for building a bibliography) by sending a DOI (Digital Object Identifier) to http://www.crossref.org from within matlab.
The crossref API suggests something like this:
curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842
based on this source.
Another example from here suggests the following in ruby:
open("http://dx.doi.org/10.1038/nrd842","Accept" => "text/bibliography; style=bibtex"){|f| f.each {|line| print line}}
Although I've heard ruby rocks I want to do this in matlab and have no clue how to translate the ruby message or interpret the crossref command.
The following is what I have so far to send a doi to crossref and retrieve data in xml (in variable retdat), but not bibtex, format:
clear
clc
doi = '10.1038/nrd842';
URL_PATTERN = 'http://dx.doi.org/%s';
fetchurl = sprintf(URL_PATTERN,doi);
numinputs = 1;
www = java.net.URL(fetchurl);
is = www.openStream;
%Read stream of data
isr = java.io.InputStreamReader(is);
br = java.io.BufferedReader(isr);
%Parse return data
retdat = [];
next_line = toCharArray(br.readLine)'; %First line contains headings, determine length
%Loop through data
while ischar(next_line)
retdat = [retdat, 13, next_line];
tmp = br.readLine;
try
next_line = toCharArray(tmp)';
if strcmp(next_line,'M END')
next_line = [];
break
end
catch
break;
end
end
%Cleanup java objects
br.close;
isr.close;
is.close;
Help translating the ruby statement to something matlab can send using a script such as that posted to establish the communication with crossref would be greatly appreciated.
Edit:
Additional constraints include backward compatibility of the code (back at least to R14) :>(. Also, no use of ruby, since that solves the problem but is not a "matlab" solution, see here for how to invoke ruby from matlab via system('ruby script.rb').
You can easily edit urlread for what you need. I won't post my modified urlread function code due to copyright.
In urlread, (mine is at C:\Program Files\MATLAB\R2012a\toolbox\matlab\iofun\urlread.m), as the least elegant solution:
Right before "% Read the data from the connection." I added:
urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');
The answer from user2034006 lays the path to a solution.
The following script works when urlread is modified:
URL_PATTERN = 'http://dx.doi.org/%s';
doi = '10.1038/nrd842';
fetchurl = sprintf(URL_PATTERN,doi);
method = 'post';
params= {};
[string,status] = urlread(fetchurl,method,params);
The modification in urlread is not identical to the suggestion of user2034006. Things worked when the line
urlConnection.setRequestProperty('Content-Type','application/x-www-form-urlencoded');
in urlread was replaced with
urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');

Cannot open file for output (Matlab)

I am having a problem in matlab and the problem is described as follows:
When i try to read an image ( I have several images) and write them to a specific folder, the matlab triggers an error saying
Error using ==> imwrite at 394
Can't open file "\Temp\\inim735282.4716703009300000.jpg" for writing.
You may not have write permission.
May I know why this is happening?
this is the code where the problem occurs
mkdir('.\Temp');
temp_name = sprintf('%.16f',now);
corner_file = ['\Temp\corners', temp_name,'.in'];
image_file = ['\Temp\inim', temp_name,'.jpg'];
out_file = ['\Temp\out', temp_name,'.desc'];
out_imname = ['\Temp\out', temp_name,'.desc.jpg'];
I tried to change it by omitting
mkdir('.\Temp');
moreoever, i direct the path in the folder to the folder by doing this
binary_path = 'C:\Users\cool\Documents\MATLAB\Experment\experiments\bag_of_words\Temp';
to read and and write in and out of the folder.
Can someone please help me figure out this problem?
Thank you guys
Open MatLAB with admin privileges.
A few suggestions:
To generate a temporary output name use the command tempname.
temp_name = tempname();
To concatenate paths and file names use fullfile.
conrner_file = fullfile( '\', 'Temp', 'corners', [temp_name, '.in'] );
You should be careful not to mix '\Temp' and '.\Temp': as the first is an absolute path, while the second is a relative path to cwd.
EDIT:
How about:
temp_name = tempname(); % temp name + folder name in TEMP
corner_file = [ temp_name,'.in'];
image_file = [ temp_name,'.jpg'];
out_file = [temp_name,'.desc'];
out_imname = [temp_name,'.desc.jpg'];
Is it working now?

Ruby-mp3info album artwork

I have this gem working such that I can change the id3 data for a given song. However I need to also be able to add album artwork to the song. I have the artwork at a given URL. How do I go about this?
Mp3Info.open(file.path) do |mp3|
mp3.tag.title = title
mp3.tag.artist = artist
end
It seems ruby-mp3info only supports text frames at the moment, see here: https://github.com/moumar/ruby-mp3info/blob/v0.7.1/lib/mp3info/id3v2.rb#L319
Using taglib-ruby, it would work like this:
require 'taglib'
require 'open-uri'
picture_data = open(picture_url).read
TagLib::MPEG::File.open(file.path) do |file|
tag = file.id3v2_tag
pic = TagLib::ID3v2::AttachedPictureFrame.new
pic.picture = picture_data
pic.mime_type = "image/jpeg"
pic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
tag.add_frame(pic)
file.save
end
If you are not stuck with mp3Info gem, try using id3Lib, http://id3lib-ruby.rubyforge.org/. From my experience, that one's better.
not sure about this, but try reading the file and setting it directly to
mp3.tag2.APIC
Using ruby-mp3info you can add artwork:
From the documentation:
file = File.new('input_img','rb')
Mp3Info.open '1.mp3' do |m|
m.tag2.add_picture(file.read)
end

How to check if image is available in ruby?

I guess this is easy, I seem to be missing something simple.
I need to read an image and then display it which i do as follows, I should skip the code if the image is not available.
stu_image ="/admin_num.jpg"
if !stu_image
code for displaying the image
end
But, this is not working. Could somebody help with this please.
Cheers!
Seems to me like you're just saving a string, not the image.
this_dir = File.dirname( __FILE__ )
file = File.expand_path( "admin_num.jpg", this_dir )
Why not just testing if the file exists?
stu_image ="/admin_num.jpg"
FileTest.exists?(RAILS_ROOT + stu_image ) # < rails 3.0
FileTest.exists?(Rails.root + stu_image ) # >= rails 3.0
See File#exists? here:
http://ruby-doc.org/core-1.9.3/File.html#method-c-exists-3F

Jython, ImageInfo

I trying to use ImageInfo and Jython to get information from a image on my harddrive.
I have imported the module fine but keep getting this error:
TypeError: setInput(): expected 2 args; got 1
And this is the code I am trying to use:
filename = "C:\\image.jpg"
img = ImageInfo.setInput(filename)
Could anyone point out what I am doing wrong.
Cheers
Eef
The missing argument Jython complains about is the ImageInfo object itself, which doesn't exist yet. You must construct it first. So:
filename = "C:\\image.jpg"
ii = ImageInfo()
img = ii.setInput(filename)
or
filename = "C:\\image.jpg"
img = ImageInfo().setInput(filename)
may work also.

Resources