Need Help in R : How to convert image file stored in local computer to binary data

I want to convert an image file to binary data in order to send request to an api. The api body can accept only binary data. I tried to explore different solutions but its not working. Please guide me on this.

is this a public api whose documentation we could read?
theres a pedantic sense in which all data on a computer is 'binary' but thats not really any help to you :smiley: , is there some particular representation of pixels required ?

Yes its a public api. I need to send the file to the api to do face verification. The image file is stored in my local, so only way to send the same to api is by converting the image file to binary data.

Does the API have documentation?
Do they have an example of proper API use.
I'm afraid if won't be possible to hell you with the abstract question you have posed, as possible representations are myriad.

It’s Microsoft azure face detection api. The documentation is available here https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

Thanks, so I saw the following possibly relevant info:

  • JPEG, PNG, GIF (the first frame), and BMP format are supported. The allowed image file size is from 1KB to 6MB.
  • The minimum detectable face size is 36x36 pixels in an image no larger than 1920x1080 pixels. Images with dimensions higher than 1920x1080 pixels will need a proportionally larger minimum face size.
    and the requests take either url (which presumably be easy but require you to host an image on a server somewhere) or application/octet-stream, so assuming it is so accomodating of arbitrary binary data, given some assumption of JPG or PNG, or GIF formats we could try the simplest R approach and see if it works...
    my best guess.
library(jpeg)

# i saved the image at https://pbs.twimg.com/profile_images/905186381995147264/7zKAG5sY_400x400.jpg
# to the folder where this R script is 


hadley_jpeg <- readJPEG("7zKAG5sY_400x400.jpg", native = FALSE) # could try TRUE if false didnt work ?

  #show the image to screen
  plot(1:2,1:2, type='n')
  rasterImage(hadley_jpeg, 1, 1, 2, 2)
  
  
  #image dimensions
  dim(hadley_jpeg)
  # 400 400   3
  
  #
  binary_rep <- as.raw(hadley_jpeg)
  text_of_binary_rep <- paste0(as.character(binary_rep),collapse = "")

Thank you! I will try this and let you know how it goes.

Thank You. I tried the code, its converting the jpeg into binary. But in my case, original image size is 8 kb and when it is getting converted to binary the size is getting increased to 1.3 mb.
text_of_binary_rep = Large character (1.3 mb)

Is there any way to compress the size?

# i saved the image at https://pbs.twimg.com/profile_images/905186381995147264/7zKAG5sY_400x400.jpg
# to the folder where this R script is 

my_filename <- "7zKAG5sY_400x400.jpg"
my_raw <- readBin(my_filename, what=raw(), n=file.info(my_filename)$size)
binary_rep <- rawToBits(my_raw)
position_of_1s <- which(binary_rep==1)

text_of_binary_rep <- rep('0',length(binary_rep))

text_of_binary_rep[position_of_1s] <- '1'

to_send <- paste0(text_of_binary_rep, collapse = "")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.