Skip to content

Add RandomTranslation transform #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Add RandomTranslation transform #363

wants to merge 4 commits into from

Conversation

daavoo
Copy link
Contributor

@daavoo daavoo commented Dec 7, 2017

Using PIL.Image.transform and PIL.Image.AFFINE

@fmassa
Copy link
Member

fmassa commented Dec 13, 2017

Hey, thanks for the PR!

Quick question: is this equivalent to crop + scale? I'm not sure how PIL affine behave on the boundaries of the image, could you expand on that?

Thanks!

@daavoo
Copy link
Contributor Author

daavoo commented Dec 18, 2017

Hi! I had never thought about it but after some tests, yes. You can achieve the same results using either translate or crop. It appears that regarding the borders the affine transform behaves just like in crop, it fills with 0s the outside of the image. For example the following transforms are equivalent:

import numpy as np
from torchvision.transforms.functional import to_pil_image

image = to_pil_image((np.random.rand(100, 100, 1)* 255).astype(np.uint8))

horizontal = 10
vertical = 10

t = image.transform(image.size, Image.AFFINE, (1, 0, horizontal, 0, 1, vertical))

c = image.crop((0 + horizontal, 0 + vertical, image.width + horizontal, image.height + vertical))

np.testing.assert_equal(np.array(t), np.array(c))

So I guess that translate could use crop internally or should we still use the affine transform?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants