How do you pass an image from Wand to Pillow?
I'm trying to do some OCR work. pytesseract
doesn't play nicely with Wand images, but Python Image Library can't do some of the image transforms I want to do that make the OCR perform better.
At the moment, I'm using Wand to open the image, do my ImageMagick transforms, then saving it to a temp file which I then open using Pillow and pass to pytesseract. Is there a way I can do this without using a temp file? I'd like to be able to handle this all using functions that can't potentially foul each other up if they're running in parallel.
python python-imaging-library wand
|
show 3 more comments
I'm trying to do some OCR work. pytesseract
doesn't play nicely with Wand images, but Python Image Library can't do some of the image transforms I want to do that make the OCR perform better.
At the moment, I'm using Wand to open the image, do my ImageMagick transforms, then saving it to a temp file which I then open using Pillow and pass to pytesseract. Is there a way I can do this without using a temp file? I'd like to be able to handle this all using functions that can't potentially foul each other up if they're running in parallel.
python python-imaging-library wand
Instead of a temp file - can you save it to aBytesIO
object instead and and pass that as the input to pillow?
– Jon Clements♦
Nov 21 '18 at 17:27
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38
|
show 3 more comments
I'm trying to do some OCR work. pytesseract
doesn't play nicely with Wand images, but Python Image Library can't do some of the image transforms I want to do that make the OCR perform better.
At the moment, I'm using Wand to open the image, do my ImageMagick transforms, then saving it to a temp file which I then open using Pillow and pass to pytesseract. Is there a way I can do this without using a temp file? I'd like to be able to handle this all using functions that can't potentially foul each other up if they're running in parallel.
python python-imaging-library wand
I'm trying to do some OCR work. pytesseract
doesn't play nicely with Wand images, but Python Image Library can't do some of the image transforms I want to do that make the OCR perform better.
At the moment, I'm using Wand to open the image, do my ImageMagick transforms, then saving it to a temp file which I then open using Pillow and pass to pytesseract. Is there a way I can do this without using a temp file? I'd like to be able to handle this all using functions that can't potentially foul each other up if they're running in parallel.
python python-imaging-library wand
python python-imaging-library wand
edited Nov 21 '18 at 18:03
asked Nov 21 '18 at 17:25
futuraprime
3,05042336
3,05042336
Instead of a temp file - can you save it to aBytesIO
object instead and and pass that as the input to pillow?
– Jon Clements♦
Nov 21 '18 at 17:27
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38
|
show 3 more comments
Instead of a temp file - can you save it to aBytesIO
object instead and and pass that as the input to pillow?
– Jon Clements♦
Nov 21 '18 at 17:27
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38
Instead of a temp file - can you save it to a
BytesIO
object instead and and pass that as the input to pillow?– Jon Clements♦
Nov 21 '18 at 17:27
Instead of a temp file - can you save it to a
BytesIO
object instead and and pass that as the input to pillow?– Jon Clements♦
Nov 21 '18 at 17:27
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:
In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:
In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38
|
show 3 more comments
1 Answer
1
active
oldest
votes
To go from Wand to PIL, you can use io.BytesIO
:
with io.BytesIO() as transfer:
with WandImage(filename=fpath) as img:
img.unsharp_mask(radius=2, sigma=0, amount=75, threshold=2)
img.level(black=150/255, white=202/255, gamma=2.1)
img.save(transfer)
with Image.open(transfer) as img:
text = pytesseract.image_to_string(img)
with open('./%s/%s-tess4.txt' % (outputdir, fname), 'w+') as f:
f.write(text)
This does not seem to work to move a PIL image into Wand, however.
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%2f53417573%2fhow-do-you-pass-an-image-from-wand-to-pillow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To go from Wand to PIL, you can use io.BytesIO
:
with io.BytesIO() as transfer:
with WandImage(filename=fpath) as img:
img.unsharp_mask(radius=2, sigma=0, amount=75, threshold=2)
img.level(black=150/255, white=202/255, gamma=2.1)
img.save(transfer)
with Image.open(transfer) as img:
text = pytesseract.image_to_string(img)
with open('./%s/%s-tess4.txt' % (outputdir, fname), 'w+') as f:
f.write(text)
This does not seem to work to move a PIL image into Wand, however.
add a comment |
To go from Wand to PIL, you can use io.BytesIO
:
with io.BytesIO() as transfer:
with WandImage(filename=fpath) as img:
img.unsharp_mask(radius=2, sigma=0, amount=75, threshold=2)
img.level(black=150/255, white=202/255, gamma=2.1)
img.save(transfer)
with Image.open(transfer) as img:
text = pytesseract.image_to_string(img)
with open('./%s/%s-tess4.txt' % (outputdir, fname), 'w+') as f:
f.write(text)
This does not seem to work to move a PIL image into Wand, however.
add a comment |
To go from Wand to PIL, you can use io.BytesIO
:
with io.BytesIO() as transfer:
with WandImage(filename=fpath) as img:
img.unsharp_mask(radius=2, sigma=0, amount=75, threshold=2)
img.level(black=150/255, white=202/255, gamma=2.1)
img.save(transfer)
with Image.open(transfer) as img:
text = pytesseract.image_to_string(img)
with open('./%s/%s-tess4.txt' % (outputdir, fname), 'w+') as f:
f.write(text)
This does not seem to work to move a PIL image into Wand, however.
To go from Wand to PIL, you can use io.BytesIO
:
with io.BytesIO() as transfer:
with WandImage(filename=fpath) as img:
img.unsharp_mask(radius=2, sigma=0, amount=75, threshold=2)
img.level(black=150/255, white=202/255, gamma=2.1)
img.save(transfer)
with Image.open(transfer) as img:
text = pytesseract.image_to_string(img)
with open('./%s/%s-tess4.txt' % (outputdir, fname), 'w+') as f:
f.write(text)
This does not seem to work to move a PIL image into Wand, however.
answered Nov 21 '18 at 17:44
futuraprime
3,05042336
3,05042336
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53417573%2fhow-do-you-pass-an-image-from-wand-to-pillow%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
Instead of a temp file - can you save it to a
BytesIO
object instead and and pass that as the input to pillow?– Jon Clements♦
Nov 21 '18 at 17:27
I tried that, but I keep getting "BlobError: zero-length blob not permitted `' @ error/blob.c/BlobToImage/348"
– futuraprime
Nov 21 '18 at 17:33
Can you show the code you're using for that in your question then? (eg - creating the BytesIO - saving to it, then trying to read from it?)
– Jon Clements♦
Nov 21 '18 at 17:34
Actually, this does work. It fails the other way, which I'd like to do but don't need; that code is:
In [110]: with io.BytesIO() as output: ⁋ region.save(output, format=i.format) ⁋ wimg = WandImage(blob=output)
– futuraprime
Nov 21 '18 at 17:35
You've already solved it then?
– Jon Clements♦
Nov 21 '18 at 17:38