文生图买不起,试试用必应的bing搜索图片服务

首先需要一个API key,可以在这里先登录:

Microsoft Azure

然后创建资源 – > 搜索 bing serach --> 选择 bing search v7

定价层选F1,每月有1000的额度,注意勾选最后的(我确认本人已阅读并理解下面的声明。)

审阅并创建后,会跳转到该资源,如果以后要查看该资源,在主页(Microsoft Azure)可以看到资源列表。

点开这个搜索资源,页面里有示例代码:

#Copyright (c) Microsoft Corporation. All rights reserved.
#Licensed under the MIT License.

# -*- coding: utf-8 -*-

import json
import os 
from pprint import pprint
import requests

'''
This sample makes a call to the Bing Web Search API with a query and returns relevant web search.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
'''

# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscription_key = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/bing/v7.0/search"

# Query term(s) to search for. 
query = "Microsoft"

# Construct a request
mkt = 'en-US'
params = { 'q': query, 'mkt': mkt }
headers = { 'Ocp-Apim-Subscription-Key': subscription_key }

# Call the API
try:
    response = requests.get(endpoint, headers=headers, params=params)
    response.raise_for_status()

    print("
Headers:
")
    print(response.headers)

    print("
JSON Response:
")
    pprint(response.json())
except Exception as ex:
    raise ex

把endpoint改为:

"https://api.bing.microsoft.com/v7.0/images/search"

subscription_key改为页面中可以点开看到的key,不过程序有小点错误。

mkt改为zh-CN,代码可参考(Market and language codes used by the Bing Visual Search API - Bing Services | Microsoft Learn

query 改为 "刺客+月光下",按这个搜索

过滤条件,只要横幅的图片,高分辨率

'minWidth':1280,

'maxHeight':1280,

'minHeight':640

最后把返回存为json文件:

with open(file='image_list.json',mode="w",encoding="utf-8") as f:

f.write(str(response.text))

请求结果,总共544条命中,可以看出宽高都满足要求:

	
_type	"Images"
instrumentation	
_type	"ResponseInstrumentation"
readLink	"images/search?q=刺客+月光下"
webSearchUrl	"https://www.bing.com/images/search?q=刺客+月光下&FORM=OIIARP"
queryContext	
originalQuery	"刺客+月光下"
alterationDisplayQuery	"刺客+月光下"
alterationOverrideQuery	"+刺客+月光下"
alterationMethod	"AM_JustChangeIt"
alterationType	"CombinedAlterationsChained"
totalEstimatedMatches	544
nextOffset	30
currentOffset	0
value	
0	
webSearchUrl	"https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=%E5%88%BA%E5%AE%A2%2b%E6%9C%88%E5%85%89%E4%B8%8B&id=CBC7F6C635655098200110CF78C28BB3EC2DE70C&simid=608025927915498584"
name	"刺客|插画|游戏原画|SI__YUAN - 原创作品 - 站酷 (ZCOOL)"
thumbnailUrl	"https://ts2.cn.mm.bing.net/th?id=OIP-C.YLlAnPrEer2IRDJ5AWZ6AwHaEc&pid=Api"
datePublished	"2020-09-03T23:00:00.0000000Z"
isFamilyFriendly	true
contentUrl	"https://img.zcool.cn/community/0138495ba75e5aa8012099c807c5d9.jpg@1280w_1l_2o_100sh.jpg"
hostPageUrl	"https://www.zcool.com.cn/work/ZMzAzODYzODg=.html"
contentSize	"411352 B"
encodingFormat	"jpeg"
hostPageDisplayUrl	"https://www.zcool.com.cn/work/ZMzAzODYzODg=.html"
width	1280
height	768
hostPageFavIconUrl	"https://www.bing.com/th?id=ODF.JEyGtERG1FO2k2Kl-ibPdA&pid=Api"
hostPageDiscoveredDate	"2018-10-17T00:00:00.0000000Z"
isTransparent	false
thumbnail	
width	474
height	284
imageInsightsToken	"ccid_YLlAnPrE*cp_C745C2185F7E74C4C8A62D6AEB61977C*mid_CBC7F6C635655098200110CF78C28BB3EC2DE70C*simid_608025927915498584*thid_OIP.YLlAnPrEer2IRDJ5AWZ6AwHaEc"
insightsMetadata	
pagesIncludingCount	4
availableSizesCount	3
imageId	"CBC7F6C635655098200110CF78C28BB3EC2DE70C"
accentColor	"AE9B1D"
1	
webSearchUrl	"https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=%E5%88%BA%E5%AE%A2%2b%E6%9C%88%E5%85%89%E4%B8%8B&id=74335DBDE7128047E252B596CC3AD88972B5214C&simid=608018798297695190"
name	"刺客|插画|概念设定|fengweic12 - 原创作品 - 站酷 (ZCOOL)"
thumbnailUrl	"https://ts4.cn.mm.bing.net/th?id=OIP-C.sO9BRnNrkfQB8k718Y4U5AHaEo&pid=Api"
datePublished	"2021-08-02T14:30:00.0000000Z"
isFamilyFriendly	true
contentUrl	"https://img.zcool.cn/community/01aada554953e70000019ae91b9012.jpg@1280w_1l_2o_100sh.jpg"
hostPageUrl	"https://www.zcool.com.cn/work/ZNjQ0NTEy.html"
contentSize	"420890 B"
encodingFormat	"jpeg"
hostPageDisplayUrl	"https://www.zcool.com.cn/work/ZNjQ0NTEy.html"
width	1280
height	800
hostPageFavIconUrl	"https://www.bing.com/th?id=ODF.JEyGtERG1FO2k2Kl-ibPdA&pid=Api"
hostPageDiscoveredDate	"2018-11-12T00:00:00.0000000Z"
isTransparent	false
thumbnail	
width	474
height	296
imageInsightsToken	"ccid_sO9BRnNr*cp_0CDEE98093951038C22771FF502ADAB0*mid_74335DBDE7128047E252B596CC3AD88972B5214C*simid_608018798297695190*thid_OIP.sO9BRnNrkfQB8k718Y4U5AHaEo"
insightsMetadata	
pagesIncludingCount	1
availableSizesCount	1
imageId	"74335DBDE7128047E252B596CC3AD88972B5214C"
accentColor	"2597A6"
2	
webSearchUrl	"https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=%E5%88%BA%E5%AE%A2%2b%E6%9C%88%E5%85%89%E4%B8%8B&id=2C8A3040C263BB0C8A935325E85838D8A928857F&simid=608023226415787705"
name	"《刺客信条4》壁纸11_游戏新闻__单机游戏_新浪游戏_新浪网"
thumbnailUrl	"https://ts4.cn.mm.bing.net/th?id=OIP-C.tA3NGAvNWkDVRyYZrP6W0gHaEK&pid=Api"
datePublished	"2013-09-01T14:50:00.0000000Z"
isFamilyFriendly	true
contentUrl	"http://i1.sinaimg.cn/gm/j/i/2013-07-31/U4341P115T41D243969F757DT20130731151822.jpg"
hostPageUrl	"http://games.sina.com.cn/j/i/2013-07-31/1518243969.shtml"
contentSize	"679314 B"
encodingFormat	"jpeg"
hostPageDisplayUrl	"games.sina.com.cn/j/i/2013-07-31/1518243969.shtml"
width	1920
height	1080
hostPageFavIconUrl	"https://www.bing.com/th?id=ODF.9HJur1uaiYYuDkDT1cIQZQ&pid=Api"
hostPageDomainFriendlyName	"新浪网"
hostPageDiscoveredDate	"2013-09-01T14:50:00.0000000Z"
isTransparent	false
thumbnail	
width	474
height	266
imageInsightsToken	"ccid_tA3NGAvN*cp_721E22113F2571D0366D91CD1BED4982*mid_2C8A3040C263BB0C8A935325E85838D8A928857F*simid_608023226415787705*thid_OIP.tA3NGAvNWkDVRyYZrP6W0gHaEK"
insightsMetadata	
pagesIncludingCount	37
availableSizesCount	10
imageId	"2C8A3040C263BB0C8A935325E85838D8A928857F"
accentColor	"348397"

下载前面几个看看内容,好像没有月光,这个要怪bing:

再试试query = "月光下+刺客",结果有点区别:

改进后的示例代码,未封装:

#Copyright (c) Microsoft Corporation. All rights reserved.
#Licensed under the MIT License.

# -*- coding: utf-8 -*-

import json
import os 
from pprint import pprint
import requests

'''
This sample makes a call to the Bing Web Search API with a query and returns relevant web search.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
'''

# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscription_key = "YOURKEY"
endpoint = "https://api.bing.microsoft.com/v7.0/images/search"

# Query term(s) to search for. 
query = "刺客+月光下"

# Construct a request
mkt = 'zh-CN'    #'en-US'
params = { 'q': query,
            'mkt': mkt,
            'count':30,
            'minWidth':1280,
            'maxHeight':1280,
            'minHeight':640 }
headers = { 'Ocp-Apim-Subscription-Key': subscription_key }

# Call the API
try:
    response = requests.get(endpoint, headers=headers, params=params)
    response.raise_for_status()

    print("Headers:")
    print(response.headers)

    print("JSON Response:")
    pprint(response.json())
    print(response.text)
    with open(file='image_list.json',mode="w",encoding="utf-8") as f:
        f.write(str(response.text))
except Exception as ex:
    raise ex

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值