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.
 
  • Locked thread
Sylink
Apr 17, 2004

If I wanted to dabble in making computer generated images pixel by pixel and applying changes via a program, is there any modules like that in Python?

Like say I wanted to make a png where the top left pixel was red, the next one over in the same row was green, etc.

I realize I dont know what I really want here, but I want something fairly low level.

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
There's PIL, but it's old and outdated and silly, and has a bad API in my opinion.

You can also try things like cairo, of course, which can write image surfaces to PNGs. Last I checked, pycairo could do pixel-level access.

No Safe Word
Feb 26, 2005

Sylink posted:

If I wanted to dabble in making computer generated images pixel by pixel and applying changes via a program, is there any modules like that in Python?

Like say I wanted to make a png where the top left pixel was red, the next one over in the same row was green, etc.

I realize I dont know what I really want here, but I want something fairly low level.

the PIL is the standard here, and you can use something like Image.putpixel to do this


e: or cairo would be reasonable as well, since the PIL does have its downsides

No Safe Word fucked around with this message at 17:25 on Jun 1, 2012

Cat Plus Plus
Apr 8, 2011

:frogc00l:

Kungfoomasta posted:

where src is the file including path. I thought that including the "rb" in open(src,'rb') meant to read it in as binary, but maybe I'm doing it wrong - does anyone know why this doesn't work?

Don't use print, it adds additional newlines that might screw up the data. Also HTTP requires you to use \r\n as terminator, not \n. Do

code:
headers = ["Content-Type: image/png", ... etc. ...]
sys.stdout.write("\r\n".join(headers))
sys.stdout.write("\r\n\r\n")
sys.stdout.write(buffer)
or something like that (assuming this is plain CGI). Also, for webapps in Python, look up WSGI (PEP333).

Cat Plus Plus fucked around with this message at 17:27 on Jun 1, 2012

Kungfoomasta
Apr 26, 2007

No user servicable parts inside.

PiotrLegnica posted:

Don't use print, it adds additional newlines that might screw up the data. Also HTTP requires you to use \r\n as terminator, not \n. Do

code:
headers = ["Content-Type: image/png", ... etc. ...]
sys.stdout.write("\r\n".join(headers))
sys.stdout.write("\r\n\r\n")
sys.stdout.write(buffer)
or something like that (assuming this is plain CGI). Also, for webapps in Python, look up WSGI (PEP333).

Brilliant! I wound up with this:

code:
def download_file():
	getfile = open(src,'rb')
	buffer = getfile.read()
	length = str(len(buffer))
	filename = os.path.split(src)[-1]
	

	headers = ["Content-Type:application/x-download","Content-Disposition:attachment;filename="+filename, "Content-Length:%s" % length]
	sys.stdout.write("\r\n".join(headers))
	sys.stdout.write("\r\n\r\n")
	sys.stdout.write(buffer)


download_file()
and it works perfectly. Thanks!

Emacs Headroom
Aug 2, 2003
When I use PIL if I have to any manipulation, I find it's a lot easier to convert it to a numpy array first and then mess around, and then cast it back.

Python code:
from PIL import Image
import numpy as np

im = Image.open('test_image.png')
im_a = np.array(im)    #has rgba or rgb channels as [x][y][channel]

new_a = np.array([800, 600, 3], dtype = 'uint8')
#do stuff, mess around with pixels
new_im = Image.fromarray(new_a)
This makes pixel-level manipulations far, far more tolerable.

Hed
Mar 31, 2004

Fun Shoe
Python 3.3 alpha release notes out and give clues to what is going in to the next release.
What I'm most excited about :
  • PEP 3144 IP address class is going in.
  • New string handling that uses compact structure until you go over 0x00FF and 0xFFFF boundaries and then expands to 4 bytes per symbol.
  • bz2 module has been rewritten from scratch and looks like it will allow wrapping a buffered IO reader so that handling text streams will be more similar for compressed and uncompressed flows.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Hed posted:

PEP 3144 IP address class is going in.

The reference implementation that appears to be used for that PEP doesn't support valid IPv4 addresses such as "1.2" and "1.2.3456".

