PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

[FIXED] How to add a row of pixels at the bottom and to the right of an image?

 May 05, 2022     image, image-processing, numpy, python, python-imaging-library     No comments   

Issue

So Im experiencing an issue with a framework that Im working with, where the images sort of get translated by 1 pixel downwards and one pixel to the left (making the pixel data wrong).

To counteract this I would like to add a one pixel "padding" then at the bottom & to the left. The script needs to be relatively fast as its an interactive software Im working with, so iterating over the pixels in a python for loop wont work as an option 😅

I have Numpy & Pillow installed already, so those modules are very welcome to be used in any solutions

The tricky part is, the new row of pixels needs to "blend in" with the pixels next to them (so ideally the padding to the left would steal the color of its right neigbour, and the pixels at the bottom would take the color from the pixel right above) - they cant just all be black.


Solution

You can expand a PIL Image like this.

You can then make it into a Numpy array using:

na = np.array(PILImage)

You can then set the first column and last row using Numpy indexing:

na[:, 0]  = na[:, 1]   # first col = second col
na[-1, :] = na[-2, :]  # last row = penultimate row

Then make it back into a PIL image with:

im = Image.fromarray(na)


Answered By - Mark Setchell
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing