Bing Image Search API - V5 filter by image size (using Ruby)

I would like to limit my search to images using "filter query parameters ( https://msdn.microsoft.com/en-us/library/dn760791.aspx )". But I always get photos that are 250 to 300 pixels in size (both in width and height), although I want them to be larger than 500 x 500 pixels. I know there is already a similar question ( Bing Image API by Image Size ), but I couldn't solve the problem.

I am using Ruby and the code is as follows. What is the problem?

require "open-uri"
require "FileUtils"
require 'net/http'
require 'json'


@dirName = "/Users/hoge/img"
FileUtils.mkdir_p(@dirName) unless FileTest.exist?(@dirName)

def save_image(url, num)
  filePath = "#{@dirName}/christ#{num.to_s}.jpg"
  open(filePath, 'wb') do |output|
    open(url) do |data|
      output.write(data.read)
    end
  end
end

search_word = 'christ painting'
count = 5
size = 'Large'


uri = URI('https://api.cognitive.microsoft.com/bing/v5.0/images/search')
uri.query = URI.encode_www_form({
    'q' => search_word,
    'count' => count,

    'size' => size
})
request = Net::HTTP::Post.new(uri.request_uri)
request['Content-Type'] = 'multipart/form-data'
request['Ocp-Apim-Subscription-Key'] = 'mykey' # Fix Me
request.body = "{body}"
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(request)
end

count.times do |i|
  begin
    image_url = JSON.parse(response.body)["value"][i]["thumbnailUrl"]
    save_image(image_url, i)
  rescue => e
    puts "image#{i} is error!"
    puts e
  end
end

      

+3


source to share





All Articles