# PILasOPENCV
Wrapper for Image functions which are used and called in the manner of the famous PIL or pillow module but work internally only with OpenCV and numpy. Since there is no truetype font support for Python in OpenCV (it exists for the OpenCV C libraries) this module might be useful since it supports all kind of truetype fonts to be integrated in images. It depends on the library freetype-py for this. See below for more details on this. You can also load and save gif images into the numpy format thanks to the gif2numpy and numpy2gif modules. OpenCV does not support the gif format.
This library can be used to migrate old PIL projects to OPENCV or if for some reason PIL or pillow cannot be used on your machine or your platform.
You install this module with
pip install PILasOPENCV
or you download the module and install it with:
python setup.py install
and then you change the import command at the beginning of your project files from
from PIL import Image
or
import PIL.Image as Image
to
import PILasOPENCV as Image
Internally no PIL or Pillow library is used anymore but the opencv and numpy module for doing all the graphical work.
Sample script:
from __future__ import print_function
import PILasOPENCV as Image
# was: from PIL import Image
im = Image.new("RGB", (512, 512), "red")
im.show()
testfile = "lena.jpg"
im = Image.open(testfile)
print (type(im))
# JPEG (512, 512) RGB
im.save("lena.bmp")
im.show()
small = im.copy()
thsize = (128, 128)
small.thumbnail(thsize)
small.show()
box = (100, 100, 400, 400)
region = im.crop(box)
print("region",region.format, region.size, region.mode)
# region = region.transpose(Image.ROTATE_180)
region = region.transpose(Image.ROTATE_180)
region.show()
im.paste(region, box)
im.show()
# Export and import CV2/Numpy images
You can export and import cv2/numpy images from and to an Image instance with the commands getim() and setim():
from __future__ import print_function
import PILasOPENCV as Image
import cv2
im = Image.open("lena.jpg")
numpy_image = im.getim()
print(type(numpy_image), numpy_image.shape)
cv2.imshow("numpy_image", numpy_image)
cv2.waitKey(0)
# import numpy image
new_im = Image.new("RGB", (512, 512), "black")
new_im.setim(numpy_image)
new_im.show()
# Usage of truetype fonts in PILasOPENCV
PILasOPENCV supports the use of truetype fonts with Python. The python module freetype-py needs to be installed for this. To import and use truetype fonts you can do the following:
from __future__ import print_function
import PILasOPENCV as Image
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFont
import cv2
# was: from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("arial.ttf", 18)
print(font)
im = Image.open("lena.jpg")
draw = ImageDraw.Draw(im)
text = "Lena's image"
draw.text((249,455), text, font=font, fill=(0, 0, 0))
print(ImageFont.getsize(text, font))
mask = ImageFont.getmask(text, font)
# in PIL this code is written differently:
# print(font.getsize(text))
# mask = font.getmask(text)
print(type(mask))
cv2.imshow("mask", mask)
im.show()
# Attention:
This is a quite stable development version. Still use with care. Testing has been done to it and tests can be found in the tests directory.
# TO DO:
The most used classes and methods like ImageMode, ImageColor, ImageChops are implemented but have not been fully tested. Image, ImageFont, ImageDraw, ImageGrab and ImageFilter have been tested with several testcases.
Some functions/methods of these classes are missing and are not implemented though.
If you want to import them, import them with:
import PILasOPENCV as ImageMode
import PILasOPENCV as ImageColor
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFilter
import PILasOPENCV as ImageChops
import PILasOPENCV as ImageFont
import PILasOPENCV as ImageGrab
The PIL classes ImageEnhance, ImageFile, ImageFileIO, ImageMath, ImageOps, ImagePath, ImageQt, ImageSequence, ImageStat, ImageTk, ImageWin, ImageGL have not been implemented.
If you want to use the methods getsize and getmask from ImageFont you have to use them differently:
from __future__ import print_function
import PILasOPENCV as Image
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFont
import cv2
# from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("ARIAL.ttf".lower(), 18)
im = Image.open("lena.jpg")
draw = ImageDraw.Draw(im)
text = "Lena's image"
draw.text((249,455), text, font=font, fill=(0, 0, 0))
# in PIL:
# print(font.getsize(text))
# mask = font.getmask(text)
print(ImageFont.getsize(text, font))
mask = ImageFont.getmask(text, font)
print(type(mask))
cv2.imshow("mask", mask)
im.show()
If you want to fork this project, feel free to do so. Give me a message in case you are forking and improving the code.
abunkahle@t-online.de
# Dependencies:
You need to have numpy, opencv, freetype, mss, gif2numpy and numpy2gif installed to run the module completely.
Install it with
pip install numpy opencv-python freetype-py mss gif2numpy numpy2gif
# Version history:
2.7: Bugfix when drawing text and lines or other draw objects the lines were not drawn, fixed
2.6: Bugfix for method show: Old windows were not deleted so it came to display errors, fixed
2.5: Bugfixes for coordinates which were given as float instead of integers when drawing polygons, texts, lines, points, rectangles; bugfix for composite when alphamask and images had not the same amount of channels; bugfix in floodfill when value was given as single integer
2.4: Caught several exceptions in case dependencies modules are not installed you can still work with the basic functions, ImageDraw method bitmap implemented, ImageChops method screen implemented, saves now single or multiple frames in gif files
2.3: Updated the module for gif2numpy Version 1.2
2.2: Bugfix for Python3 on file objects, multiple frames from gifs can be loaded now and can be retrieved with seek(frame)
2.1: though OpenCV does not support gif images, PILasOPENCV now can load gif images by courtesy of the library gif2numpy
2.0: disabled ImageGrab.grabclipboard() in case it throws exceptions which happens e.g. on Ubuntu/Linux
1.9: disabled ImageGrab.grabclipboard() which throw exceptions on some platforms
1.8: ImageGrab.grab() and ImageGrab.grabclipboard() implemented with dependency on mss
1.7: fixed fromarray
1.6: fixed frombytes, getdata, putdata and caught exception in case freetype-py is not installed or dll is missing
# Licence
MIT
Raw data
{
"_id": null,
"home_page": "https://github.com/bunkahle/PILasOPENCV",
"name": "PILasOPENCV",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "PIL OPENCV wrapper",
"author": "Andreas Bunkahle",
"author_email": "abunkahle@t-online.de",
"download_url": "https://files.pythonhosted.org/packages/11/2f/14ac5963e3f9b177a1a726230466b9149efcea37f00f51b745d4c192ee13/PILasOPENCV-2.7.zip",
"platform": "any",
"description": "# PILasOPENCV\nWrapper for Image functions which are used and called in the manner of the famous PIL or pillow module but work internally only with OpenCV and numpy. Since there is no truetype font support for Python in OpenCV (it exists for the OpenCV C libraries) this module might be useful since it supports all kind of truetype fonts to be integrated in images. It depends on the library freetype-py for this. See below for more details on this. You can also load and save gif images into the numpy format thanks to the gif2numpy and numpy2gif modules. OpenCV does not support the gif format.\n\nThis library can be used to migrate old PIL projects to OPENCV or if for some reason PIL or pillow cannot be used on your machine or your platform.\n\nYou install this module with\n\n pip install PILasOPENCV\n \nor you download the module and install it with:\n\n python setup.py install\n \nand then you change the import command at the beginning of your project files from\n\n from PIL import Image\n \nor \n\n import PIL.Image as Image\n \nto\n\n import PILasOPENCV as Image\n \nInternally no PIL or Pillow library is used anymore but the opencv and numpy module for doing all the graphical work.\n\nSample script:\n\n from __future__ import print_function\n\t\n import PILasOPENCV as Image\n\t\n # was: from PIL import Image\n\n im = Image.new(\"RGB\", (512, 512), \"red\")\n\t\n im.show()\n\n testfile = \"lena.jpg\"\n\t\n im = Image.open(testfile)\n\t\n print (type(im))\n\t\n # JPEG (512, 512) RGB\n\t\n im.save(\"lena.bmp\")\n\t\n im.show()\n\t\n small = im.copy()\n\t\n thsize = (128, 128)\n\t\n small.thumbnail(thsize)\n\t\n small.show()\n\t\n box = (100, 100, 400, 400)\n\t\n region = im.crop(box)\n\t\n print(\"region\",region.format, region.size, region.mode)\n\t\n # region = region.transpose(Image.ROTATE_180)\n\t\n region = region.transpose(Image.ROTATE_180)\n\t\n region.show()\n\t\n im.paste(region, box)\n\t\n im.show()\n \n# Export and import CV2/Numpy images\nYou can export and import cv2/numpy images from and to an Image instance with the commands getim() and setim():\n \n from __future__ import print_function\n\t\n import PILasOPENCV as Image\n\t\n import cv2\n\t\n im = Image.open(\"lena.jpg\")\n\t\n numpy_image = im.getim()\n\t\n print(type(numpy_image), numpy_image.shape)\n\t\n cv2.imshow(\"numpy_image\", numpy_image)\n\t\n cv2.waitKey(0)\n\t\n # import numpy image\n\t\n new_im = Image.new(\"RGB\", (512, 512), \"black\")\n\t\n new_im.setim(numpy_image)\n\t\n new_im.show()\n \n# Usage of truetype fonts in PILasOPENCV\nPILasOPENCV supports the use of truetype fonts with Python. The python module freetype-py needs to be installed for this. To import and use truetype fonts you can do the following:\n\n\tfrom __future__ import print_function\n\t\n\timport PILasOPENCV as Image\n\t\n\timport PILasOPENCV as ImageDraw\n\t\n\timport PILasOPENCV as ImageFont\n\t\n\timport cv2\n\t\n\t# was: from PIL import Image, ImageDraw, ImageFont\n\n\tfont = ImageFont.truetype(\"arial.ttf\", 18)\n\t\n\tprint(font)\n\t\n\tim = Image.open(\"lena.jpg\")\n\t\n\tdraw = ImageDraw.Draw(im)\n\t\n\ttext = \"Lena's image\"\n\t\n\tdraw.text((249,455), text, font=font, fill=(0, 0, 0))\n\t\n\tprint(ImageFont.getsize(text, font))\n\t\n\tmask = ImageFont.getmask(text, font)\n\t\n\t# in PIL this code is written differently:\n\t\n\t# print(font.getsize(text))\n\t\n\t# mask = font.getmask(text)\n\t\n\tprint(type(mask))\n\t\n\tcv2.imshow(\"mask\", mask)\n\t\n\tim.show()\n \n# Attention:\nThis is a quite stable development version. Still use with care. Testing has been done to it and tests can be found in the tests directory.\n\n# TO DO:\nThe most used classes and methods like ImageMode, ImageColor, ImageChops are implemented but have not been fully tested. Image, ImageFont, ImageDraw, ImageGrab and ImageFilter have been tested with several testcases.\nSome functions/methods of these classes are missing and are not implemented though. \nIf you want to import them, import them with:\n\n import PILasOPENCV as ImageMode\n\t\n import PILasOPENCV as ImageColor\n\t\n import PILasOPENCV as ImageDraw\n\t\n import PILasOPENCV as ImageFilter\n\t\n import PILasOPENCV as ImageChops\n\t\n import PILasOPENCV as ImageFont\n\t\n import PILasOPENCV as ImageGrab\n \nThe PIL classes ImageEnhance, ImageFile, ImageFileIO, ImageMath, ImageOps, ImagePath, ImageQt, ImageSequence, ImageStat, ImageTk, ImageWin, ImageGL have not been implemented.\n \nIf you want to use the methods getsize and getmask from ImageFont you have to use them differently:\n\n\tfrom __future__ import print_function\n\t\n\timport PILasOPENCV as Image\n\t\n\timport PILasOPENCV as ImageDraw\n\t\n\timport PILasOPENCV as ImageFont\n\t\n\timport cv2\n\t\n\t# from PIL import Image, ImageDraw, ImageFont\n \n\tfont = ImageFont.truetype(\"ARIAL.ttf\".lower(), 18)\n\t\n\tim = Image.open(\"lena.jpg\")\n\t\n\tdraw = ImageDraw.Draw(im)\n\t\n\ttext = \"Lena's image\"\n\t\n\tdraw.text((249,455), text, font=font, fill=(0, 0, 0))\n\t\n\t# in PIL:\n\t\n\t# print(font.getsize(text))\n\t\n\t# mask = font.getmask(text)\n\t\n\tprint(ImageFont.getsize(text, font))\n\t\n\tmask = ImageFont.getmask(text, font)\n\t\n\tprint(type(mask))\n\t\n\tcv2.imshow(\"mask\", mask)\n\t\n\tim.show()\n\nIf you want to fork this project, feel free to do so. Give me a message in case you are forking and improving the code.\nabunkahle@t-online.de\n\n# Dependencies:\nYou need to have numpy, opencv, freetype, mss, gif2numpy and numpy2gif installed to run the module completely.\nInstall it with \n\n pip install numpy opencv-python freetype-py mss gif2numpy numpy2gif\n\n# Version history:\n\n2.7: Bugfix when drawing text and lines or other draw objects the lines were not drawn, fixed\n\n2.6: Bugfix for method show: Old windows were not deleted so it came to display errors, fixed\n\n2.5: Bugfixes for coordinates which were given as float instead of integers when drawing polygons, texts, lines, points, rectangles; bugfix for composite when alphamask and images had not the same amount of channels; bugfix in floodfill when value was given as single integer\n\n2.4: Caught several exceptions in case dependencies modules are not installed you can still work with the basic functions, ImageDraw method bitmap implemented, ImageChops method screen implemented, saves now single or multiple frames in gif files \n\n2.3: Updated the module for gif2numpy Version 1.2\n\n2.2: Bugfix for Python3 on file objects, multiple frames from gifs can be loaded now and can be retrieved with seek(frame)\n\n2.1: though OpenCV does not support gif images, PILasOPENCV now can load gif images by courtesy of the library gif2numpy\n\n2.0: disabled ImageGrab.grabclipboard() in case it throws exceptions which happens e.g. on Ubuntu/Linux\n\n1.9: disabled ImageGrab.grabclipboard() which throw exceptions on some platforms\n\n1.8: ImageGrab.grab() and ImageGrab.grabclipboard() implemented with dependency on mss\n\n1.7: fixed fromarray\n\n1.6: fixed frombytes, getdata, putdata and caught exception in case freetype-py is not installed or dll is missing \n\n# Licence\nMIT",
"bugtrack_url": null,
"license": "MIT",
"summary": "Wrapper for Image functions which are used and called in the manner of the famous PIL/Pillow module but work internally with OpenCV.",
"version": "2.7",
"split_keywords": [
"pil",
"opencv",
"wrapper"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "4713053f09ac5aab435b52e4d9717057",
"sha256": "4bb2701497117d54db836cf21bfcce76b9fc15806216ae3b0adc3e7e03ff2ce0"
},
"downloads": -1,
"filename": "PILasOPENCV-2.7.zip",
"has_sig": false,
"md5_digest": "4713053f09ac5aab435b52e4d9717057",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 36915,
"upload_time": "2019-07-11T09:04:14",
"upload_time_iso_8601": "2019-07-11T09:04:14.412277Z",
"url": "https://files.pythonhosted.org/packages/11/2f/14ac5963e3f9b177a1a726230466b9149efcea37f00f51b745d4c192ee13/PILasOPENCV-2.7.zip",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-07-11 09:04:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "bunkahle",
"github_project": "PILasOPENCV",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pilasopencv"
}