Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
mystes
May 31, 2006

Did you write to the file and not close it or something?

Adbot
ADBOT LOVES YOU

mystes
May 31, 2006

Yeah I didn't actually read your code. Maybe print the file size or otherwise make sure it's writing a valid image though.

mystes
May 31, 2006

Also are you really supposed to use response.raw? I think it might break in some situations.

mystes
May 31, 2006

code:
if avatar.status_code == 200:
    with open(avatarFile, 'wb') as avFile:
         avFile.write(avatar.content)

mystes
May 31, 2006

You don't even need to be logged in to access avatars, it seems, so you can just do something like this to test it:
code:
import requests
import shutil
avatarURL = "https://fi.somethingawful.com/safs/titles/05/1a/00227624.0029.png"
avatarFile = "asdf.png"
avatar = requests.get(avatarURL)
print(str(avatar.status_code))
if avatar.status_code == 200:
    with open(avatarFile, 'wb') as avFile:
        avatar.raw.decode_content = True
        shutil.copyfileobj(avatar.raw, avFile)
And you can confirm that indeed the original code produces a 0 byte file.

mystes
May 31, 2006

hbag posted:

alright well im out of ideas for how to download an image from the link
cos i can get the link, ive tested that, but i dont know how to download the image
considering it only writes a file if its 200 then i dont think thats the case
Did the thing I suggested a minute ago not work? Because it works for me with a hardcoded url. I'm guessing it's a problem with compression or something even though I guess response.raw.decode_content = True is allegedly supposed to fix that.

mystes
May 31, 2006

hbag posted:

NOW what need to do is figure out how to detect if an entire image is just one big transparent space
my reasoning for this is so i can detect if someone's doing a tall gangtag av (like mine) and then, if they are, just use the first image gangtag in place of their avatar
You can just iterate through the pixels and check the alpha value, but unless people are using giant transparent avatars maybe you should just check whether the avatar is like 5x5 or smaller?

mystes
May 31, 2006

hbag posted:

i thought putting loops inside loops was a bad idea
this part of the code is already in a while loop, sticking a for loop in it is gonna be 2 loops
Probably whoever said nesting loops was bad was talking about stuff like looping over a huge list in the inner loop of a nested loop which will be executed a zillion times, when you could just be using a hashtable or something, and not looping over the x and y coordinates of a tiny image.

Fart simpson is just trying to make everything complicated.

You can probably just do something like:
code:
def has_opaque_pixel(im):
    if im.mode != "RGBA":
        return True
    for x in range(im.width):
        for y in range(im.height):
            alpha = im.getpixel((x,y))[3]
            if alpha != 0:
                return True
    return False
Although again I'm not sure this is really the best way to decide if an avatar is just an empty one pixel image in general.

Also this might not handle gifs if people still use gifs, because they're indexed color and I don't care enough to check whether they are treated as a different color mode by pillow.

mystes
May 31, 2006

fart simpson posted:

i already posted a link to the pillow docs for the built in method that tells you the min and max pixel values for an entire band of the whole image :shrug: seems pretty simple and useful here to me
Ah yeah I guess you did somewhere in there.

Adbot
ADBOT LOVES YOU

mystes
May 31, 2006

Who needs SVG when you can use webass to run a windows emulator and render gdi+ to a canvas?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply