-2

Is it possible to check that a picture that I downloaded is readable, if a picture viewer is going to tell me it cannot open it for some reason.

Example:

"invalid byte sequence in conversion input" when mousepad is used because Ephoto could not open it on Linux ) in Ruby

I download my picture with OpenURI and then output it in a file. Is there a function/gem that would allow to do that?

pic_buffer = open(my_link, "User-Agent" => "Ruby/#{RUBY_VERSION}")
if function_to_check_if_the_picture_is_readable(pic_buffer) == false
  abort("file is unreadable")
end
if pic_buffer != nil
  File.open(name_buffer + ".jpg", 'wb') do |pic|
    pic << pic_buffer.read
  end
end

I am downloading JPEG pictures only.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • nope, I would like to know how to do it in Ruby – Matthieu Raynaud de Fitte Jun 20 '16 at 19:36
  • @Mo H. changed the question ( thanks for the advice ) – Matthieu Raynaud de Fitte Jun 20 '16 at 19:48
  • Welcome to Stack Overflow. Please read "[ask]" including the linked pages. We need more information. OpenURI is simply a pipe from a server to your code. What you do with the received data is very important, but you didn't tell us anything useful. We need the smallest version of your code that demonstrates the problem. Please read "[mcve]" for the requirements. Define "is readable". Readable by the account that's running the code? Readable by an image viewer, because the file is correctly formatted? Your question is broad. Please ask a specific, detailed question. – the Tin Man Jun 20 '16 at 19:48
  • thanks, I will correct it right now – Matthieu Raynaud de Fitte Jun 20 '16 at 19:51
  • @the tin Man did the corrections, is it good now ? ( I am sorry, I am not very familiar with the stack overflow question methodology ) – Matthieu Raynaud de Fitte Jun 20 '16 at 19:59
  • 1
    By reading the help pages and reviewing other questions you'll figure it out. SO is an online reference book and you're creating an article in that book to help others in the future. It's not necessary to tell us you made a change, we can see when and what changed because SO maintains an edit history. – the Tin Man Jun 20 '16 at 20:02
  • 1
    I did not know, thanks for the tip – Matthieu Raynaud de Fitte Jun 20 '16 at 20:04
  • This question does not appear to be a duplicate of the [nominated exemplar](http://stackoverflow.com/q/5138797/238886): That question is for Delphu; this one for Ruby. – Wayne Conrad Jun 20 '16 at 22:27

1 Answers1

2

The only real way to tell if a JPEG image (as true with most compressed image formats) is to decompress it. You can do some sanity checks on the stream structure that will show some streams that cannot open. However, errors in the compressed data can only be found by expanding them. There is no CRC checking in JPEG as there is in PNG.

user3344003
  • 20,574
  • 3
  • 26
  • 62