Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV and Python to build a picture thumbnail server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use OpenCV and Python to build a picture thumbnail server, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Application process tree, initializing 10 Python worker processes (Worker) for processing pictures in the default Poolboy

First install the toolkits required by OpenCV

Ruby-e "$(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Brew install python

Brew tap homebrew/science

Brew install opencv

Sudo pip install numpy

Sudo pip install matplotlib

Use the Python version of Homebrew instead of Python that comes with the Mac OS X system

Alias python='/usr/local/bin/python'

Create an Elixir project

? Mix new opencv_thumbnail_server-sup

* creating README.md

* creating .gitignore

* creating mix.exs

* creating config

* creating config/config.exs

* creating lib

* creating lib/opencv_thumbnail_server.ex

* creating test

* creating test/test_helper.exs

* creating test/opencv_thumbnail_server_test.exs

Your Mix project was created successfully.

You can use "mix" to compile it, test it, and more:

Cd opencv_thumbnail_server

Mix test

Run "mix help" for more commands.

Elixir module

Require Logger

Defmodule OpencvThumbnailServer do

Use Application

Def start (_ type, _ args) do

Logger.info "Start opencv thumbnail server"

OpencvThumbnailServer.Supervisor.start_link ()

End

End

Defmodule OpencvThumbnailServer.Supervisor do

Use Supervisor

@ config Application.get_env: opencv_thumbnail_server,: settings

Def start_link () do

Supervisor.start_link (_ _ MODULE__, [], name: {: global,__MODULE__})

End

Def init ([]) do

Pool_options = @ config [: poolboy]

{_, name} = pool_options [: name]

Children = [

: poolboy.child_spec (name, pool_options, @ config [: module_name])

]

Supervise (children, strategy:: one_for_all, max_restarts: 1000, max_seconds: 3600)

End

End

Defmodule OpencvThumbnailServer.Worker do

Use GenServer

@ config Application.get_env (: opencv_thumbnail_server,: settings)

Def start_link (python_module) do

GenServer.start_link (_ _ MODULE__, python_module, [])

End

Def call_python (worker, function, args) do

GenServer.call (worker, {: call_python, function, args}, 10000)

End

Def init (python_module) do

IO.puts "Start worker"

{: ok, pid} =: python.start_link ([

{: python_path, @ config [: python_path]}

{: python, @ config [: python]}

])

State = {python_module, pid}

{: ok, state}

End

Def handle_call ({: call_python, function, args}, _ from, state) do

{module, pid} = state

Result =: python.call (pid, module, function, args)

Reply = {: ok, result}

{: reply, reply, state}

End

Def handle_call (_ request, _ from, state) do

{: stop,: error,: bad_call, state}

End

Def handle_info (_ msg, {module,py_pid}) do

{: stop,: error, {module,py_pid}}

End

Def terminate (_ reason, {_, py_pid}) do

: python.stop (py_pid)

: ok

End

End

Image processing.

Get width and height

#-*-coding: utf-8-*-

Import urllib2 as urllib

Import numpy as np

Import cv2

Def load_image_url (url):

Resp = urllib.urlopen (url)

Buf = resp.read ()

Return buf

Def load_image_file (filename):

Image = cv2.imdecode (filename, cv2.IMREAD_COLOR)

Return image

Def get_photo_sizes ():

Return [

[160, 160]

[320, 320]

[640, 640]

[1060, 1060]

[1280, 1280]

]

Def show (buf):

# print buf

# x = cv2.imdecode (image, cv2.IMREAD_COLOR)

# d = cv2.cvtColor (c, cv2.COLOR_RGB2BGR)

Np_ndarray = np.fromstring (buf, dtype=np.uint8)

X = cv2.imdecode (np_ndarray, cv2.IMREAD_UNCHANGED)

Return cv2.imshow ('NBA Image', x)

Def write (buf):

Nparray = np.fromstring (buf, dtype=np.uint8)

Img = cv2.imdecode (nparray, cv2.IMREAD_UNCHANGED)

Return cv2.imwrite ('/ tmp/imwrite.png', img)

# def get_dimension ():

# url = 'http://img1.gtimg.com/16/1601/160106/16010642_1200x1000_0.jpg'

# resp = urllib.urlopen (url)

# buf = resp.read ()

