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()```
#π New to Py, and want to finish this ASAP!
108 messages Β· Page 1 of 1 (latest)
@cyan moth
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.
Closes after a period of inactivity, or when you send !close.
what does your current program look like
oh
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
whats the color then...
tghat's weird
is that not what i have
yeah i just chekced ur code sorry π
im gonna lose it
and i dont wanna wait until tomorrow to do this i wanna get it done TONIGHT
you're sure that the grass is on the surface of the green?
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
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)
for the new or the old
new
yes, it doesnt skip 227 it draws 227 and 229
ok i see
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)
ill brb sorry
all good
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
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
the draw.rectangle([(0, 230), (349, 299)], fill="green")
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!')```
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()
you're shifting it up too high
try 228 and 230
huh
ok revert your grass back to how it was before
(227 and 229
bro this is stumping me
gg
i hope my teacher gets fried for this
have fun in your coding adventure
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
remember these threads close after an hr of inactivity so yeah you can do that
alright
@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
!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.