-2

I am trying to make some image filters for my API. Some URLs work while most do not. I wanted to know why and how to fix it. I have Looked through another stack overflow post but have not had much luck as I don't know the problem.

Here is an example of a working URL

And one that does not work

Edit Here is another URL that does not work

Here is the API I am trying to make

Here is my code

def generate_image_Wanted(imageUrl):

    with urllib.request.urlopen(imageUrl) as url:
        f = io.BytesIO(url.read())

    im1 = Image.open("images/wanted.jpg")
    im2 = Image.open(f)
    im2 = im2.resize((300, 285))

    img = im1.copy()
    img.paste(im2, (85, 230))
    d = BytesIO()
    d.seek(0)
    img.save(d, "PNG")
    d.seek(0)
    return d

Here is my error

Traceback (most recent call last):
  File "c:\Users\micha\OneDrive\Desktop\MicsAPI\test.py", line 23, in <module>
    generate_image_Wanted("https://cdn.discordapp.com/avatars/902240397273743361/9d7ce93e7510f47da2d8ba97ec32fc33.png")
  File "c:\Users\micha\OneDrive\Desktop\MicsAPI\test.py", line 11, in generate_image_Wanted
    with urllib.request.urlopen(imageUrl) as url:
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Thank you for looking at this and have a good day.

Chris
  • 18,724
  • 6
  • 46
  • 80
Michael
  • 114
  • 1
  • 14
  • 1
    How do you know it doesn't work? See [ask], how to create a [mcve], and edit the question. – Peter Wood Apr 01 '22 at 12:06
  • I can't access the second URL. – Peter Wood Apr 01 '22 at 12:06
  • @PeterWood I tried them a few URLs and the image did save with some URLS while with the working one it did with out errors or problems. THe second URL is a discord avatar URL I will grab a different one that did not work – Michael Apr 01 '22 at 12:17
  • 1
    What does "does not work" mean? Does it give you an error? Does fetching the URL fail? Does the `copy` operation fail? Does the resulting image fail to display? – MatsLindh Apr 01 '22 at 12:33
  • @MatsLindh I have Ran it with out the fast API stuff and do get a error. I have updated the question. And sorry for only just running it on its own. – Michael Apr 01 '22 at 12:43

1 Answers1

0

maybe sites you can't scrape has server prevention for known bot and spiders and block your request from urllib.

You need to provide some headers - see more about python request lib

Working example:

import urllib.request
hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
url = "https://cdn.discordapp.com/avatars/902240397273743361/9d7ce93e7510f47da2d8ba97ec32fc33.png"

req = urllib.request.Request(url, headers=hdr)
response = urllib.request.urlopen(req)
response.read()
  • `hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }` `req = urllib.request.Request(url, headers=hdr)` when i use this header with second url request is successful without it request return 403 – Nikolay Tsvetanov Apr 01 '22 at 13:16
  • This did Work Thank you. No idea how a guide I was trying to use did it without. – Michael Apr 01 '22 at 13:32