Python:实现图片裁剪的两种方式——Pillow和OpenCV

原文:https://blog.csdn.net/hfutdog/article/details/82351549

在这篇文章里我们聊一下Python实现图片裁剪的两种方式,一种利用了Pillow,还有一种利用了OpenCV。两种方式都需要简单的几行代码,这可能也就是现在Python那么流行的原因吧。

首先,我们有一张原始图片,如下图所示:

 

然后,我们利用OpenCV对其进行裁剪,代码如下所示:

import cv2img = cv2.imread("./data/cut/thor.jpg")
print(img.shape)
cropped = img[0:128, 0:512] # 裁剪坐标为[y0:y1, x0:x1]
cv2.imwrite("./data/cut/cv_cut_thor.jpg", cropped)

 


这里,我们先用imread方法读取待裁剪的图片,然后查看它的shape,shape的输出是(1080, 1920, 3),输出的顺序的是高度、宽度、通道数。之后我们利用数组切片的方式获取需要裁剪的图片范围。这里需要注意的是切片给出的坐标为需要裁剪的图片在原图片上的坐标,顺序为[y0:y1, x0:x1],其中原图的左上角是坐标原点。最后我们用cv2.imwrite()方法将裁剪得到的图片保存到本地(第一个参数为图片名,第二参数为需要保存的图片),如图所示:


OpenCV裁剪所得图片

 


接下来,我们看一下使用Pillow如何对图片进行裁剪,代码如下所示:

from PIL import Imageimg = Image.open("./data/cut/thor.jpg")
print(img.size)
cropped = img.crop((0, 0, 512, 128)) # (left, upper, right, lower)
cropped.save("./data/cut/pil_cut_thor.jpg")

 


首先我们使用open方法读取图片,然后查看它的size(这里的size和OpenCV中的shape是类似的),size的输出是(1920, 1080),也就是图片的宽度和高度。之后我们调用crop方法来对图片进行裁剪,crop需要给定一个box参数,box是一个四元组,元组中元素的顺序是需要裁剪得到的图片在原图中的左、上、右、下坐标,即(left, upper, right, lower)。然后,我们使用save方法保存裁剪得到的图片。如下图所示,Pillow可以同样完成OpenCV裁剪图片的工作。


Pillow裁剪所得图片

 

转载于:https://www.cnblogs.com/gcgc/p/11343919.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391735.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第一个应在JavaScript数组的最后

by Thomas Barrasso由Thomas Barrasso 第一个应在JavaScript数组的最后 (The first shall be last with JavaScript arrays) So the last shall be [0], and the first [length — 1].所以最后一个应该是[0] ,第一个[length_1]。 – Adapted from Matthew 20:16–根…

鼠标移动到ul图片会摆动_我们可以从摆动时序分析中学到的三件事

鼠标移动到ul图片会摆动An opportunity for a new kind of analysis of Major League Baseball data may be upon us soon. Here’s how we can prepare.不久之后,我们将有机会对美国职棒大联盟数据进行新的分析。 这是我们准备的方法。 It is tempting to think t…

leetcode 1052. 爱生气的书店老板(滑动窗口)

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。 在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气&#xf…

回到网易后开源APM技术选型与实战

篇幅一:APM基础篇\\1、什么是APM?\\APM,全称:Application Performance Management ,目前市面的系统基本都是参考Google的Dapper(大规模分布式系统的跟踪系统)来做的,翻译传送门《google的Dappe…

持续集成持续部署持续交付_如何开始进行持续集成

持续集成持续部署持续交付Everything you need to know to get started with continuous integration: branching strategies, tests automation, tools and best practices.开始进行持续集成所需的一切:分支策略,测试自动化,工具和最佳实践。…

51nod 1073约瑟夫环

思路传送门 &#xff1a;http://blog.csdn.net/kk303/article/details/9629329 n里面挑选m个 可以递推从n-1里面挑m个 然后n-1里面的x 可以转换成 n里面的x 的公式 x &#xff08;xm&#xff09;%n; #include <bits/stdc.h> using namespace std;int main () {int n,m;s…

如何选择优化算法遗传算法_用遗传算法优化垃圾收集策略

如何选择优化算法遗传算法Genetic Algorithms are a family of optimisation techniques that loosely resemble evolutionary processes in nature. It may be a crude analogy, but if you squint your eyes, Darwin’s Natural Selection does roughly resemble an optimisa…

