Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 27th, 2005, 6:52 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Talking I'm ignorant!

For Comp Science class, we had to draw a picture using drawfillbox, drawstar, drawfilloval, etc... in Turing (Pic.FileNew is banned).

Well that's just complete n00b right?

So I made this program in Python:

a = raw_input("Enter Image Name: ")
b = raw_input("Enter Output Name: ")
c = input("Reduce Size By What Degree: ")
d = raw_input("Transparency? ")

import pygame
pygame.init()
print "Initialized Program"

palette = [(255, 255, 255), (0, 0, 128), (0, 128, 0), (0, 128, 128), (128, 0, 0),
           (128, 0, 128), (128, 128, 0), (0, 0, 0), (192, 192, 192), (0, 0, 255),
           (0, 255, 0), (0, 255, 255), (255, 0, 0), (255, 0, 255), (255, 255, 0),
           (128, 128, 128), (0, 0, 0), (20, 20, 20), (32, 32, 32), (44, 44, 44),
           (56, 56, 56), (69, 69, 69), (81, 81, 81), (97, 97, 97), (113, 113, 113),
           (130, 130, 130), (146, 146, 146), (162, 162, 162), (182, 182, 182), (203, 203, 203),
           (227, 227, 227), (255, 255, 255), (0, 0, 255), (65, 0, 255), (125, 0, 255),
           (190, 0, 255), (255, 0, 255), (255, 0, 190), (255, 0, 125), (255, 0, 65),
           (255, 0, 0), (255, 65, 0), (255, 125, 0), (255, 190, 0), (255, 255, 0),
           (190, 255, 0), (125, 255, 0), (65, 255, 0), (0, 255, 0), (0, 255, 65),
           (0, 255, 125), (0, 255, 190), (0, 255, 255), (0, 190, 255), (0, 125, 255),
           (0, 65, 255), (125, 125, 255), (158, 125, 255), (190, 125, 255), (223, 125, 255),
           (255, 125, 255), (255, 125, 223), (255, 125, 190), (255, 125, 158), (255, 125, 125),
           (255, 158, 125), (255, 190, 125), (255, 223, 125), (255, 255, 125), (223, 255, 125),
           (190, 255, 125), (158, 255, 125), (125, 255, 125), (125, 255, 158), (125, 255, 190),
           (125, 255, 223), (125, 255, 255), (125, 223, 255), (125, 190, 255), (125, 158, 255),
           (182, 182, 255), (199, 182, 255), (219, 182, 255), (235, 182, 255), (255, 182, 255),
           (255, 182, 235), (255, 182, 219), (255, 182, 199), (255, 182, 182), (255, 199, 182),
           (255, 219, 182), (255, 235, 182), (255, 255, 182), (235, 255, 182), (219, 255, 182),
           (199, 255, 182), (182, 255, 182), (182, 255, 199), (182, 255, 219), (182, 255, 235),
           (182, 255, 255), (182, 235, 255), (182, 219, 255), (182, 199, 255), (0, 0, 113),
           (28, 0, 113), (56, 0, 113), (85, 0, 113), (113, 0, 113), (113, 0, 85),
           (113, 0, 56), (113, 0, 28), (113, 0, 0), (113, 28, 0), (113, 56, 0),
           (113, 85, 0), (113, 113, 0), (85, 113, 0), (56, 113, 0), (28, 113, 0),
           (0, 113, 0), (0, 113, 28), (0, 113, 56), (0, 113, 85), (0, 113, 113),
           (0, 85, 113), (0, 56, 113), (0, 28, 113), (56, 56, 113), (69, 56, 113),
           (85, 56, 113), (97, 56, 113), (113, 56, 113), (113, 56, 97), (113, 56, 85),
           (113, 56, 69), (113, 56, 56), (113, 69, 56), (113, 85, 56), (113, 97, 56),
           (113, 113, 56), (97, 113, 56), (85, 113, 56), (69, 113, 56), (56, 113, 56),
           (56, 113, 69), (56, 113, 85), (56, 113, 97), (56, 113, 113), (56, 97, 113),
           (56, 85, 113), (56, 69, 113), (81, 81, 113), (89, 81, 113), (97, 81, 113),
           (105, 81, 113), (113, 81, 113), (113, 81, 105), (113, 81, 97), (113, 81, 89),
           (113, 81, 81), (113, 89, 81), (113, 97, 81), (113, 105, 81), (113, 113, 81),
           (105, 113, 81), (97, 113, 81), (89, 113, 81), (81, 113, 81), (81, 113, 89),
           (81, 113, 97), (81, 113, 105), (81, 113, 113), (81, 105, 113), (81, 97, 113),
           (81, 89, 113), (0, 0, 65), (16, 0, 65), (32, 0, 65), (48, 0, 65),
           (65, 0, 65), (65, 0, 48), (65, 0, 32), (65, 0, 16), (65, 0, 0),
           (65, 16, 0), (65, 32, 0), (65, 48, 0), (65, 65, 0), (48, 65, 0),
           (32, 65, 0), (16, 65, 0), (0, 65, 0), (0, 65, 16), (0, 65, 32),
           (0, 65, 48), (0, 65, 65), (0, 48, 65), (0, 32, 65), (0, 16, 65),
           (32, 32, 65), (40, 32, 65), (48, 32, 65), (56, 32, 65), (65, 32, 65),
           (65, 32, 56), (65, 32, 48), (65, 32, 40), (65, 32, 32), (65, 40, 32),
           (65, 48, 32), (65, 56, 32), (65, 65, 32), (56, 65, 32), (48, 65, 32),
           (40, 65, 32), (32, 65, 32), (32, 65, 40), (32, 65, 48), (32, 65, 56),
           (32, 65, 65), (32, 56, 65), (32, 48, 65), (32, 40, 65), (44, 44, 65),
           (48, 44, 65), (52, 44, 65), (60, 44, 65), (65, 44, 65), (65, 44, 60),
           (65, 44, 52), (65, 44, 48), (65, 44, 44), (65, 48, 44), (65, 52, 44),
           (65, 60, 44), (65, 65, 44), (60, 65, 44), (52, 65, 44), (48, 65, 44),
           (44, 65, 44), (44, 65, 48), (44, 65, 52), (44, 65, 60), (44, 65, 65),
           (44, 60, 65), (44, 52, 65), (44, 48, 65), (0, 0, 0), (0, 0, 0),
           (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
print "Got Palette"

image = pygame.image.load(a)
image = pygame.transform.scale(image, ((image.get_width()/c,image.get_height()/c)))
print "Prepared Image"

def get_8bit(rgb):

    R = []
    for x in range(255):
        n = palette[x]

        Ra = abs(n[0]-rgb[0])
        Rb = abs(n[1]-rgb[1])
        Rc = abs(n[2]-rgb[2])
        R.append( [Ra+Rb+Rc, x, max([Ra,Rb,Rc]), n] )

    R = min(R)
    return R[1], str(R[2]), R[3]     

m = ['setscreen("graphics:800;600")',
     'var x, y : int',
     'x := 100',
     'y := 100',
     'procedure draw_%s (x, y : int)'%(a[:-4]),
     '    %s ... old color(RGB) | new color(RGB) | difference(RGB)'%('%')]
exec "d = %s"%(d)
print "Prepared Code and Cases"

total = 0
trans = 0
i = 0
ii = image.get_width()*image.get_height()

for x in range(image.get_width()):
    for y in range(image.get_height()):
        
        i += 1
        if not y%100:
            print "%",int(round(100.0/float(ii)*float(i)))
        
        n = image.get_at((x,y))[:3]
        if n != d:
            
            eBit = get_8bit(n)
            m.append("    drawdot (%s + x, %s + y, %s) %s %s | %s | %s"%(x, image.get_height()-y, eBit[0], "%", n, eBit[2], eBit[1]))
            total += int(eBit[1])

        else:trans += 1

m = m[:4] + ["",
             "/*",
             "Created from:       %s"%(a),
             "Reduced by:         x%s"%(c),
             "Transparancy (RGB): %s"%(str(d)),
             "Transparent Dots:   %s"%(trans),
             "Drawn Dots:         %s"%(ii-trans),
             "Total Color Loss:   %s"%(total),
             "Average Color Loss: %s"%(float(total)/float(ii-trans)),
             "",
             "Generated by Aaron Voelker's Python Program (Sept 27, 2005)",
             "*/",
             ""] + m[4:] + [
                 'end draw_%s'%(a[:-4]),
                 '',
                 'draw_%s (x, y)'%(a[:-4])]
print "% 100\nFinished Code"

try:
    file = open(b,'w')
    file.write('\n'.join(m))
    file.close()
    print "Save Complete"
except:
    print "Save Failed"

Which creates a program in Turing that will draw whatever picture you ran through that program.

http://1v7.com/drsane/gingerale.txt << me with a gingerale-box over my head

But if you're going to use my program to do manual graphics in Turing, please give credit to my program (as if anyone uses Turing).

Haha.

Last edited by Sane; Sep 27th, 2005 at 7:20 PM.
Sane is offline   Reply With Quote
Old Sep 27th, 2005, 7:38 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Wow, that's awesome..
__________________

tempest is offline   Reply With Quote
Old Sep 27th, 2005, 7:58 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Here's my friend doing a cartwheel to FS Handspring (as an animation).

http://1v7.com/drsane/chris_trick.zip <<< Source
http://1v7.com/drsane/chris_exe.zip <<< Stand-Alone Executable

I love it!
Sane is offline   Reply With Quote
Old Sep 27th, 2005, 9:22 PM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
wow, this is l33t man, nice work.
OpenLoop is offline   Reply With Quote
Old Sep 27th, 2005, 9:25 PM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
I bow to you

(except for the fact that I had to boot into windows in order to run the turing program...)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Sep 27th, 2005, 9:44 PM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
cool stuff
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 27th, 2005, 9:55 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Okay, I took a high quality photo from last year's summer camping trip. One of the following is that photo compressed in 256 colors using Paint. The other is compressed in 256 colors using my program and was print screened out of the Turing display window.

Try to guess which is which:








It's really actually quite funny though. Everyone's graphic project is like a box for grass and a circle for a sun, but mine is like realistic moving graphics. o_o

Tomorrow I'm going to rip screen shots from a movie preview and compile them into an animation for what I'll hand in.
Sane is offline   Reply With Quote
Old Sep 27th, 2005, 10:06 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Nice work, Sane, very nice. There are a number of different methods for reducing a large number of colors to a smaller number (like 256). Have you gotten into that to any extent? I'm going to vote for #1 as being the most successful, all around, although it was poorer on the sky. I think it has a greater range near the extremes, also, more detail in the darks and lights. That's a photographer's perspective, no sound technical basis for the comment.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2005, 10:15 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
No, I've only done the method that has been most obvious to me. Just selecting the closest match based on the pre-determined 256 color palette.

But the goal of this wasn't even really to make it look semi-good, that's just a bonus. It was to be able to draw pictures without importing them from a seperate file in a very old-school programming language.
Sane is offline   Reply With Quote
Old Sep 27th, 2005, 10:21 PM   #10
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
How long have you been doing Python Sane?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion 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 6:56 PM.

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