我想限制使用“过滤查询参数(https://msdn.microsoft.com/en-us/library/dn760791.aspx)”。但是我总是得到尺寸约为250-300像素(宽和高)的照片,尽管我想要它们,它大于500 x 500像素。我知道已经有类似的问题(Bing Image Search API filter by image size),但我无法解决问题。
我正在使用Ruby,下面的代码。有什么问题吗?
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
我无法帮助解决Bing的问题,但是我制作了a website,它会根据您指定的高度和宽度搜索Google图片。抱歉,我无法解决实际的问题,但是...这是我能做到的最好。
[8K Image Search(又称我正在谈论的网站)]