Convert Base64 String to Image file in Groovy - image

i checked with old threads but didn't find anything helpful.
i am a JD Edwards developer and we have a requirement in jde Orchestrator to process base64 string.
Can anyone help me and share full code for this?
i am new in groovy script.

def base64str = 'R0lGODlhAwADAHAAACwAAAAAAwADAIHsHCT97KYAAAAAAAACBIQRBwUAOw==' // < base64 string with 3x3 gif inside
def filename = System.properties['user.home']+'/documents/my.gif' // < filename with path
// save decoded base64 bytes into file
new File(filename).bytes = base64str.decodeBase64()
as a result you there should be a new file my.gif in current user/ documents folder
with 3x3 pixels image (43 bytes file)

Related

How to convert Embedded image to Base64 string in xamarin forms..?

I want to convert an Embedded image to base 64 string. The image is in PCL solution so let me know how to convert an image into base64. As I tried lots of ways but I am not getting the file path properly. So please help me with this.
//file to base64 string
byte[] b = System.IO.File.ReadAllBytes(FileName);
String s = Convert.ToBase64String(b);
//base64 string to file
byte[] data = Convert.FromBase64String(s);
System.IO.File.WriteAllBytes(FileName2, data);
Since it is an embedded resource getting the path for the image is a problem to convert it into a stream, you can take the following steps to get the path of your embedded image:
string imagePath = "NameOfProject.Assets.applicationIcon.png";
Note: This is the sample path in your case you will give your path, Where the name of the project is the name of the project, Assets is the folder in which I have the image and application icon is the image. (I hope you understood what I am doing here)
After that get the Assembly details something like this:
Assembly assembly = typeof(NameOfClass).GetTypeInfo().Assembly;
Then inside a using statement convert your image into a stream, Something like this
string result;
using (Stream stream = assembly.GetManifestResourceStream(imagePath))
{
long length = stream.Length;
byte[] buffer = new byte[length];
stream.Read(buffer, 0, (int)length);
result = Convert.ToBase64String(data);
}
Revert in case of queries.

Reading image file (file storage object) using OpenCV

I am sending an image by curl to flask server, i am using this curl command
curl -F "file=#image.jpg" http://localhost:8000/home
and I am trying to read the file using OpenCV on the server side.
On the server side I handle the image by this code
#app.route('/home', methods=['POST'])
def home():
data =request.files['file']
img = cv.imread(data)
fact_resp= model.predict(img)
return jsonify(fact_resp)
I am getting this error-
img = cv.imread(data)
TypeError: expected string or Unicode object, FileStorage found
How do I read the file using OpenCV on the server side?
Thanks!
I had similar issues while using opencv with flask server, for that first i saved the image to disk and read that image using saved filepath again using cv.imread()
Here is a sample code:
data =request.files['file']
filename = secure_filename(file.filename) # save file
filepath = os.path.join(app.config['imgdir'], filename);
file.save(filepath)
cv.imread(filepath)
But now i have got even more efficient approach from here by using cv.imdecode() to read image from numpy array as below:
#read image file string data
filestr = request.files['file'].read()
#convert string data to numpy array
file_bytes = numpy.fromstring(filestr, numpy.uint8)
# convert numpy array to image
img = cv.imdecode(file_bytes, cv.IMREAD_UNCHANGED)
After a bit of experimentation, I myself figured out a way to read the file using CV2.
For this I first read the image using PIL.image method
This is my code,
#app.route('/home', methods=['POST'])
def home():
data =request.files['file']
img = Image.open(request.files['file'])
img = np.array(img)
img = cv2.resize(img,(224,224))
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
fact_resp= model.predict(img)
return jsonify(fact_resp)
I wonder if there is any straight forward way to do this without using PIL.
So incase you want to do something like ,
file = request.files['file']
img = cv.imread(file)
then do it like this
import numpy as np
file = request.files['file']
file_bytes = np.fromfile(file, np.uint8)
file = cv.imdecode(file_bytes, cv.IMREAD_COLOR)
Now you don't need to do cv.imread() again, but can use this in the next line of codes.
This applies to OpenCV v3.x and onwards
Two-line solution, change grayscale to what you need
file_bytes = numpy.fromfile(request.files['image'], numpy.uint8)
# convert numpy array to image
img = cv.imdecode(file_bytes, cv.IMREAD_GRAYSCALE)

How to convert imageSet to idx3-ubyte format, using mnisten,

I am trying to use a CNN code to train 10 images stored in an imageSet. The CNN code rather uses the idx3-ubyte format.
I want to know how to convert from my imageSet data to idx3-ubyte format.
I came across the mnisten command below, but I don't have any Idea how to use it.
Please help.
%Here is my imageSet code that I want to convert to idx3-ubyte format.
%% Load image dataset
imgFolder1 = fullfile('C:\Users\Jay\Desktop\practical-cnn-2015a\NairaNotes');
trainingSet = imageSet(imgFolder1, 'recursive');
%%
for digit = 1:numel(trainingSet)
numImages = trainingSet(digit).Count;
for i = 1:numImages
img = read(trainingSet(digit), i);
im = rgb2gray(im2single(read(trainingSet(digit), i)));
end
end
%% here is the mnisten command I got, but I don't have an idea how to use it
mnisten -d my_image_files_directory_name -o my_prefix -s 32x32
Since I assume you're on Windows, check out this guide.

Convert image from sitecore field to base64 string

I need some help with an issue if anyone could help me. I am trying get an image from a Sitecore field and convert it afterwards to a base64 string.
What i have done is getting that content into an ImageField datatype, but i can't seem to find a solution to convert it to base64.
Sitecore.Data.Fields.ImageField img = itm.Fields["image"];
Is anyone able to help me?
Best regards,
Ionut.
You need to get the media item linked from the image field and create the base64 string from the media item file stream:
ImageField imageField = itm.Fields["Image"];
MediaItem mediaItem = imageField.MediaItem;
Stream stream = mediaItem.GetMediaStream();
Byte[] bytes = new Byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
img64.Src = "data:" + mediaItem.MimeType + ";base64," + Convert.ToBase64String(bytes);

Python Wand open a img file as blob, md5 is incorrect. Is this the Wand's bug?

Python Wand open a img file as blob, md5 is incorrect.
with Image(filename=picture) as img:
blob = img.make_blob()
print 'blob md5', hashlib.md5(blob).hexdigest()
with open(picture, 'rb') as img:
content = img.read()
print 'content md5', hashlib.md5(content).hexdigest()
.make_blob() method does not write the exactly same binary to its source file. Use .signature property instead if you want the signature of the image pixels, not file representation.

Resources