Creating image file from base64 data [duplicate] - ruby

This question already has answers here:
Open and save base64 encoded image data URI in Ruby
(4 answers)
Closed 5 years ago.
I have a base64 encoded image data . I am pasting the first few characters
string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD /2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopG R8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgo......'
I am doing following to it in ruby
decoded_string=Base64.decode64 string
output_file = Tempfile.new(['image','.jpeg'])
output_file.binmode
output_file.write image
After this when I am opening 'image.jpeg', It is giving error
Error interpreting JPEG image file (Not a JPEG file: starts with 0x75 0xab)
I also tried
File.open('a.jpeg', 'wb') do|f|
f.write decoded_string
end
In this case also, I got the same error.
What am I doing wrong?

File.open('shipping_label.gif', 'wb') do|f|
f.write(Base64.decode64(base_64_encoded_data))
end
This answer is from: How to save a base64 string as an image using ruby

Related

AWS Lambda replacing certain characters from attachments of multipart-form data with replacement character(U+FFFD)

I am using AWS lambda function to receive a multipart request with attachments and upload them to S3.
But Lambda function replaces few characters with the replacement character and so the attachment getting corrupt.
I checked on a PNG file.
sample content: \x89PNG\r\n\u001A\n\u0000\u0000\u0000
All the characters are received as they are but \x89 or in general \x** getting replaced by the replacement character(U+FFFD).
I am extracting the attachment file_str as a string and writing into a file and then uploading it to s3.
File.open(file_path, 'w') do |f|
f << file_str
end
Thanks in advance.
You need the binary mode on to work with (to write) binary files.
# ⇓ THIS
File.open(file_path, 'wb') do |f|
f << file_str
end
You try to store the content in UTF-8 and \x89 is not valid UTF-8.

Saving a GIF image with RMagick failure

I'm using ruby 2.1.0, rmagick 2.15.4, ImageMagick 6.7.7-10
I'd like to load a JPEG file and then save in the GIF format.
x = Magick::Image.read("a.jpg").first
puts "Start write..."
x.format = "GIF"
x.write("a.gif")
puts "Done."
Gives me this:
Start write...
cli.rb:104:in `exit': no implicit conversion from nil to integer (TypeError)
The stack trace includes foreman and thor gems, but no step in my code.
The filesystem has a.gif defined, but the filesize is zero.
UPDATE
I think I have a problem with ImageMagick itself. Here's what happens on the command line:
$ convert -debug a.jpg a.gif
convert.im6: unrecognized event type `a.jpg' #error/convert.c/ConvertImageCommand/1135.
It looks like this question was answered here:
JPG to PNG using RMagick
Just changing the file name will not convert he file format.
Yes it will. Something else is going on here.
"This makes it easy to convert an image file to another format. Simply write the image file using a name that has either a prefix or a suffix corresponding to the format you want."
https://rmagick.github.io/imusage.html

Restore jpeg file from its encoded64 code in Rails/Ruby

Our question is that with Base64 encoded jpeg image file in uploaded_io, how to restore jpeg file out of it?
The encoded uploaded_io is generated by canvas.toDataURL("image/jpeg"). Here is the uploaded_io looks like:
uploaded_io = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2....."
In ruby/rails 4, a base64 encoded file could be decoded with:
require 'base64'
decoded = Base64.decode64(uploaded_io.sub(/.+,/, '')) #removed file header 'data:image/jpeg;base64,' as suggested
We added the gem mini_magick (v3.5.0) and installed the image magick library on our computer. Did the following:
image = MiniMagick::Image.new(decoded)
However the image is not a jpeg image file and does not respond well to .type and .size. There is no need to manipulate the image file and we are not sure that weather mini_magick/image magick are really needed here.
One issue that stands out is your decoding the image then removing the header which will cause problems.
image = MiniMagick::Image(decoded.sub(/.+,/, ''))
I did a simple test encoding / decoding an image using Ruby Base64 and everything worked as expected.
irb example:
require 'base64'
e = Base64.encode64(IO.read('/path/to/jpeg'))
d = Base64.decode64(e)
File.open("test.jpg", "w") { |f| f.write(d) }
test.jpg should be a valid file. Confirm by executing file test.jpg.

How to convert PIL image file into string in python3.4?

I have been trying to read a jpeg file using PIL in python 3.4. I need to save this file into string format. Although some options are provided on this site but I have tried a few but it is not working. Following is my code snippet which i have found on this site only:-
from io import StringIO
fp = Image.open("images/login.jpg")
output = StringIO()
fp.save(output, format="JPEG")
contents = output.getvalue()
output.close()
But i am facing the following error :-
TypeError: string argument expected, got 'bytes'
Could you please suggest what I have done wrong and how to get this working?
In python 3 you should use a BytesIO,
whereas as read in python docs:
StringIO is a native in-memory unicode container
.
Thanks a lot for the hint. I Actually have a found a different way of reading the image file and storing in string object in python2.x . Here is the code. Please let me know if there is any disadvantage of using this.
imgText = open("images/login.jpg", 'rb')
imgTextStr = imgText.read()
imgText.close()

How to convert a Ruby object to JSON from a File? [duplicate]

This question already has answers here:
How to convert a Ruby object to JSON
(3 answers)
Closed 8 years ago.
I'm trying to use the Linguist gem: https://github.com/github/linguist
My code is:
require 'linguist'
filePath = ARGV
langDetails = Linguist::FileBlob.new(filePath)
puts langDetails
That outputs: #<Linguist::FileBlob:0x007faf93b17200>
However, when I do puts langDetails.language, I get
/Users/myuser/.rvm/gems/ruby-1.9.3-p545#linguist/gems/github-linguist-2.10.15/lib/linguist/file_blob.rb:39:in `stat': can't convert Array into String (TypeError)
from /Users/myuser/.rvm/gems/ruby-1.9.3-p545#linguist/gems/github-linguist-2.10.15/lib/linguist/file_blob.rb:39:in `mode'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p545#linguist/gems/github-linguist-2.10.15/lib/linguist/blob_helper.rb:294:in `language'
from ./linguist.rb:9:in `<main>'
I'm not entirely sure what I'm doing wrong. Ideally I want the data back as a JSON object. How do I accomplish this?
Look at the source. FileBlog is saying File.stat(#path).mode.to_s(8) but #path is an array. filePath needs to be a path string, but ARGV is an array.
Perhaps you meant ARGV[0]?

Resources