Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Scripting Languages (http://www.programmingforums.org/forum39.html)
-   -   I'm ignorant! (http://www.programmingforums.org/showthread.php?t=6130)

Sane Sep 27th, 2005 6:52 PM

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.

tempest Sep 27th, 2005 7:38 PM

Wow, that's awesome..

Sane Sep 27th, 2005 7:58 PM

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!

OpenLoop Sep 27th, 2005 9:22 PM

wow, this is l33t man, nice work.

Jessehk Sep 27th, 2005 9:25 PM

I bow to you :D ;)

(except for the fact that I had to boot into windows in order to run the turing program...)

Infinite Recursion Sep 27th, 2005 9:44 PM

cool stuff

Sane Sep 27th, 2005 9:55 PM

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:

http://1v7.com/drsane/photo1.bmp

http://1v7.com/drsane/photo2.bmp




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.

DaWei Sep 27th, 2005 10:06 PM

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.

Sane Sep 27th, 2005 10:15 PM

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. :)

Infinite Recursion Sep 27th, 2005 10:21 PM

How long have you been doing Python Sane?


All times are GMT -5. The time now is 9:32 PM.

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