# x = np.fromstring (buf, dtype=np.uint8)

# img = cv2.imdecode (x, cv2.IMREAD_UNCHANGED)

# # height = np.size (img, 0)

# # width = np.size (img, 1)

# height, width = image.shape [: 2]

# return (width, height)

Def get_dimension (buffer):

# convert the original binary image data to NpArray

Nparray = np.fromstring (buffer, dtype=np.uint8)

# convert nparray to opencv image format

Image = cv2.imdecode (nparray, cv2.IMREAD_UNCHANGED)

Height, width = image.shape [: 2]

Return (width, height)

Def convert_color ():

Url = 'http://ww3.sinaimg.cn/mw690/6941baebgw1epzcuv9vmxj20me0hy0u1.jpg'

Resp = urllib.urlopen (url)

Buf = resp.read ()

X = np.fromstring (buf, dtype=np.uint8)

Img = cv2.imdecode (x, cv2.IMREAD_UNCHANGED)

If _ _ name__ = ='_ _ main__':

Get_dimension ()

Transfer binary data between Erlang and Python

The mapping between the binary () data type of Erlang and Python. The binary data type is str () in Python 2.x and bytes () in Python 3.x.

Buf = resp.read (), where the variable buf is of type

At Elixir, we see the following values

Python

{: ok,}

Call the Python function

{: ok, data} = OpencvThumbnailServer.Api.load_image_url ("https://segmentfault.com/img/bVwhAW")"

OpencvThumbnailServer.Api.get_dimension (data)

{: ok, {800, 431}}

Create a Python module

The previous Python image processing module can be organized into a project for separate maintenance. Here we use the tool cookiecutter to create a basic Python project skeleton, which is used to implement the function of thumbnails.

Cookiecutter can be installed in many ways, including pip, easy_install, conda, brew

Pip install cookiecutter

Easy_install cookiecutter

Conda install-c https://conda.binstar.org/pydanny cookiecutter

Brew install cookiecutter (Mac OS X)

Directory structure

? Opencv_thumbnail git: (master) tree

.

├── AUTHORS.rst

├── CONTRIBUTING.rst

├── HISTORY.rst

├── LICENSE

├── MANIFEST.in

├── Makefile

├── README.rst

├── build

│ ├── bdist.macosx-10.11-x86_64

│ └── lib

│ └── opencv_thumbnail

│ ├── _ _ init__.py

│ └── opencv_thumbnail.py

├── dist

│ └── opencv_thumbnail-0.1.0-py2.7.egg

├── docs

│ ├── Makefile

│ ├── authors.rst

│ ├── conf.py

│ ├── contributing.rst

│ ├── history.rst

│ ├── index.rst

│ ├── installation.rst

│ ├── make.bat

│ ├── readme.rst

│ └── usage.rst

├── opencv_thumbnail

│ ├── _ _ init__.py

│ ├── _ _ init__.pyc

│ ├── opencv_thumbnail.py

│ └── opencv_thumbnail.pyc

├── opencv_thumbnail.egg-info

│ ├── PKG-INFO

│ ├── SOURCES.txt

│ ├── dependency_links.txt

│ ├── not-zip-safe

│ └── top_level.txt

├── requirements_dev.txt

├── setup.cfg

├── setup.py

├── tests

│ ├── _ _ init__.py

│ └── test_opencv_thumbnail.py

├── tox.ini

└── travis_pypi_setup.py

9 directories, 36 files

API implementation

To call, you need to take a worker process from the Poolboy pool and call the call_python of the worker process. After the process is finished, it will be returned to the Poolboy process pool. It is encapsulated here to simplify the call to Xiamen forklift Rental Company.

Defmodule OpencvThumbnailServer.Api do

Alias OpencvThumbnailServer.Worker

Def get_dimension (data) do

Worker =: poolboy.checkout (: opencv_thumbnail_server_pool)

{w, h} = Worker.call_python (worker,: get_dimension, [data])

: poolboy.checkin (: opencv_thumbnail_server_pool, worker)

{w, h}

End

Def load_image_url (url) do

Worker =: poolboy.checkout (: opencv_thumbnail_server_pool)

Image_bin = Worker.call_python (worker,: load_image_url, [url])

: poolboy.checkin (: opencv_thumbnail_server_pool, worker)

Image_bin

End

End

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report