#πŸ”’ New to Py, and want to finish this ASAP!

108 messages Β· Page 1 of 1 (latest)

cyan moth
#
from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
    star_list = []
    for i in range(nstars):
        x = random.randint(0, sky_width)
        y = random.randint(0, sky_height)
        star_list.append((x, y))
    return star_list

img = Image.new("RGB", (350, 300), (3, 20, 41))
draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 229)
for (sx, sy) in stars:
    draw.circle((sx, sy), 1, fill=(245, 244, 229))

draw.circle((275, 75), 30, fill=(247, 234, 206))

cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w // 2, cy - h // 2)
bottom_right = (cx + w // 2 - 1, cy + h // 2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_left = door_cx - door_w // 2
door_right = door_cx + door_w // 2 - 1

for x in range(0, 350, 2):
    if door_left <= x <= door_right:
        continue
    draw.line([(x, 227), (x, 229)], fill=(16, 94, 12))

door_upper_left = (door_cx - door_w // 2, door_cy - door_h // 2)
door_bottom_right = (door_cx + door_w // 2 - 1, door_cy + door_h // 2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

if __name__ == "__main__":
    img.show()```
livid pulsarBOT
#

@cyan moth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cyan moth
merry escarp
#

oh

cyan moth
#

previouys question before i had to add the grass, from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
star_list = []
for i in range(nstars):
x = random.randint(0, sky_width)
y = random.randint(0, sky_height)
star_list.append((x, y))
return star_list

img = Image.new('RGB', (350, 300), (3, 20, 41))

draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 230)
for (sx, sy) in stars:
draw.ellipse((sx-1, sy-1, sx+1, sy+1), fill=(245, 244, 229))

cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w//2, cy - h//2)
bottom_right = (cx + w//2 - 1, cy + h//2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

moon_left = 275 - 30
moon_top = 75 - 30
moon_right = 275 + 30
moon_bottom = 75 + 30
draw.ellipse((moon_left, moon_top, moon_right, moon_bottom), fill=(247, 234, 206))

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_upper_left = (door_cx - door_w//2, door_cy - door_h//2)
door_bottom_right = (door_cx + door_w//2 - 1, door_cy + door_h//2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

if name == "main":
img.show()

#

like im gonna tweak

#

i got that right

#

now when it was time to add the grass

#

all shit goes south

merry escarp
#

what are you having trouble adding

#

oh

#

ok

cyan moth
#

i added thge grass

#

but the issue is

#

this

#

my image

#

"the right image"

merry escarp
cyan moth
#

breh

#

these are the instructions for the grass\

merry escarp
#

it's supposted to be (16, 94,12)

#

wait

cyan moth
#

whats the color then...

merry escarp
#

tghat's weird

cyan moth
#

is that not what i have

merry escarp
#

yeah i just chekced ur code sorry 😭

cyan moth
#

im gonna lose it

#

and i dont wanna wait until tomorrow to do this i wanna get it done TONIGHT

merry escarp
#

you're sure that the grass is on the surface of the green?

cyan moth
#

here

#

compare the codes before i added the grass

#

to vs after

#

BEFORE:

from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
    star_list = []
    for i in range(nstars):
        x = random.randint(0, sky_width)
        y = random.randint(0, sky_height)
        star_list.append((x, y))
    return star_list
    
img = Image.new('RGB', (350, 300), (3, 20, 41))

draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 230)
for (sx, sy) in stars:
    draw.ellipse((sx-1, sy-1, sx+1, sy+1), fill=(245, 244, 229))
    
cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w//2, cy - h//2)
bottom_right = (cx + w//2 - 1, cy + h//2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

moon_left = 275 - 30
moon_top = 75 - 30
moon_right = 275 + 30
moon_bottom = 75 + 30
draw.ellipse((moon_left, moon_top, moon_right, moon_bottom), fill=(247, 234, 206))

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_upper_left = (door_cx - door_w//2, door_cy - door_h//2)
door_bottom_right = (door_cx + door_w//2 - 1, door_cy + door_h//2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

if __name__ == "__main__":
    img.show()

AFTER:

from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
    star_list = []
    for i in range(nstars):
        x = random.randint(0, sky_width)
        y = random.randint(0, sky_height)
        star_list.append((x, y))
    return star_list

img = Image.new("RGB", (350, 300), (3, 20, 41))
draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 229)
for (sx, sy) in stars:
    draw.circle((sx, sy), 1, fill=(245, 244, 229))

draw.circle((275, 75), 30, fill=(247, 234, 206))

cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w // 2, cy - h // 2)
bottom_right = (cx + w // 2 - 1, cy + h // 2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_left = door_cx - door_w // 2
door_right = door_cx + door_w // 2 - 1

for x in range(0, 350, 2):
    if door_left <= x <= door_right:
        continue
    draw.line([(x, 227), (x, 229)], fill=(16, 94, 12))

door_upper_left = (door_cx - door_w // 2, door_cy - door_h // 2)
door_bottom_right = (door_cx + door_w // 2 - 1, door_cy + door_h // 2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

if __name__ == "__main__":
    img.show()```
#

i am not insane

merry escarp
#

is the line length inclusive meaning it draws the line including 227 and 229 or does it leave 227/doesn't draw 227 as a green pixel)

cyan moth
#

for the new or the old

merry escarp
#

new

cyan moth
#

yes, it doesnt skip 227 it draws 227 and 229

merry escarp
#

ok i see

cyan moth
#

please

#

help me fix this

#

ive been losing it for an hour

merry escarp
#

maybe try range(0,351,2) because the stop number in the range function is not included in the range

#

otherwise i'll need to look closer which I can't really do right now (busy)

cyan moth
#

i already have for x in range(0, 350, 2): no?

#

like what does this mean

merry escarp
# cyan moth

you're doing something wrong on the blades of grass (all of them)

cyan moth
#

but what could it be 😭

#

how does 6$ sound to fix this

#

im gonna lose it

merry escarp
#

ill brb sorry

cyan moth
#

all good

merry escarp
#

it might be that your grass might be placed a bit too high or too low

#

try changing the y-pos of the grass up or down a little bit

cyan moth
#

doesnt my grass look like

#

it has more than it should

merry escarp
#

if that was the case then there would be tiny spaces in between each grass pixel in the differences image

#

your grass looks too short to me

#

maybe try lifting the grass up one pixel by editing your line function

#

@cyan moth

cyan moth
#

the draw.rectangle([(0, 230), (349, 299)], fill="green")

merry escarp
#

no the grass blades

#

tell me when you're done

livid pulsarBOT
#

Hey @cyan moth!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
cyan moth
#
from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
    star_list = []
    for i in range(nstars):
        x = random.randint(0, sky_width)
        y = random.randint(0, sky_height)
        star_list.append((x, y))
    return star_list

img = Image.new("RGB", (350, 300), (3, 20, 41))
draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 229)
for (sx, sy) in stars:
    draw.circle((sx, sy), 1, fill=(245, 244, 229))

draw.circle((275, 75), 30, fill=(247, 234, 206))

cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w // 2, cy - h // 2)
bottom_right = (cx + w // 2 - 1, cy + h // 2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_left = door_cx - door_w // 2
door_right = door_cx + door_w // 2 - 1

for x in range(0, 350, 2):
    if door_left <= x <= door_right:
        continue
    draw.line([(x, 220), (x, 235)], fill=(16, 94, 12))

door_upper_left = (door_cx - door_w // 2, door_cy - door_h // 2)
door_bottom_right = (door_cx + door_w // 2 - 1, door_cy + door_h // 2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

if __name__ == "__main__":
    img.show()
merry escarp
#

try 228 and 230

cyan moth
merry escarp
#

huh

#

ok revert your grass back to how it was before

#

(227 and 229

#

bro this is stumping me

cyan moth
merry escarp
#

it might be your grass needs to be lengthened

#

try 226 and 229

cyan moth
#

omg

#

YOU DID IT

#

😭

#

25 attempts later

#

gg

merry escarp
#

gg

cyan moth
#

i hope my teacher gets fried for this

merry escarp
#

have fun in your coding adventure

cyan moth
#

much love

#

im keeping this open for a bit longer if you could stay

#

ima finish this in like 30 minutes

#

if any other issues abrupt

merry escarp
#

remember these threads close after an hr of inactivity so yeah you can do that

cyan moth
#

alright

cyan moth
#

@merry escarp

#

yo 😭

#
from PIL import Image, ImageDraw
import random

def generate_star_list(nstars, sky_width, sky_height):
    star_list = []
    for i in range(nstars):
        x = random.randint(0, sky_width)
        y = random.randint(0, sky_height)
        star_list.append((x, y))
    return star_list

def crop_image(img):
    img2 = img.copy()
    color_with_alpha = (0, 0, 0, 0)
    for x in range(img2.width):
        for y in range(img2.height):
            r, g, b, a = img.getpixel((x, y))
            if r == 0 and g == 0 and b == 0:
                img2.putpixel((x, y), color_with_alpha)
    return img2

img = Image.new("RGBA", (350, 300), (3, 20, 41, 255))
draw = ImageDraw.Draw(img)

stars = generate_star_list(80, 349, 229)
for (sx, sy) in stars:
    draw.circle((sx, sy), 1, fill=(245, 244, 229, 255))

draw.circle((275, 75), 30, fill=(247, 234, 206, 255))

cx = 100
cy = 200
w = 100
h = 60
upper_left = (cx - w // 2, cy - h // 2)
bottom_right = (cx + w // 2 - 1, cy + h // 2 - 1)
draw.rectangle([upper_left, bottom_right], fill="firebrick")

draw.polygon([(50, 169), (149, 169), (100, 140)], fill="blue")

draw.rectangle([(0, 230), (349, 299)], fill="green")

door_w = 20
door_h = 40
door_cx = 100
door_cy = 210
door_left = door_cx - door_w // 2
door_right = door_cx + door_w // 2 - 1

for x in range(0, 350, 2):
    if door_left <= x <= door_right:
        continue
    draw.line([(x, 227), (x, 229)], fill=(16, 94, 12, 255))

path_top = (door_cy + door_h // 2 - 1) - 1
color_a = (50, 50, 50, 255)
color_b = (75, 75, 75, 255)

for y in range(path_top, 300, 8):
    for x in range(door_left, door_right + 1, 4):
        brick_left = x
        brick_top = y
        brick_right = x + 3
        brick_bottom = y + 7
        if brick_bottom > 299:
            continue
        if ((x - door_left) // 4 + (y - path_top) // 8) % 2 == 0:
            fill_color = color_a
        else:
            fill_color = color_b
        draw.rectangle([(brick_left, brick_top), (brick_right, brick_bottom)], fill=fill_color)

door_upper_left = (door_cx - door_w // 2, door_cy - door_h // 2)
door_bottom_right = (door_cx + door_w // 2 - 1, door_cy + door_h // 2 - 1)
draw.rectangle([door_upper_left, door_bottom_right], fill="blue")

bird_img = Image.open("bird.png").convert("RGBA")
resized_bird_img = bird_img.resize((50, 50))
cropped_bird_img = crop_image(resized_bird_img)

frames = []
temp_img = img.copy()
temp_img.alpha_composite(cropped_bird_img, (0, 120))
frames.append(temp_img)

if __name__ == "__main__":
    img.show()```
#

we're good on this

cyan moth
#

!close

livid pulsarBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.