robot:截图关键字

参考&#xff1a; https://www.cnblogs.com/hong-fithing/p/9656221.html--python https://blog.csdn.net/weixin_43156282/article/details/87350309--robot https://blog.csdn.net/xiongzaiabc/article/details/82912280--截图指定区域 转载于:https://www.cnblogs.com/gcgc/…

leetcode 832. 翻转图像

给定一个二进制矩阵 A&#xff0c;我们想先水平翻转图像&#xff0c;然后反转图像并返回结果。 水平翻转图片就是将图片的每一行都进行翻转&#xff0c;即逆序。例如&#xff0c;水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。 反转图片的意思是图片中的 0 全部被 1 替换&#xff…

SVN服务备份操作步骤

SVN服务备份操作步骤1、准备源服务器和目标服务器源服务器&#xff1a;192.168.1.250目标服务器&#xff1a;192.168.1.251 root/rootroot 2、对目标服务器&#xff08;251&#xff09;装SVN服务器&#xff0c; 脚本如下&#xff1a;yum install subversion 3、创建一个新的仓库…

SpringCloud入门(一)

1. 系统架构演变概述 #mermaid-svg-F8dvnEDl6rEgSP97 .label{font-family:trebuchet ms, verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .label text{fill:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .node rect,#merm…

PullToRefreshListView中嵌套ViewPager滑动冲突的解决

PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部添加到ListView中&#xff0c;发先ViewPager在滑动过程中流畅性太差几乎很难左右滑动。在网上也看了很多大神的介绍&#xff0c;看了ViewP…

神经网络 卷积神经网络_如何愚弄神经网络?

神经网络 卷积神经网络Imagine you’re in the year 2050 and you’re on your way to work in a self-driving car (probably). Suddenly, you realize your car is cruising at 100KMPH on a busy road after passing through a cross lane and you don’t know why.想象一下…

数据特征分析-分布分析

分布分析用于研究数据的分布特征&#xff0c;常用分析方法&#xff1a; 1、极差 2、频率分布 3、分组组距及组数 df pd.DataFrame({编码:[001,002,003,004,005,006,007,008,009,010,011,012,013,014,015],\小区:[A村,B村,C村,D村,E村,A村,B村,C村,D村,E村,A村,B村,C村,D村,E村…

开发工具总结(2)之全面总结Android Studio2.X的填坑指南

前言&#xff1a;好多 Android 开发者都在说Android Studio太坑了&#xff0c;老是出错&#xff0c;导致开发进度变慢&#xff0c;出错了又不知道怎么办&#xff0c;网上去查各种解决方案五花八门&#xff0c;有些可以解决问题&#xff0c;有些就是转来转去的写的很粗糙&#x…

无聊的一天_一人互联网公司背后的无聊技术

无聊的一天Listen Notes is a podcast search engine and database. The technology behind Listen Notes is actually very very boring. No AI, no deep learning, no blockchain. “Any man who must say I am using AI is not using True AI” :)Listen Notes是一个播客搜索…

如何在Pandas中使用Excel文件

From what I have seen so far, CSV seems to be the most popular format to store data among data scientists. And that’s understandable, it gets the job done and it’s a quite simple format; in Python, even without any library, one can build a simple CSV par…

Js实现div随鼠标移动的方法

HTML: <div id"odiv" style" COLOR: #666; padding: 2px 8px; FONT-SIZE: 12px; MARGIN-RIGHT: 5px; position: absolute; background: #fff; display: block; border: 1px solid #666; top: 50px; left: 10px;"> Move_Me</div>第一种&…

leetcode 867. 转置矩阵

给你一个二维整数数组 matrix&#xff0c; 返回 matrix 的 转置矩阵 。 矩阵的 转置 是指将矩阵的主对角线翻转&#xff0c;交换矩阵的行索引与列索引。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 输出&#xff1a;[[1,4,7],[2,5,8],[3,6,9]] …

数据特征分析-对比分析

对比分析是对两个互相联系的指标进行比较。 绝对数比较(相减)&#xff1a;指标在量级上不能差别过大&#xff0c;常用折线图、柱状图 相对数比较(相除)&#xff1a;结构分析、比例分析、空间比较分析、动态对比分析 df pd.DataFrame(np.random.rand(30,2)*1000,columns[A_sale…