Because, as it turns out, everybody who reimplements IPv4 address parsing gets it wrong and I don't know why people keep trying when the standard C library provides perfectly good functions for doing this.

fritz
Jul 26, 2003

Hed posted:

Python 3.3 alpha release notes out and give clues to what is going in to the next release.

Oh man they dropped vms support :(

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Still doesn't have bytes.format :smith:

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Hey, I'm hoping this will be a short question but I'm not really sure. I have this framework for Actions in my django app that is a Django Model called Action, subclassed by proxy Model that define different actions and how to do and undo them.

The base class Action acts as a dispatcher, called like with Action.do_action('import_path', source_of_action, [arguments]). It processes the import path with the __import__ function.

It's been working peachy thus far but my actions.py files have been getting heavy so I want to rework them into a directory structure ie
code:
ss
  pms
    items
      actions
        __init__.py
        base.py (contains classes Create, Delete, Rename, ChangeCode etc)
        services.py (contains classes Add, Delete, Edit)
There's a problem though, base and services are conflicting with one another, if I just go into the Django Shell, doing the imports manually this is what'll happen.
Python code:
>>> import ss.pms.items.actions.base
>>> ss.pms.items.actions.base.Delete
<class 'ss.pms.items.actions.base.Delete'>
>>> import ss.pms.items.actions.services
>>> ss.pms.items.actions.services.Delete
<class 'ss.pms.items.actions.base.Delete'>
Basically whichever module gets at the name Delete first keeps it, which is causing conflicts where the wrong action gets called. I'd rather just make sure names are unique rather than rewrite the action framework entirely if I can't fix this, but It'd be nice not to worry about naming conflicts cross modules.

Neither action module imports the other, only subclassing Action. It feels like I should be able to make this work but maybe I've just coded myself into a corner here.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Don't try to be clever and use __import__. Is there something wrong with explicitly importing an action?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
I need the path saved into the database, so that whatever Action I use, when I later do something like:

Python code:
action = Action.objects.latest()
action.undo()
Where the base Action instance gets the right action class and does it's undo method.
Python code:
def undo(self, *args, **kwargs):
    action_class = self._get_action_class(self.action_class_path)
    action_instance = action_class.objects.get(id=self.id)
    if action_instance.can_undo():
        return action_instance.undo(*args, **kwargs)
    else:
        raise Exception('Undo Timeout reached.')
undo.alters_data = True
Weird I know, but it subclasses behaviour without requiring a db table per Action subclass, which would be ridiculous. Even if I wanted to do it without using proxy models, I'd still need to be able to get the definition of the action later from a string in order to keep the Action object polymorphic. As I said, it has been working fine up until now, and this naming conflict is occurring through manual usage in a session, never mind with __import__.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Good job coupling your data to your code, there. :thumbsup: Surely any future refactors aren't going to break your database entirely.


As for the import problem, I don't know why this is happening. I'd have to see the actual code.

Scaevolus
Apr 16, 2007

ShoulderDaemon posted:

The reference implementation that appears to be used for that PEP doesn't support valid IPv4 addresses such as "1.2" and "1.2.3456".

Because, as it turns out, everybody who reimplements IPv4 address parsing gets it wrong and I don't know why people keep trying when the standard C library provides perfectly good functions for doing this.
How are those addresses valid?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Suspicious Dish posted:

Good job coupling your data to your code, there. :thumbsup: Surely any future refactors aren't going to break your database entirely.

As for the import problem, I don't know why this is happening. I'd have to see the actual code.
I don't actually think so because the data is only used for the life of the action, only 20-30 minutes. After that it can be tossed cause logging is handled by something else. Fair point but I can't think of another way to go about it, that's more about me than anything though.

edit: Anyhow, I'm just gonna reorganize my package structure a different way, no point butting my head against a brick wall aver this one.

Maluco Marinero fucked around with this message at 09:06 on Jun 3, 2012

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Scaevolus posted:

How are those addresses valid?

IPv4 addresses are made of 1 to 4 numbers, separated by periods. All numbers but the last must be from 0 to 255, and represent the higher-order bytes of the address. The last number represents all remaining bytes of the address.

This is handy when you are in a network with zeroes in the middle of its addresses, because you can compress them away.

192.168.0.1 == 192.168.1 == 192.11010049 == 3232235521
10.0.0.1 == 10.0.1 == 10.1 == 167772161
8.8.8.8 == 8.8.2056 == 8.526344 == 134744072

If you get in the habit of typing your gateway as "10.1" and then suddenly have to deal with a program that wrongly insists that isn't a valid IPv4 address, it's sort of annoying.

Stabby McDamage
Dec 11, 2005

Doctor Rope

ShoulderDaemon posted:

IPv4 addresses are made of 1 to 4 numbers, separated by periods. All numbers but the last must be from 0 to 255, and represent the higher-order bytes of the address. The last number represents all remaining bytes of the address.

This is handy when you are in a network with zeroes in the middle of its addresses, because you can compress them away.

192.168.0.1 == 192.168.1 == 192.11010049 == 3232235521
10.0.0.1 == 10.0.1 == 10.1 == 167772161
8.8.8.8 == 8.8.2056 == 8.526344 == 134744072

If you get in the habit of typing your gateway as "10.1" and then suddenly have to deal with a program that wrongly insists that isn't a valid IPv4 address, it's sort of annoying.

I knew you could go full decimal or dotted bytes, but I didn't know there was anything in between. Neat!

OnceIWasAnOstrich
Jul 22, 2006

ShoulderDaemon posted:

If you get in the habit of typing your gateway as "10.1" and then suddenly have to deal with a program that wrongly insists that isn't a valid IPv4 address, it's sort of annoying.

Like Chrome? I had no idea this was a thing and when I tried it in Chrome it just searched for it instead of sending me to 127.1

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

OnceIWasAnOstrich posted:

Like Chrome? I had no idea this was a thing and when I tried it in Chrome it just searched for it instead of sending me to 127.1

I don't use Chrome, so I wouldn't really know. Firefox supports it, uzbl (the only webkit browser I have installed) supports it, anything that uses the libc resolver supports it.

Edit: The definitive test is probably the ping program. On both Windows and Linux you can ping 127.1, so any web browser that doesn't let you attempt to connect to 127.1 has a bug.

ShoulderDaemon fucked around with this message at 02:22 on Jun 4, 2012

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

OnceIWasAnOstrich posted:

Like Chrome? I had no idea this was a thing and when I tried it in Chrome it just searched for it instead of sending me to 127.1
Chrome will navigate to it if you type out the full address http://127.1

Opinion Haver
Apr 9, 2007

http://1249763762 works too.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Can anyone recommend a Django book?

The official docs are good, but I always like to read someone elses detailed take to help fill in the gaps and sometimes when something is explained in a different way it makes more sense...

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Thermopyle posted:

Can anyone recommend a Django book?

The official docs are good, but I always like to read someone elses detailed take to help fill in the gaps and sometimes when something is explained in a different way it makes more sense...
Well, there's the Django Book at http://www.djangobook.com/ . It's somewhat outdated, but the preview for the next version is at http://www.djangobook.com/en/2.0/ and should be closer to the current Django release.

Hed
Mar 31, 2004

Fun Shoe
I really enjoyed Pro Django as it goes into detail about the inner workings far better than the docs and it gives that alternate perspective that you noted. For someone like you who understands Python first, I think it would be an excellent book. Its downside is much like the other books in the Django world--it is a few years old and only covers 1.1-1.2, missing things like Class-based Generic Views and Mixins. The official docs cover the former and for the latter turn to our very own MonkeyMaker for some great examples.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Oh hey, there's a Django thread. Thanks for your answers, you two.

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
PyAudio is going to be the death of me. I somehow got it to compile on Lion after much effort, but then I try to install PyAudio and it barfs this at me.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
You need to install PortAudio headers, it looks like.

ryo
Jan 15, 2003
I'm trying to install python-MySQL inside a virutalenv on OS X 10.7
I upgraded XCode and downloaded the command line tools as suggested in various google results.
Now when I try "pip install MySQL-python" I get a ton of _mysql.c errors, finishing with:

quote:

llvm-gcc-4.2: error trying to exec '/usr/bin/../llvm-gcc-4.2/bin/powerpc-apple-darwin11-llvm-gcc-4.2': execvp: No such file or directory

lipo: can't open input file: /var/folders/53/3w9qbng92j1674v_258wb2240000gn/T//ccnAnL6I.out (No such file or directory)

error: command 'llvm-gcc-4.2' failed with exit status 255

There are hardly any google results for that last line, so I've no idea what the problem is. Any ideas what the problem could be?

edit: Could the bit referring to "powerpc-apple-darwin11-llvm-gcc-4.2" be the problem? It's an Intel Mac so I don't know why it has anything referring to PowerPC.

ryo fucked around with this message at 11:00 on Jun 4, 2012

etcetera08
Sep 11, 2008

ryo posted:

I'm trying to install python-MySQL inside a virutalenv on OS X 10.7
I upgraded XCode and downloaded the command line tools as suggested in various google results.
Now when I try "pip install MySQL-python" I get a ton of _mysql.c errors, finishing with:


There are hardly any google results for that last line, so I've no idea what the problem is. Any ideas what the problem could be?

edit: Could the bit referring to "powerpc-apple-darwin11-llvm-gcc-4.2" be the problem? It's an Intel Mac so I don't know why it has anything referring to PowerPC.

http://stackoverflow.com/questions/6839795/cant-figure-out-the-architecture-type-of-problem-when-compiling-python

Kosani
Jun 30, 2004
Ni ni.
Hey guys. I am learning the fundamentals of python currently and, to get started, I am attempting to make a simple 'packman' game in vizard (a python based game engine/IDE)

While attempting to test out class inheritance, I have come across an error that I cannot seem to fix. I have gone over every inch of my code and I cannot understand where the error is coming from:

code:
import viz
import math
viz.go()

class Object:
	def __init__(self):
		self.age = 0
		self.powerConsumption = 25
		self.value = 100
		self.position = viz.Vector(0,0,0)
		self.velocity = viz.Vector(0,0,0)
		self.acceleration = viz.Vector(0,0,0)
		self.maxSpeed = 1000
		self.currentTime = 0.0
		
	def move(self,time):
		self.currentTime +=time
		self.acceleration += viz.Vector(math.cos(self.currentTime),math.sin(self.currentTime),0.0)
		self.acceleration.normalize() #?
		self.acceleration *= self.maxSpeed
		self.update(time)
		
	def stats(self):
		print 'Age:',self.age
		print 'Acceleration:',self.acceleration
		print 'Velocity:',self.velocity
		print 'Position:',self.position
		
	def update(self,time):
		self.velocity += self.acceleration*time
		self.position += self.velocity*time
		
#inheritance example
class PacMang(Object):
	def __init__self(self):
		Object.__init__(self,'PacMan',35,10,1000)
		self.model = viz.add('pacMan.IVE')
	def move(self,time):
		Object.move(self,time)
		self.model.setPosition(self.position.x,self.position.y,self.position.z)
		
class Enemy(Object):
	def __init__self(self):
		Object.__init__(self,'Enemy',35,10,1000)
		self.model = viz.add('enemy.IVE')
	def move(self,time):
		Object.move(self,time)
		self.model.setPosition(self.position.x,self.position.y,self.position.z)
		
pm = Enemy()

def timerFunc(e):
	if e ==1:
		pm.move(0.01)
	elif  e ==2:
		pm.stats()
	else:
		print 'something went wrong'
						
#every time there is a new frame = UPDATE_EVENT
#specified timer: TIMER_EVENT

viz.callback(viz.TIMER_EVENT,timerFunc)
viz.starttimer(1,1.0,viz.FOREVER)
and the error:
code:
self.model.setPosition(self.position.x,self.position.y,self.position.z)
AttributeError: Enemy instance has no attribute 'model'
Traceback (most recent call last):
  File "...\Vizard5.py", line 55, in timerFunc
    pm.move(0.01)
  File "...\Vizard5.py", line 49, in move
    self.model.setPosition(self.position.x,self.position.y,self.position.z)
I do not understand why it says that my instance of enemy and/or pacman does not have the 'model' attribute when I've defined it in the inherited class.

I'm completely new to python so I know it's going to be something stupid, but I've been stuck for over an hour. Thanks in advance to anyone who can help.

Kosani fucked around with this message at 21:00 on Jun 4, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What is an __init__self?

Kosani
Jun 30, 2004
Ni ni.

Suspicious Dish posted:

What is an __init__self?

Ah. Wow. That was dumb of me. Thank you.

FrozenShellfish
Aug 30, 2004

Having some trouble with the very basics: reading in a file :(

Any ideas why this isn't working? It's a 407kb bmp, but it only seems to be reading the first ~160 bytes.
code:
f_in = open('./aaaa/test001.bmp', 'r')
read_data=f_in.read()
f_in.close()

sendstr='\x02\x04\x01\x01\xff\xff\x00'
sendstr+=read_data
sendstr+='\x66'

edit: figured it out. There was an EOF character in there; needed to use 'rb' when opening.

Also second anticipated problem: Suppose I am eventually able to load in the file and get a nice 407kb string. What is the best way to sum the elements mod 100?

Thanks!

FrozenShellfish fucked around with this message at 23:37 on Jun 5, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What are the elements? Bytes?

sum(ord(c) for c in contents) % 100

Emacs Headroom
Aug 2, 2003

FrozenShellfish posted:

Also second anticipated problem: Suppose I am eventually able to load in the file and get a nice 407kb string. What is the best way to sum the elements mod 100?

Thanks!

If you've got numpy, it's easy to go

Python code:
import struct 
import numpy as np

filesum = 0

for #some byte-wise loop:
    s = file.read(size of buffer)
    a = np.array(struct.unpack('i'* number of bytes, s))
    filesum += (a % 100).sum()
If you didn't have numpy, you could do something like
Python code:
import struct 

filesum = 0

for #some byte-wise loop:
    s = file.read(size of buffer)
    a = struct.unpack('i'* number of bytes, s)
    filesum += sum([value % 100 for value in a])

tripwire
Nov 19, 2004

        ghost flow

Ridgely_Fan posted:

When I use PIL if I have to any manipulation, I find it's a lot easier to convert it to a numpy array first and then mess around, and then cast it back.

Python code:
from PIL import Image
import numpy as np

im = Image.open('test_image.png')
im_a = np.array(im)    #has rgba or rgb channels as [x][y][channel]

new_a = np.array([800, 600, 3], dtype = 'uint8')
#do stuff, mess around with pixels
new_im = Image.fromarray(new_a)
This makes pixel-level manipulations far, far more tolerable.

Also, you can just use numpy to cast your image array a float datatype, which lets you do awesome and surprisingly simple stuff like auto-equalizing and tone mapping.

Zombywuf
Mar 29, 2008

Python code:
#!/usr/bin/python
#coding=UTF-8

print u"£"
Bash code:
ubuntu~/code$ ./test.py
£
ubuntu~/code$ ./test.py > test.py.out
Traceback (most recent call last):
  File "./test.py", line 4, in <module>
    print u"£"
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)
ubuntu~/code$ locale
LANG=en_GB.UTF-8
LANGUAGE=en_GB:en
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=
Why are you doing this to me Python?

Any idea how I make it stop doing this to me? I'd rather not just encode the string as UTF-8 before outputting it, I just want it to use the appropriate locale settings even when it's not writing to a pty.

EDIT: urgh, found the answer, which is to do this:
Python code:
#!/usr/bin/python
#coding=UTF-8
import locale

print u"£".encode(locale.getpreferredencoding(), "replace")
Damnit Guido!

Zombywuf fucked around with this message at 14:45 on Jun 6, 2012

tef
May 30, 2004

-> some l-system crap ->

Zombywuf posted:

Why are you doing this to me Python?

sys.stdout is opened without an encoding when you pipe.


quote:

Damnit Guido!


:argh:

Adbot
ADBOT LOVES YOU

raminasi
Jan 25, 2005

a last drink with no ice
Here's a total newbie question :ohdear:

How do I create a list of some given size populated with all the same element? Basically
Python code:
>>> [10 for x in range(5)]
[10, 10, 10, 10, 10]
feels clunky. I feel like I might have seen this in a tutorial somewhere but I can't recall and I'm not sure what to google.

  • Locked thread