PIL is pasting the original image instead of the cropped image
i would like to get some help, why isn't Pillow putting the cropped image and not the original when i save the image?
It displays as cropped when i save it after cropping it, but when i paste it, it pastes the original instead, even after saving the cropped one and loading the cropped image and then pasting it.
Here's my code, to recreate the issue:
import os
from PIL import Image, ImageDraw, ImageOps, ImageFont, ImageFilter
filename = "image to crop.png"
offset = 50,50
size = 400,400
avatar = Image.open(filename)
avatar = avatar.resize(size, Image.ANTIALIAS)
rad = 200
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
background = Image.open("image to paste to.png")
background.paste(avatar, offset)
background.save("image with cropped image pasted.png")
python-3.x python-imaging-library
add a comment |
i would like to get some help, why isn't Pillow putting the cropped image and not the original when i save the image?
It displays as cropped when i save it after cropping it, but when i paste it, it pastes the original instead, even after saving the cropped one and loading the cropped image and then pasting it.
Here's my code, to recreate the issue:
import os
from PIL import Image, ImageDraw, ImageOps, ImageFont, ImageFilter
filename = "image to crop.png"
offset = 50,50
size = 400,400
avatar = Image.open(filename)
avatar = avatar.resize(size, Image.ANTIALIAS)
rad = 200
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
background = Image.open("image to paste to.png")
background.paste(avatar, offset)
background.save("image with cropped image pasted.png")
python-3.x python-imaging-library
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please provideimage to crop.png
andimage to paste.png
and images showing what you get and what you expected.
– Mark Setchell
Nov 28 '18 at 18:07
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57
add a comment |
i would like to get some help, why isn't Pillow putting the cropped image and not the original when i save the image?
It displays as cropped when i save it after cropping it, but when i paste it, it pastes the original instead, even after saving the cropped one and loading the cropped image and then pasting it.
Here's my code, to recreate the issue:
import os
from PIL import Image, ImageDraw, ImageOps, ImageFont, ImageFilter
filename = "image to crop.png"
offset = 50,50
size = 400,400
avatar = Image.open(filename)
avatar = avatar.resize(size, Image.ANTIALIAS)
rad = 200
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
background = Image.open("image to paste to.png")
background.paste(avatar, offset)
background.save("image with cropped image pasted.png")
python-3.x python-imaging-library
i would like to get some help, why isn't Pillow putting the cropped image and not the original when i save the image?
It displays as cropped when i save it after cropping it, but when i paste it, it pastes the original instead, even after saving the cropped one and loading the cropped image and then pasting it.
Here's my code, to recreate the issue:
import os
from PIL import Image, ImageDraw, ImageOps, ImageFont, ImageFilter
filename = "image to crop.png"
offset = 50,50
size = 400,400
avatar = Image.open(filename)
avatar = avatar.resize(size, Image.ANTIALIAS)
rad = 200
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
background = Image.open("image to paste to.png")
background.paste(avatar, offset)
background.save("image with cropped image pasted.png")
python-3.x python-imaging-library
python-3.x python-imaging-library
edited Nov 28 '18 at 17:50
VisualCoder
asked Nov 25 '18 at 20:15
VisualCoderVisualCoder
115
115
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please provideimage to crop.png
andimage to paste.png
and images showing what you get and what you expected.
– Mark Setchell
Nov 28 '18 at 18:07
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57
add a comment |
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please provideimage to crop.png
andimage to paste.png
and images showing what you get and what you expected.
– Mark Setchell
Nov 28 '18 at 18:07
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please provide
image to crop.png
and image to paste.png
and images showing what you get and what you expected.– Mark Setchell
Nov 28 '18 at 18:07
Please provide
image to crop.png
and image to paste.png
and images showing what you get and what you expected.– Mark Setchell
Nov 28 '18 at 18:07
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57
add a comment |
2 Answers
2
active
oldest
votes
On line 444, you are not assigning the output of the resize. resize
returns a new image, it doesn’t modify the original image.
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
add a comment |
So i found an solution! the problem was that i didn't assign the mask when pasting it!
here's the code that fixed it:
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=255)
avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
avatar.putalpha(mask)
background = Image.open("welcomecardbackground.jpg")
background.paste(avatar, offset, mask)
instead of
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471501%2fpil-is-pasting-the-original-image-instead-of-the-cropped-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
On line 444, you are not assigning the output of the resize. resize
returns a new image, it doesn’t modify the original image.
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
add a comment |
On line 444, you are not assigning the output of the resize. resize
returns a new image, it doesn’t modify the original image.
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
add a comment |
On line 444, you are not assigning the output of the resize. resize
returns a new image, it doesn’t modify the original image.
On line 444, you are not assigning the output of the resize. resize
returns a new image, it doesn’t modify the original image.
answered Nov 25 '18 at 20:33
GniemGniem
914
914
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
add a comment |
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
I assigned it, but it didn't fix the issue.
– VisualCoder
Nov 27 '18 at 13:02
add a comment |
So i found an solution! the problem was that i didn't assign the mask when pasting it!
here's the code that fixed it:
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=255)
avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
avatar.putalpha(mask)
background = Image.open("welcomecardbackground.jpg")
background.paste(avatar, offset, mask)
instead of
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
add a comment |
So i found an solution! the problem was that i didn't assign the mask when pasting it!
here's the code that fixed it:
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=255)
avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
avatar.putalpha(mask)
background = Image.open("welcomecardbackground.jpg")
background.paste(avatar, offset, mask)
instead of
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
add a comment |
So i found an solution! the problem was that i didn't assign the mask when pasting it!
here's the code that fixed it:
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=255)
avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
avatar.putalpha(mask)
background = Image.open("welcomecardbackground.jpg")
background.paste(avatar, offset, mask)
instead of
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
So i found an solution! the problem was that i didn't assign the mask when pasting it!
here's the code that fixed it:
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=255)
avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
avatar.putalpha(mask)
background = Image.open("welcomecardbackground.jpg")
background.paste(avatar, offset, mask)
instead of
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', _avatar.size, "white")
w, h = _avatar.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
alpha = alpha.resize(size)
avatar.putalpha(alpha)
answered Nov 28 '18 at 19:29
VisualCoderVisualCoder
115
115
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471501%2fpil-is-pasting-the-original-image-instead-of-the-cropped-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please make sure to show the Minimum Complete Verifiable Example as required by StackOverflow rules.
– Mark Setchell
Nov 25 '18 at 20:21
Please provide
image to crop.png
andimage to paste.png
and images showing what you get and what you expected.– Mark Setchell
Nov 28 '18 at 18:07
i have fixed the issue, as i said below
– VisualCoder
Nov 29 '18 at 12:57