Python PIL to extract number from image - python

I have an image like this one:
and I would like to have a black number written on white so that I can use an OCR to recognise it. How could I achieve that in Python?
Many thanks,
John.

You don't need to manipulate the image for OCR. For example, you could just use pytesser:
from PIL import Image
from pytesser import *
im = Image.open('wjNL6.jpg')
text = image_to_string(im)
print text
Output:
0

If you just want to turn a white-on-black image to black-on-white, that's trivial; it's just invert:
from PIL import Image, ImageOps
img = Image.open('zero.jpg')
inverted = ImageOps.invert(img)
inverted.save('invzero.png')
If you also want to do some basic processing like increasing the contrast, see the other functions in the ImageOps module, like autocontrast. They're all pretty easy to use, but if you get stuck, you can always ask a new question. For more complex enhancements, look around the rest of PIL. ImageEnhance can be used to sharpen an image, ImageFilter can do edge detection and unsharp masking; etc. You may also want to change the format to greyscale (L8), or even black and white (L1); that's all in the Image.convert method.
Of course you have to know what processing you want to do. One thing you might want to try is playing around with the image in Photoshop or GIMP and keeping track of what operations you do, then looking for how to implement those operations in PIL. (It might be simpler to just use gimp-fu scripting in the first place instead of trying to use PIL…)

Related

write antialiased text with custom font on transparent image

so, as the topic says, I want to write some text on an transparent image.
Or, to be more specific, I want to write on multiple specific positions different text. The point is, I want to use a custom font (coolvetica) and (and there is my problem) I want it to be anti-aliased. unfortunately, PIL does not support font-anti-aliasing, as you might already know, but also see here:
PIL example image.
So as you see, PIL has not only an issue with the coordinates, but also does not really support font-anti-aliasing and adds some irritating black-ish border to the text. It is not very usable for me.
So after some searching, I found Python Wand. The output is a lot better, but the antialiasing leads not to the result I want: Wand example image. The curves are not as smooth as I want and there are some weird "pimples" like the dot at the end of the "r".
My code to use wand is this:
with Drawing() as draw:
with wandimage(filename="Skispringen_Score.png") as img:
draw.font_family = 'coolvetica.ttf'
draw.text_antialias = True
draw.fill_color = (Color("rgba(255, 255, 255, 255)"))
for postionName, scorePosition in scoreContent.items():
if isinstance(currentScoreText[postionName], int) and currentScoreText[postionName] > 9:
center = 2
else:
center = 0
draw.font_size = fontsize[scorePosition["size"]]
draw.text(scorePosition["x"] - center, scorePosition["y"], str(currentScoreText[postionName]))
draw(img)
img.save(filename='wand-image.png')
And here is an example of what the text should actually look like: PHP example image.
This image was generated with php and the text looks as smooth as I want to.
I also tried to use cairo, but the documentation is really not that good (especially examples are rare) and I just don't even know how to write any text with it nor how to set an custom font.
The actual output image has an resolution of 1280x720 and is - besides of the text - completely transparent, the image is just an overlay for an videostream.
Do you have any idea how to get a nice looking text onto the image with python?
You could try pycairo. Here are some code examples https://github.com/pygobject/pycairo/tree/master/examples
The reference docs for cairo are for C.
If you want even better results then you could render the image at 4 times the size and
downsample it to the desired size.
You can also try some filters like a gaussian blur.
For pixel perfect images you will have to use the same rendering engine that your php function is using.

Using Python and Tesseract OCR to solve Captcha

I am not planning to spam, and besides Google has made captcha obsolete with reCaptcha. I am doing this as a project to learn more about OCR and eventually maybe neural networks.
SO I have an image from a Captcha, I have been able to make modest progress, but the documentation on tesseract isn't exactly well documented. Here is the code I have so far and the results are bellow it.
from selenium import webdriver
from selenium.webdriver.common import keys
import time
import random
import pytesseract
from pytesseract import image_to_string
from PIL import Image, ImageEnhance, ImageFilter
def ParsePic():
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
im = Image.open("path\\screenshot.png")
im = im.filter(ImageFilter.CONTOUR)
im = im.filter(ImageFilter.DETAIL)
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(4)
im = im.convert('L')
im.save('temp10.png')
text = image_to_string(Image.open('temp10.png'))
print(text)
Original Image
Output
I understand that Captcha was made specifically to defeat OCR, but I read that it is no longer the case, and Im interested in learning how it was done.
My question is, how do I make the background the same color, so the text becomes easily readable?
Late answer but anyway...
You are doing edge detection but there are, obviously, to many in this image so this will not work.
You will have to do some thing with the colors.
I don't know if this is true for every of your captchas but you can just use contrast.
You can test this by open up your original with paint (or any other image edit program) and save the image as "monochrom" (black and white only, NOT grayscale)
result:
without any other editing! (Even the Questionmark is gone)
This would be ready to OCR right away.
Maybe your other images are not this easy, but color/contrast is the way to go for you. If you need ideas on how you can use color, contrast and other things to solve captachs, you can take a look on harder examples and how I solved them here: https://github.com/cracker0dks/CaptchaSolver
cheers

How can i transform Image1 to Image2 using matplotlib.pyplot or another library in python

How can i transform Image1 to Image2 using matplotlib.pyplot or another library in Python?
Image 1:
Image 2:
(This image turned out to be confidential,i removed it because i can't delete the post. Sorry for the inconvenience)
Any help is appreciated.
Have a look at the Python Imageing Library (which also has python bindings, by the way), especially the ImageFilter module.
But tools like ImageMagick or one of the built-in filters in Gimp might be more suitable for experimenting.
Is it the experimental data and already filtered from image 1 to image 2 by someone else? I wonder whether you have the point spread function along with the raw image 1?

Moving crops within the same image

Tried looking in the PIL module but couldn't find this covered.
I have an image, and I need to take a cropped piece of the image and move it to a different area within the same image. Simple enough, but I will need to do this for thousands of images, so I will do batch image processing.
PIL seems too basic for this, any other libraries?
Actually, PIL looks like it can do it very well, if you want to copy and paste rectangular areas.
Check the documentation on the crop and paste methods on image, on the documentation here:
http://effbot.org/imagingbook/image.htm
If you need non-rectangular areas, though, you will have to resort to more sophisticated handling, but that does not preclude you from doing it with PIL.
Yes, check OpenCV.
How to crop images with opencv in python
The Python wrapper for the GD library should do what you need:
copyTo(image[, (dx,dy)[, (sx,sy)[, (w,h)]]])
copy from (sx,sy), width sw and height sh to destination image (dx,dy)

Adding multiple images to one canvas in Python

I want to load a number of images from harddrive and place them on a larger white background. And I want to do it in Python. I am wondering what is the best way of doing that. I am using a windows machine and I can use any library I want. Any pointer to a webpage or a sample code that can point me to a good direction would be appreciated.
Something like this:
A very popular image processing library for Python is PIL. The official PIL tutorial might be useful, especially the section about "Cutting, Pasting and Merging Images".
PIL isn't enough. Try also with PIL the aggdraw library.
But aggdraw also isn't enough. It works poorly with transparency. I mean 0.5-1 gray pixel around opaque object over the transpparent area.

Categories