Python 图片格式的转换和尺寸修改
import cv2
import os
import numpy as np
from PIL import Image
import shutil
import sys image_size=144
#改变之后的图片尺寸 source_path=os.getcwd()+"/image/"#等待转换的图片存放地址
types='png' #转换后的图片格式
target_path=os.getcwd()+"/changepng/"#转换过格式的图片存放地址
final_path=os.getcwd()+"/final/"转换过格式和尺寸的图片存放地址 #如果没有转换后的图片存放文件夹,就创建对应的文件夹
if not os.path.exists(target_path):
os.makedirs(target_path)
if not os.path.exists(final_path):
os.makedirs(final_path) #转变图片格式的函数
def changepng(source_path,types):
files = []
image_list=os.listdir(source_path)
#print(image_list)
files = [os.path.join(source_path,_) for _ in image_list]
for index,jpg in enumerate(files):
if index > 1000:
break
try:
sys.stdout.write('\r>>Converting image %d/100000 ' % (index))
sys.stdout.flush()
im = Image.open(jpg)
png = os.path.splitext(jpg)[0] + "." + types
im.save(png)
shutil.move(png,target_path)
except IOError as e:
print('could not read:',jpg)
print('error:',e)
print('skip it\n')
sys.stdout.write('Convert Over!\n')
sys.stdout.flush() #转化图片尺寸的函数
def changesize(source_path):
image_lists=os.listdir(source_path)
i=0
for file in image_lists:
i=i+1
print(os.getcwd()+"/"+file)
split=os.path.splitext(file)
filename,type=split
image_file = source_path+file
image_source=cv2.imdecode(np.fromfile(image_file,dtype=np.uint8),cv2.IMREAD_UNCHANGED)
image = cv2.resize(image_source, (image_size, image_size))
cv2.imencode('.png',image)[1].tofile(final_path+file) changepng(source_path,types)
changesize(target_path)
Python 图片格式的转换和尺寸修改的更多相关文章
- 【我的Android进阶之旅】推荐一款视频转换GIF图片格式的转换工具(Video to GIF)
一.背景 最近想把一些Android Demo的运行效果图获取下来,但是一直使用真机进行调试,在电脑上不好截取一段gif动画.而之前使用模拟器的时候可以使用 GifCam 工具进行屏幕动画截取.Gif ...
- python 图片格式转换png转jpg,如何利用python给图片添加半透明水印
from PIL import Imageim = Image.open(r'd:\test2.png')r, g, b, a = im.split()im = Image.merge("R ...
- 图片格式转换之ImageMagick
项目中需要实现一些图片文件到TIFF文件的转换,去网上下载了一些第三方软件. 好的软件需要收费,免费的存在各种问题. 自己动手,丰衣足食! 众里寻他千百度,蓦然回首,那人就是ImageMagick. ...
- 使用IMAGEMAGICK的CONVERT工具批量转换图片格式
使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...
- 利用PBFunc在Powerbuilder中进行图片格式转换
利用PBFunc的n_pbfunc_image对象可以方便的进行图片格式的转换与大小转换 支持相互转换的格式有以下几种: FORMAT_BMP //bmp格式FORMAT_GIF //gif格式FO ...
- Python 批量修改图片格式和尺寸
公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大小的软件网上很多,我的同事原来使用的美图看看的批量转换,但是稍微有点麻烦,每次还需要指定要转换的图 ...
- Python批量修改图片格式和尺寸
Python批量修改图片格式和尺寸 备注: 1.导入了PIL库,是处理图片用的,很强大; 2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除. 3.导入 ...
- python 将png图片格式转换生成gif动画
先看知乎上面的一个连接 用Python写过哪些[脑洞大开]的小工具? https://www.zhihu.com/question/33646570/answer/157806339 这个哥们通过爬气 ...
- python base64 编解码,转换成Opencv,PIL.Image图片格式
二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...
随机推荐
- JDK11 | 第四篇 : 增强API
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/O0xntC-JfeSBk-9x2QfwnA 一.简介 JDK 9~11 在语言语法方面有一 ...
- AtCoder Beginner Contest 133 -D — Rain Flows into Dams
(https://atcoder.jp/contests/abc133/tasks/abc133_d) 思路:每座山为2Xi,每个坝为Ai.已知Ai,求出2Xi. 根据已知的X1,则可分别求出X2-n ...
- c# ASP.NET MVC easyui-filebox 图片上传和显示
原文:https://www.cnblogs.com/huatao/p/4727398.html https://www.cnblogs.com/weiweithe/p/4363458.html 表单 ...
- VUE:v-for获取列表前n个数据、中间范围数据、末尾n条数据的方法
说明: 1.开发使用的UI是mintUI, 要求: 1.获取6到13之间的数据:items.slice(6,13) <mt-cell v-for="(item,index) in it ...
- Nodejs 学习笔记 --- 安装与环境配置
一.安装Node.js步骤 1.下载对应自己系统对应的 Node.js 版本,地址:https://nodejs.org/zh-cn/ 2.选安装目录进行安装 3.环境配置 ...
- 你的package包名有问题!
今天在Eclipse中运行我的Java程序中,就弹出了以下消息的窗口: 注意窗口的名字为Java Virtual Machine Launcher . Error : A JNI error has ...
- 查询sql server占用内存的情况
se master go select * from sysperfinfo where counter_name like '%Total Server Memory (KB)%' go
- 三、Windows下用FFmpeg+nginx+rtmp搭建直播环境 实现推流、拉流
一.环境 1.开发环境:windows 2.开发工具:FFmpeg.nginx.nginx-rmtp-module (链接:https://pan.baidu.com/s/119d2GeMzddas_ ...
- [MethodImpl(MethodImplOptions.Synchronized)]、lock(this)与lock(typeof(...))
对于稍微有点经验的.NET开发人员来说,倘若被问及如何保持线程同步,我想很多人都能说好好几种.在众多的线程同步的可选方式中,加锁无疑是最为常用的.如果仅仅是基于方法级别的线程同步,使用System.R ...
- 函数&&变量
#*- encoding=utf-8 -*import sysprint(sys.getdefaultencoding()) def test(x,y,z): print(x) print(y) pr ...