Python Forum
Python Pillow - Photo Manipulation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Pillow - Photo Manipulation (/thread-13328.html)



Python Pillow - Photo Manipulation - keegan_010 - Oct-10-2018

Hi,
I am trying to use pillow for an online python photo manipulation course, I am only starting at the basics one of my tasks is to write a program which keeps only the blue channel from an image. Their hint is to loop through the pixels in the image and set the red and green values of each pixel to 0. The code I am trying is:
from PIL import Image

file = input("File name: ")
img = Image.open(file)

red, green, blue = img.split()

for y in range (red.height):
  for x in range (red.width):
    img.putpixel((x, y), 0)
    
for y in range (green.height):
  for x in range (green.width):
    img.putpixel((x, y), 0)
    
new_image = Image.merge ('RGB', (red, green, blue))

    
img.save("output.png")
However, after I do this, I only get an image that is totally black.

Can anybody help with this?

Thanks.


RE: Python Pillow - Photo Manipulation - j.crater - Oct-10-2018

Hello,
I checked the docs for putpixel() here, and I believe that instead of 0 (which probably means black), you should place a RGB tuple, so (0, 0, blue_value).

By the way, may I ask what is the course you are taking? Thanks.


RE: Python Pillow - Photo Manipulation - keegan_010 - Oct-11-2018

Hi j.crater,
Thanks for your help but I figured out where I went wrong. The course is on a website called Grok, and the course is called Investigating Images. Although you have to pay for the course, but I get it free as an education Queensland student.
Thanks.