惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
月光博客
月光博客
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LangChain Blog
博客园 - 聂微东
G
Google Developers Blog
博客园 - Franky

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Read and Write Images with Unicode Paths in OpenCV
2019-09-11 · via jdhao's digital space

The other day, when I was using OpenCV to read some images and print the image shape, I got an error:

‘NoneType’ object has no attribute ‘shape’.

I was confused and do not know why since the image path is valid. It tooks me a while to understand the cause.

The reason is that OpenCV only accepts ASCII characters for image paths when reading and writing images. There is an open PR to add Unicode path support, but it has not been merged. However, there are ways to work around this issue.

First, we can read the image with the help of Numpy fromfile() method, and then use cv2.imdecode() to read the image. Here is a sample code:

import numpy as np
import cv2

# img with unicode path
im_path = "测试目录/test.jpg"
# img is in BGR format if the underlying image is a color image
img = cv2.imdecode(np.fromfile(im_path, dtype=np.uint8),
                   cv2.IMREAD_UNCHANGED)

In the above script, the image is read with numpy and converted to a one-dimensional numpy ndarray of type np.uint8. Then we use imdecode() to decode the ndarray to OpenCV image format. cv2.IMREAD_UNCHANGED is a flag for imdecode1.

Write images with unicode paths#

To write images with unicode paths, we can first encode the image in OpenCV format to one dimension numpy ndarray format. Then we convert this numpy ndarray to image on disk with the tofile() method.

import cv2
import numpy as np

img = cv2.imread("test.jpg", 1)
h, w = img.shape[:2]
im_resize = cv2.resize(img, (w//2, h//2))

im_save_path = "测试目录/small_img.jpg"
# encode the im_resize into the im_buf_arr, which is a one-dimensional ndarray
is_success, im_buf_arr = cv2.imencode(".jpg", im_resize)
im_buf_arr.tofile(im_save_path)

In the above code, cv2.imencode() will encode the image as a one-dimension numpy array and tofile() will save this array into disk.

References#