I am trying to return True or False
by comparing dimension size(width and heights) of two pictures.
so i came up with
import media

f = media.choose_file()
pic = media.load_picture(f)

g = media.choose_file()
pic1 = media.load_picture(g)

x = pic.get_width() * pic.get_height()
y = pic1.get_width() * pic1.get_height()

but i have no idea return these valuse to true and false(bool).

Also, i have another thing to solve,

Return a new 20 pixel by 20 pixel Picture that has a white background, a black 16 by 16 oval at position (0, 0), and a black letter C (uppercase) at position (6, 3).

For this one i came up with,

pic = media.create_picture(20, 20)

i know that i have to use media.add_oval and media.add_text function
but i have no idea how to.
plz help me out here.

Dani AI

Generated

Short expert notes tying the thread together and giving ready-to-run examples.

For comparing two pictures: comparing area (width * height) works, but it’s clearer and less error-prone to compare the two dimensions directly. A compact, readable helper that returns True/False is:

def same_size(pic, pic1):
    return (pic.get_width(), pic.get_height()) == (pic1.get_width(), pic1.get_height())

This uses the picture API’s width/height accessors so it will behave the same way across the media library implementations used in teaching environments. (pythonhosted.org)

To produce the 20x20 icon with a white background, a filled black 16x16 oval at (0,0), and a black uppercase "C" at (6,3), use the media drawing helpers (names vary slightly by install — some ports use snake_case, others camelCase). Example using the common snake_case API:

def make_20_icon():
    black = media.create_color(0, 0, 0)
    white = media.create_color(255, 255, 255)
    pic = media.create_picture(20, 20, white)
    media.add_oval_filled(pic, 0, 0, 16, 16, black)
    media.add_text(pic, 6, 3, "C", black)
    return pic

If your environment uses camelCase names (JES/Jython variants) the calls will be addOvalFilled / addText / makeStyle instead; check the local docs for the exact names. (pythonhosted.org)

Practical tips and gotchas: on a 20x20 canvas default fonts can be large or clipped — if the letter is too big or unreadable, use the styled-text API (makeStyle / addTextWithStyle or make_style / add_text_withstyle depending on your port) to pick a smaller font size, and tweak the (x,y) until the C sits where you want it. Also note ’s approach using PIL is a valid alternative, but when the assignment expects the course media API it’s simpler to use the media.add... and media.add_text functions (and remember to pass black as the text color so the letter isn’t drawn white by mistake). (cs.gordon.edu)

Recommended Answers

All 5 Replies

So it looks like you are writing a function to see if the areas of the pictures are the same? (x is the area of pic, and y is the area of pic1)

Simply end your function with

return(x==y)

This will return True or False.

what about the second one?
oh
actually im not comparing the area, im comparing each size.

In that case there is no need to calculate the area.

Instead, just compare the dimensions. Something like:

return (pic.get_width() == pic1.get_width()) and (pic.get_height() == pic1.get_height())

CAn you please help me with media.add_oval and media.add_text functions to
return a new 20 pixel by 20 pixel Picture that has a white background, a black 16 by 16 oval at position (0, 0), and a black letter C (uppercase) at position (6, 3)?

Ok, so I've looked over your assignment, and I've come up with this. I don't know if it really helps you, but here goes.

'''
Created on Feb 15, 2012

@author: sin
'''
import Image, ImageDraw #As a first thing, you must have the third party graphics libraries.
#You can easily download them off of 

def circleplusc():# Defining the new function
    img = Image.new("RGB", (20, 20), "#FFFFFF") #Creating the new imagine, with the 20x20 pixels.
    draw = ImageDraw.Draw(img) # We assign the function imagedraw.draw(img) to a value draw, for an easy use up ahead.

    r, g, b = 0, 0, 0 # Assigning colors to the rgb values, r=0, g=0, b=0, meaning r=red, g=green, b=blue
    r1, g1, b1 = 255, 255, 255 #Same as above.
    draw.ellipse((0, 0, 16, 16), fill=(int(r), int(g), int(b))) #We draw the ellipse, oval, circle, depending on the coordinates.
    draw.text((6, 3), "C", fill=(int(r1), int(g1), int(b1)))# We draw the text, literally... having as colors the opposite of the elipse. 

    img.save("circleplusc.png", "PNG") # We save the imagine as a PNG.
if __name__ == "__main__":
    circleplusc() #We run the function.

#Tested with Python 2.7.2 interpretor, I personally use Eclipse for a better use on Python, but still, tested with Python also.
#The output is a PNG image, of a 20x20 pixels, with a black circle, and nearly its center a white C
#Tested and completed Feb 15, 2012 by sin.

Please report me any bugs, or errors, as I myself i'm a n00b @ Python, and still learning.:D
Hope I helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.