【问题标题】:Have an image link from iMDB api, how can I change it's size pyton有来自 iMDB api 的图像链接,我怎样才能改变它的大小 pyton
【发布时间】:2022-01-21 04:20:43
【问题描述】:

我正在尝试使用 IMDb API。到目前为止我的代码是

import http.client
import json
import requests

conn = http.client.HTTPSConnection("imdb-api.com", 443)
payload = ''
headers = {'User-agent': 'Chrome/95.0'}
conn.request("GET", "https://imdb-api.com/en/API/MostPopularMovies/<API_Key>",headers=headers)
res = conn.getresponse()
data = res.read()
convertedDict = json.loads(data.decode("utf-8"))
imagepath = r'venv/files/image.jpeg'
req = requests.get(convertedDict['items'][0]['image'], headers=headers)

with open(imagepath, 'wb') as file:
   file.write(req.content)

这让我可以下载第一部流行电影的图像,但是图像尺寸非常小。这是我正在下载的链接。我知道如果我在@之后摆脱所有东西,图像会变得更大。有没有办法编辑链接,以便我可以在 @ 之后删除所有内容,甚至使用代码编辑 UX 之后的数字? 我尝试对字符串或 URL 操作进行的所有操作都会给我一个错误

https://m.media-amazon.com/images/M/MV5BZWMyYzFjYTYtNTRjYi00OGExLWE2YzgtOGRmYjAxZTU3NzBiXkEyXkFqcGdeQXVyMzQ0MzA0NTM@._V1_UX128_CR0,3,128,176_AL_.jpg

提前谢谢你

【问题讨论】:

    标签: python api imdb


    【解决方案1】:

    说明

    (下面的代码示例)

    以下是获得所需尺寸的更大图像的方法。鉴于此网址,

     https://m.media-amazon.com/images/M/MV5BZWMyYzFjYTYtNTRjYi00OGExLWE2YzgtOGRmYjAxZTU3NzBiXkEyXkFqcGdeQXVyMzQ0MzA0NTM@._V1_UX128_CR0,3,128,176_AL_.jpg
    

    它有一个子串:

     UX128_CR0,3,128,176
    

    这包含三个重要部分:

    1. 前128个按宽度调整图片大小,保持比例
    2. 第二个128控制图片出现的容器宽度
    3. 176 控制图片出现的容器高度。

    所以,我们可以这样查看结构:

     UX<image_width>_CR0,3,<container_width>,<container_height>
    

    例如,将图像尺寸加倍:

     UX256_CR0,3,256,352_AL_.jpg
    

    (点此查看:https://m.media-amazon.com/images/M/MV5BZWMyYzFjYTYtNTRjYi00OGExLWE2YzgtOGRmYjAxZTU3NzBiXkEyXkFqcGdeQXVyMzQ0MzA0NTM@.V1_UX256_CR0,3,256,352_AL.jpg

    更新:如何在 Python 中执行此操作的示例。

    import re
    
    resize_factor = 2 # Image size multiple
    url = "https://m.media-amazon.com/images/M/MV5BZWMyYzFjYTYtNTRjYi00OGExLWE2YzgtOGRmYjAxZTU3NzBiXkEyXkFqcGdeQXVyMzQ0MzA0NTM@._V1_UX128_CR0,3,128,176_AL_.jpg"
    
    #
    # resize_factor : Image size multiplier (e.g., resize_factor = 2 doubles the image size, positive integer only)
    # url : full URL of the image
    # return : string of the new URL
    #
    
    def getURL(resize_factor, url):
    
        # Regex for pattern matching relevant parts of the URL
        p = re.compile(".*UX([0-9]*)_CR0,([0-9]*),([0-9]*),([0-9]*).*") 
        match = p.search(url)
        
        if match:
            # Get the image dimensions from the URL
            img_width = str(int(match.group(1)) * resize_factor)
            container_width = str(int(match.group(3)) * resize_factor)
            container_height = str(int (match.group(4)) * resize_factor)
        
            # Change the image dimensions
            result = re.sub(r"(.*UX)([0-9]*)(.*)", r"\g<1>"+ img_width +"\g<3>", url)
            result = re.sub(r"(.*UX[0-9]*_CR0,[0-9]*,)([0-9]*)(.*)", r"\g<1>"+ img_width +"\g<3>", result)
            result = re.sub(r"(.*UX[0-9]*_CR0,[0-9]*,[0-9]*,)([0-9]*)(.*)", r"\g<1>"+ container_height +"\g<3>", result)
        
            return result
    #
    # Test
    #
    
    print (getURL(resize_factor,url))
    
    
    
     
    

    编辑:错字

    【讨论】:

    • 感谢您的回答!这可能与代码有关吗?
    • @Nemo 我更新了我的帖子。希望对你有所帮助。
    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 2023-02-22
    • 2022-01-24
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多