Moving crops within the same image - python

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)

Related

Python PIL to extract number from image

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…)

Using python, how can i draw objects on image and move, find it

I'm trying to make a program which can draw some objects on an image
the features what i need are
can draw vector(or bitmap) objects (eg. dots, lines..) on pre-existing image file (especially jpg format)
can keep track that objects and find by id or x,y coordinates
can move/modify/remove that objects
please recommend some nice library or code samples for these features.
(I'm trying with WxPython.PseudoDC, but I think it is not what i'm looking for)
thanks.
PIL is the library for you. You can use ImageDraw class. There is no integration between PIL and wxPython, so you should convert from one Image class to another, but that's not a problem.
For interactive graphical programming, look at pyprocessing.
how about guiqwt,
Here is an example:
http://packages.python.org/guiqwt/examples.html#image-plot-tools

PIL - empty PSD image

I'm trying to load this PSD image with Python Imaging Library.
http://www.2shared.com/photo/JjSY7dN-/BG1.html
I'm not very familiar with layered images. Can someone check to see what's the issue? The image appears to be completely transparent. Opening it in my image editor I noticed that the only layer in the image was hidden, I could unhide it to see the colors.
When I load the image with PIL, I get the same issue, but it seems that PIL consolidates the layers into one and I can't do the same as in my image editor. Or maybe there's something I don't know.
PIL provides only very rudimentary hooks into the PSD format. It doesn't support writing or much in the way of manipulation. Your image layer is not shown, so it results in a transparent image. You'll need something more advanced to modify the PSD.
Here's all there is about the PSD format in PIL: http://www.pythonware.com/library/pil/handbook/format-psd.htm
Soon to be available here: http://effbot.org/imagingbook/format-psd.htm

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.

How can I use PIL to crop a select area based on face detection?

Hi I am wanting to use the python imaging library to crop images to a specific size for a website. I have a problem, these images are meant to show people's faces so I need to automatically crop based on them.
I know face detection is a difficult concept so I'm thinking of using the face.com API http://developers.face.com/tools/#faces/detect which is fine for what I want to do.
I'm just a little stuck on how I would use this data to crop a select area based on the majority of faces.
Can anybody help?
Joe
There is a library for python that have a concept of smart-cropping that among other options, can use face detection to do a smarter cropping.
It uses opencv under the hood, but you are isolated from it.
https://github.com/globocom/thumbor
If you have some rectangle that you want to excise from an image, here's what I might try first:
(optional) If the image is large, do a rough square crop centered on the face with dimensions sqrt(2) larger than the longer edge (if rectangular). Worst-case (45° rotation), it will still grab everything important.
Rotate based on the face orientation (something like rough_crop.rotate(math.degrees(math.atan(ydiff/xdiff)), trig is fun)
Do a final crop. If you did the initial crop, the face should be centered, otherwise you'll have to transform (rotate) all your old coordinates to the new image (more trig!).

Categories