How to crop an image using PIL?
There is a crop() method: w, h = yourImage.size yourImage.crop((0, 30, w, h-30)).save(…)
There is a crop() method: w, h = yourImage.size yourImage.crop((0, 30, w, h-30)).save(…)
First, I downloaded a test TIFF image from this page called a_image.tif. Then I opened with PIL like this: >>> from PIL import Image >>> im = Image.open(‘a_image.tif’) >>> im.show() This showed the rainbow image. To convert to a numpy array, it’s as simple as: >>> import numpy >>> imarray = numpy.array(im) We can see … Read more
The PIL version packaged on pypi (by the author) is incompatible with setuptools and thus not easy_installable. People have created easy_installable versions elsewhere. Currently, you need to specify a find-links URL and use pip get a good package: pip install –no-index -f http://dist.plone.org/thirdparty/ -U PIL By using pip install with the –no-index you avoid running … Read more
You need to make the following changes: append a tuple (255, 255, 255, 0) and not a list [255, 255, 255, 0] use img.putdata(newData) This is the working code: from PIL import Image img = Image.open(‘img.png’) img = img.convert(“RGBA”) datas = img.getdata() newData = [] for item in datas: if item[0] == 255 and item[1] … Read more
I agree with almost everything as answered by “unutbu” and Ignacio Vazquez-Abrams, however… EXIF Orientation flag can have a value between 1 and 8 depending on how the camera was held. Portrait photo can be taken with top of the camera on the left, or right edge, landscape photo could be taken upside down. Here … Read more
Quite a busy one-liner, but here it is: First ensure your NumPy array, myarray, is normalised with the max value at 1.0. Apply the colormap directly to myarray. Rescale to the 0-255 range. Convert to integers, using np.uint8(). Use Image.fromarray(). And you’re done: from PIL import Image from matplotlib import cm im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255)) with … Read more
I had a same issue. from PIL import Image instead of import Image fixed the issue
The easiest way to do it is by using masks. Create a black and white mask with any shape you want. And use putalpha to put that shape as an alpha layer: from PIL import Image, ImageOps mask = Image.open(‘mask.png’).convert(‘L’) im = Image.open(‘image.png’) output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) output.save(‘output.png’) Here is the mask … Read more
On Ubuntu, you need to have libfreetype-dev installed before compiling PIL. i.e. $ sudo apt-get install libfreetype6-dev $ sudo -s \# pip uninstall pil \# pip install –no-cache-dir pil PS! Running pip install as sudo will usually install packages to /usr/local/lib on most Ubuntu versions. You may consider to install Pil in a virtual environment … Read more
use this: pil_image = PIL.Image.open(‘Image.jpg’).convert(‘RGB’) open_cv_image = numpy.array(pil_image) # Convert RGB to BGR open_cv_image = open_cv_image[:, :, ::-1].copy()