#๐Ÿ”’ Image overlay help for Python beginner

21 messages ยท Page 1 of 1 (latest)

real kayak
#

I'm struggling hard and am still new to coding, I have an assignment due and was looking for advice on my code as I have frequent improper syntax. I can already see a few mistakes such as pix and being unsure of where to specify specific x,y coordinates, but here's what I have so far (assignment requirements are near the bottom):

from PIL import Image

img2 = Image.open("edinburgh-castle.jpg")

width,height = img2.size

def draw_lines(img):
for x in range (width):
for y in range(height):
pix = img2.getpixel((x,y))
img2.getpixel((106,240))
green_component = pix[1]
newpix = (green_component,green_component,green_component,green_component)
img2.putpixel((106,240),newpix)
img.save("edinburgh-castle.jpg")
img2.show("edinburgh-castle.jpg")
for x in range(width):
for y in range(height):
pix = img2.getpixel((x,y))
img2.getpixel((320,80))
blue_component = pix[2]
newpix = (blue_component,blue_component,blue_component,blue_component)
img2.putpixel((320,80),newpix)
img.save("edinburgh-castle.jpg")
img2.show("edinburgh-castle.jpg")
return img

  •   Write code below to draw a green vertical line from top to bottom,
     1/3 of the way across the edinburgh-castle.gif; also draw a
     horizontal blue line 1/3 of the way down the same image. Return
     the image
    
    Draw your green and blue lines on the image*

I'm not receiving specific error, but I'm not getting an output either, even when commenting out return. The image dimensions are 320x240

unborn robinBOT
#

@real kayak

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.

real kayak
#

Would I also need to specify the file path more specifically?? Again I'm not receiving an error in that regard but I'm wondering if it's even drawing out of the right library atp

compact sphinx
#

@real kayak is that all of the code?

#

also put it in three of ` on either side and start it with a line of "py"

#

like this

#
print("hello world")```
#

if that is all the code you haven't called the function

real kayak
#

#Asn4.py: Image processing

from PIL import Image

img2 = Image.open("edinburgh-castle.jpg")

width,height = img2.size

def draw_lines(img):
for x in range (width):
for y in range(height):
pix = img2.getpixel((x,y))
img2.getpixel((106,240))
green_component = pix[1]
newpix = (green_component,green_component,green_component,green_component)
img2.putpixel((106,240),newpix)
img.save("edinburgh-castle.jpg")
img2.show("edinburgh-castle.jpg")
for x in range(width):
for y in range(height):
pix = img2.getpixel((x,y))
img2.getpixel((320,80))
blue_component = pix[2]

unborn robinBOT
#

Hey @real kayak!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
real kayak
#

newpix = (blue_component,blue_component,blue_component,blue_component)
img2.putpixel((320,80),newpix)
img.save("edinburgh-castle.jpg")
img2.show("edinburgh-castle.jpg")
return img

#    Write code below to draw a green vertical line from top to bottom,
#    1/3 of the way across the edinburgh-castle.gif; also draw a
#    horizontal blue line 1/3 of the way down the same image. Return
#    the image
# Draw your green and blue lines on the image

def border(img):

Write code below to draw a black border 10 pixels wide around

arch.gif; this amounts to drawing 10 black lines across the

top and bottom and 10 vertical black lines on the left and

right sides.

#Draw the border on img

return img

def blend(img1, img2):

Write code below to blend .6 of img1 with .4 of img2; assume

img1 and img2 are the same size

blended = Image.new("RGB", (width, height))

#for y in range of the height of new_img

# for x in the range of the width of new_image

# get a pixel from the x, y position of img1

# get a pixel from the x, y position of img2

# set r to 0.6 * red from img1 pixel + 0.4 * red from img2 pixel

# create g & b using the same method as for r

# make a new pixel using r, g, and b

# set the pixel in the x, y position in new_img to the new pixel

return blended

#Don't change anythign below this point

def main():

img1 = Image.open('images/edinburgh-castle.jpg')

new_img1 = draw_lines(img1)

new_img1.show()

img2 = Image.open('images/arch.jpg')

new_img2 = border(img2)

new_img2.show()

img3 = Image.open('images/beach.jpg')

img4 = Image.open('images/blueMotorcycle.jpg')

img5 = blend(img3, img4)

img5.show()

main()

#

i'll fix the bold in a moment because holy hell

real kayak
# compact sphinx ```py print("hello world")```

but either way i have it commented out, thank you for the assistance however! ill go ahead and add that, i wasn't sure if print worked with images or not since it's from an imported library

compact sphinx
#

im talking about discord formatting

real kayak
#

oh LOL my bad, i'll do that, i misinterpreted what you said and I thought you were meaning use print to call on my function lol

mighty epoch
#

!code

unborn robinBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

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

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

unborn robinBOT
#
Python help channel closed for inactivity

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.