Python ecosystem image format conversion problems (recommended)

In the Python ecosystem, the most common image library is PIL-- although that has been superseded by subsequent pillow, pillow but because the API is almost fully inherited the PIL, so we still convention be called PIL. In addition to PIL, more and more programmers accustomed to using openCV to manipulate the image. Further, in the GUI library, there are defined respective image processing mechanism, such as wxPyton, as defined wx.Image class image processing, image display as defined wx.Bitmap class.

FIG PIL tease out the image file read and write, read and write the image file cv2, cv2 PIL objects and objects conversion, PIL objects and objects wx.Image conversion, and a method numpy array rollover images. Mastered these methods, enough to cope with a wide variety of image processing needs.Here Insert Picture Description

  1. PIL to read and write image file

The following code illustrates the read png format image file with PIL, excluding the alpha channel image file dump jpg format.

>>> from PIL import Image
>>> im = Image.open(r'D:\CSDN\Python_Programming.png')
>>> r,g,b,a = im.split()
>>> im = Image.merge("RGB",(r,g,b))
>>> im.save(r'D:\CSDN\Python_Programming.jpg')
  1. cv2 read and write image file

The following code illustrates the format of a read cv2 png image file, an image file dump jpg format.

>>> import cv2
>>> im = cv2.imread(r'D:\CSDN\Python_Programming.png')
>>> cv2.imwrite(r'D:\CSDN\Python_Programming.jpg', im)
True
  1. PIL objects and objects Huzhuan cv2

Object cv2 format, essentially numpy array, which is numpy.ndarray object. As long as the object can do PIL and numpy array conversion, naturally achieve a PIL objects and objects cv2 system conversion.

The following code illustrates the png format with PIL read image file is converted into an image file saved numpy array.

>>> import cv2
>>> from PIL import Image
>>> import numpy as np
>>> im_pil = Image.open(r'D:\CSDN\Python_Programming.png')
>>> im_cv2 = np.array(im_pil)
>>> cv2.imwrite(r'D:\CSDN\Python_Programming.jpg', im_cv2)
True

The following code is read by the image file format png CV2, saved as an image file converted into PIL objects.

>>> import cv2
>>> from PIL import Image
>>> im_cv2 = cv2.imread(r'D:\CSDN\Python_Programming.png')
>>> im_pil = Image.fromarray(im_cv2)
>>> im_pil.save(r'D:\CSDN\Python_Programming.jpg')
  1. PIL objects and objects Huzhuan wx.Image

These are two functions to achieve PIL objects and wx.Image of the object system conversion.

def PilImg2WxImg(pilImg):
  '''PIL的image转化为wxImage'''
  image = wx.EmptyImage(pilImg.size[0],pilImg.size[1])
  image.SetData(pilImg.convert("RGB").tostring())
  image.SetAlphaData(pilImg.convert("RGBA").tostring()[3::4])
  return image
def WxImg2PilImg(wxImg):
  '''wxImage转化为PIL的image'''
  pilImage = Image.new('RGB', (wxImg.GetWidth(), wxImg.GetHeight()))
  pilImage.fromstring(wxImg.GetData())
  if wxImg.HasAlpha():
    pilImage.convert( 'RGBA' )
    wxAlphaStr = wxImg.GetAlphaData()
    pilAlphaImage = Image.fromstring( 'L', (wxImg.GetWidth(), wxImg.GetHeight()), wxAlphaStr )
    pilImage.putalpha( pilAlphaImage )
  return pilImage
  1. Dump image array numpy

The following code, generating a random image is 515x512 pixels.

>>> from PIL import Image
>>> import numpy as np
>>> a = np.random.randint(0,256,((512,512,3)), dtype=np.uint8)
>>> im_pil = Image.fromarray(a)
>>> im_pil.save(r'D:\CSDN\random.jpg')

Here Insert Picture Description
to sum up

The above is a Python ecosystem image format conversion problems Xiaobian to introduce, we want to help
content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of veteran study skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data, the programmer has to explain the timing of Python technology every day, share some learning methods and needs attention nitty grittyHere Insert Picture Description

Published 35 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105029720