Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 12th, 2005, 8:39 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Grayscale Converter with Pygame

Yeah, I was bored, so I made a pic --> grayscale program in Pygame.

fileName  = "python.gif"
red       = False
green     = True
blue      = False
########################

from pygame.image import load, save

pic = load(fileName)

for x in range(pic.get_width()):
    for y in range(pic.get_height()):

        c = list(pic.get_at((x,y))[:3])
        deg = (c[0]*red + c[1]*green + c[2]*blue) / (red + green + blue)
        
        pic.fill( [deg]*3, ((x,y),(1,1)) )

save(pic, "%s_%s%s%s.bmp"%(fileName.split('.')[0],
                           'R'*red,
                           'G'*green,
                           'B'*blue))

The user can select if it measures the degree based on R, G or B. Or any combination of the latter. A good example of manipulating boolean values I guess.

Here's an example: http://netpub.cstudies.ubc.ca/oleary/images/python.gif

Outputs: http://img204.imageshack.us/img204/8374/pythong4qm.png

I think it actually works quite beautifully.

Bleeeeh. ^_^
Sane is offline   Reply With Quote
Old Dec 12th, 2005, 4:10 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Heh, cool. You might wanna try and write the PIL equivalent - should be easy peasy using the Image.point function.
Cerulean is offline   Reply With Quote
Old Dec 12th, 2005, 7:38 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Python Image Library?

But that would mean I'd have to learn new functions... hmmm... >_>

I'm thinking of maybe doing more image conversions/manipulations. Any ideas?

And by the way, in my program, if you replace the True/False with a number. It'll take that number and use it as a percentage/weight. Pretty cool.
Sane is offline   Reply With Quote
Old Dec 13th, 2005, 2:00 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Maybe:
from PIL import Image, ImageEnhance
image = Image.open(filename)
greyscale_image = ImageEnhance.Color(image).enhance(0.0)
I think that should work, though I haven't yet tried it.
Arevos is offline   Reply With Quote
Old Dec 14th, 2005, 1:10 AM   #5
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Going from a color .jpg to grayscale:
# PIL convert to grayscale mode "L"

import Image

im1 = Image.open("Arevos.jpg")
im2 = im1.convert("L")

im2.show()
im2.save("ArevosGray.jpg")
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Dec 14th, 2005, 11:21 AM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
But that would mean I'd have to learn new functions... hmmm... >_>
If you're gonna do a lot of image stuff it makes sense to use PIL - I assume it'll be faster for images, reads and writes more formats, and has more convenience functions (for adjusting contrast and the like). Laziness is good, and you'll be able to be lazier if you use PIL. Just take the time up front to learn to use the methods it provides.
Cerulean is offline   Reply With Quote
Old Dec 14th, 2005, 11:59 AM   #7
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
<troll>
Now make one that goes from black and white to color
</troll>

I think i'd tweak the red/green/blue values to be integers or even doubles, to provide a different weighting of colors. IIRC a blue generally shows up 'darker' than a simiairly bright green.

Overall though good job, and interresting use of boolean 'division'

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Dec 14th, 2005, 12:03 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
"I think i'd tweak the red/green/blue values to be integers or even doubles, to provide a different weighting of colors. IIRC a blue generally shows up 'darker' than a simiairly bright green."

Quote:
Originally Posted by Sane
And by the way, in my program, if you replace the True/False with a number. It'll take that number and use it as a percentage/weight. Pretty cool.

>_>
Sane is offline   Reply With Quote
Old Dec 14th, 2005, 12:38 PM   #9
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
**Disclaimer : Anything stupid I say in this post and previous and/or future posts are subject to mockery, however any information in said posts are not to be used for any legal or for trade applications. Additionally any failure to fully read or comprehend the posts of others shall in no way detract from or preclude my ability to sound like a 'know-it-all' or expert.
MBirchmeier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC