1

I use python to read an image from url and save it, by using opencv imdecode: read url demo

import numpy as np
import urllib
import cv2

def url_to_image(url):
 resp = urllib.urlopen(url)
 data = bytearray(resp.read())
 image = np.asarray(data, dtype="uint8")
 image = cv2.imdecode(image, cv2.IMREAD_COLOR)

 return image

In my understanding: decoding is turing some encoded data into un-encoded data, for example, when you read .jpg, you need to decode the Jpeg2000 code into readable data. When you decode a 64base code, you need to do the 64base code into regular number. In a word, I think decoding need to change the data or data length.

In this code, I think "resp.read()" already read and decoded the raw bits flow (01001010...) to uint8 numbers(7,8,123,255...) And what "imdecode" did is only turning the long pix array into opencv format, it is only the re-ordering pixel instead of decoding. In this process, 255 is also 255, the data length and data itself didn't change.

As the demo says:"To reshape the array into a 2D format, assuming 3 components per pixel (i.e. the Red, Green, and Blue components, respectively), we make a call to cv2.imdecode "

If so, why imdecode called decode since it didn't do any decoding work?

David Ding
  • 680
  • 3
  • 9
  • 19
  • 1
    That would be the case if the image in question was stored in raw format, without any metadata. This is usually not the case. You mention JPEG -- just as if you just read the file from a disc, you will have an array of bytes. `imdecode` has to interpret [what they mean](https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure) (and perform decompression, colour conversion, etc.) – Dan Mašek Jun 11 '17 at 15:52
  • 1
    "I think "resp.read()" already read and decoded the raw bits flow (01001010...) to uint8 numbers(7,8,123,255...)" - This probably doesn't decode the image. Decoding an image means to provide raw pixel data (instead of for example an encoded and compressed jpeg data where you couldn't access the pixels). – Micka Jun 11 '17 at 22:28
  • Thank you for answering. I checked the file size of the output of resp.read and imdecode. The first one is much smaller than the second one. It means the raw data is encoded, and imdeocode was doing decoding work. – David Ding Jun 13 '17 at 01:53

0 Answers0