0

I tried this:

import pygame
import urllib.request

page = urllib.request.urlopen("http://s.gjcdn.net/img/logo-small-3.png")
image = page.read()

pygame_image = pygame.image.frombuffer(image, (196, 25), "RGB") #(196, 25) are checked on image original

But when I run it I get ValueError: Buffer length does not equal format and resolution size

Is there any way to do this (without saving file to the disk), or maybe I did something wrong in my code?

I would also like to know how to know size of image (because I mostly can't know size of image)?

martineau
  • 119,623
  • 25
  • 170
  • 301
knowledge
  • 383
  • 3
  • 15
  • technically, you're downloading the image EVERY time you run this code. just because you're not saving it to disk doesn't magically make it a "not-a-download". – Marc B Feb 06 '15 at 21:22
  • @Marc B I meant not saving to the disk. – knowledge Feb 06 '15 at 21:23
  • Try using `image = cStringIO.StringIO(page.read())`. – martineau Feb 06 '15 at 21:46
  • @martineau I use Python 3 and I don't have `cStringIO` in my library, is it 3rd part library (if it is where can I download it?) and if it's only in Python 2 what can I use for instead of `cStringIO`? – knowledge Feb 07 '15 at 10:37
  • Oh, sorry, your question wasn't tagged Python 3. No `cStringIO` isn't a third-party library, see this [answer](http://stackoverflow.com/questions/11914472/stringio-in-python3/18284900#18284900). – martineau Feb 07 '15 at 14:10
  • Then I got this error `TypeError: initial_value must be str or None, not bytes` (I used io.StringIO) – knowledge Feb 07 '15 at 14:27

2 Answers2

0

Try putting it in RGBA format? The image does contain alpha values after all. If that doesn't help, then there is this question. It's dealing with sockets, but it may be relevant. Pygame not working with sockets

Community
  • 1
  • 1
  • I tried RGBA format but it's same error, also that question/answer isn't very helpful. – knowledge Feb 07 '15 at 10:31
  • I just did a quick search, and it seems that you can't use frombuffer with a compressed png image. The function expects raw pixel data. [link](http://archives.seul.org/pygame/users/Dec-2005/msg00158.html) – Delya Erricson Feb 08 '15 at 21:02
0

image.frombuffer is for creating an image from data already loaded into memory as an image buffer. What you want is image.load, which takes an image file and turns it INTO an image buffer. You should just be able to pass it your page since that's a file-like object already.

tzaman
  • 46,925
  • 11
  • 90
  • 115