1

I want to keep track of a point/pixel for reference in a PIL image while I do a (perspective) transformation and cut off the transparent borders.

from PIL import Image

# load image
img = Image.open("img.png")

# do some perspective transformation
img.transform(new_size, Image.PERSPECTIVE, mapping_coeffs)

# cut the borders
img = img.crop(img.getbbox())

For the cropping I could keep track of a position by subtracting the size of the padding. But how can I do this for a perspective transformation, or even multiple transformations in a row?

Ricoter
  • 665
  • 5
  • 17
  • where is your code for :'For the cropping I could keep track of a position by subtracting the size of the padding.' ?? . when you resize an Image your reference pixel wont exist anymore, or not ? – pippo1980 Feb 09 '21 at 18:08
  • was thinking about recalculate the pixel position respect the new size, not sure it will always work – pippo1980 Feb 09 '21 at 18:15
  • guess you need to know how the resizing works to figure out which pixel represent your initial pixel – pippo1980 Feb 09 '21 at 19:12
  • more here : https://stackoverflow.com/questions/48121916/numpy-resize-rescale-image – pippo1980 Feb 09 '21 at 19:34
  • `ref = [x,y]; box = img.getbbox(); ref_new = [ref[0]-box[0], ref[1]-box[1]` the first two elements of box represent the lower bound of x and y. – Ricoter Feb 10 '21 at 12:32
  • I see you were talking about image.transform : https://stackoverflow.com/questions/14177744/how-does-perspective-transformation-work-in-pil or not ? I though you were talking about perspective meaning any kind of transform like resizing too ... sorry about that – pippo1980 Feb 10 '21 at 12:54

1 Answers1

0

For others with the same question, I made a black image with only the reference pixel in white using NumPy and transformed it in the same way as my image.

from PIL import Image
import numpy as np

# get black img with the same size
refArray = np.zeros(PILimg.size)

# make the reference pixel white
refArray[xRef, yRef] = 1e8

# to PIL image object 
refImg = Image.fromarray(refArray.T)  

Do the same transformations with the reference image, and then find the max value in the transformed reference image

ref = np.array(refImg).T
xRef, yRef = np.unravel_index(np.argmax(ref), ref.shape)

edit: For some transformations the pixel disappears, this is solved by using a small square of pixels (5x5) instead of a single pixel.

Ricoter
  • 665
  • 5
  • 17