Python Forum
Python Pillow - Photo Manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Pillow - Photo Manipulation
#1
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.
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pillow _getexif for python 3 Larz60+ 3 31,759 May-28-2025, 11:48 AM
Last Post: StephenTuh
  tkinter photo image problem jacksfrustration 5 6,162 Jun-27-2024, 12:06 AM
Last Post: AdamHensley
  Python Code Help - pip install PyMuPDF python-docx pillow Splishsplash92 3 5,023 Jun-05-2024, 06:49 AM
Last Post: Pedroski55
  aoigram, pil Help with photo processing kolpac21 0 2,169 Aug-07-2023, 04:59 PM
Last Post: kolpac21
  Python re.sub text manipulation on matched contents before substituting xilex 2 4,051 May-19-2020, 05:42 AM
Last Post: xilex
  Add Photo in Python adninqasifa 4 14,567 Nov-26-2018, 06:06 PM
Last Post: adninqasifa
  Python Pillow I_Am_Groot 1 3,805 Oct-13-2018, 07:28 AM
Last Post: j.crater
  [split] Python Pillow - Photo Manipulation keegan_010 1 4,719 Oct-11-2018, 09:57 AM
Last Post: Larz60+
  Python Variable manipulation KirkmanJ 8 7,123 Aug-14-2018, 11:13 PM
Last Post: snippsat
  Combine images using Pillow and Python GMA 2 17,363 Jun-06-2018, 11:41 AM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020