Hello Mat

 找回密码
 立即注册
查看: 10435|回复: 6

python怎么按顺序读取文件夹中的图片

[复制链接]

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
发表于 2017-6-14 20:08:59 | 显示全部楼层 |阅读模式
如题,有好几个文件夹,每个文件夹中有多个图片,怎么一张一张读出来
回复

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-6-16 22:34:02 | 显示全部楼层
首先安装PIL库:http://halcom.cn/forum.php?mod=viewthread&tid=438
注意:python可能不识别带中文字符的路径

参考连接:Tensorflow:https://stackoverflow.com/questions/34783030/saving-image-files-in-tensorflow
https://stackoverflow.com/questions/33849617/how-do-i-convert-a-directory-of-jpeg-images-to-tfrecords-file-in-tensorflow


百度网盘链接:
链接:http://pan.baidu.com/s/1qY4YkHY
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1




算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
 楼主| 发表于 2017-6-18 22:51:04 | 显示全部楼层
Halcom 发表于 2017-6-16 22:34
首先安装PIL库:http://halcom.cn/forum.php?mod=viewthread&tid=438
注意:python可能不识别带中文字符的 ...

谢谢了,我用tensorflow顺序读彩色图片,然后加上图片序号存入一个4-D张量中,总是报错,麻烦你帮我看一下,程序楼下我贴出来了
回复 支持 反对

使用道具 举报

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
 楼主| 发表于 2017-6-18 22:51:47 | 显示全部楼层
import numpy as np
import tensorflow as tf
def get_image(image_path):  
    """Reads the jpg image from image_path.
    Returns the image as a tf.float32 tensor
    Args:
        image_path: tf.string tensor
    Reuturn:
        the decoded jpeg image casted to float32
    """  
    return tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.read_file(image_path), channels=3),dtype=tf.uint8)
data=np.zeros((25,300,300,3),np.uint8)
for i in range(25):
    for idx in range(25):            
            a=get_image('C:/Users/Administrator/Desktop/test/2 '+'('+str(i)+').jpg')
            data[idx,:,:,:]=a
回复 支持 反对

使用道具 举报

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
 楼主| 发表于 2017-6-18 22:53:03 | 显示全部楼层
错误原因是这样的:
  File "C:/Users/Administrator/小甲鱼/readPIC.py", line 48, in <module>
    data[idx,:,:,:]=a

ValueError: setting an array element with a sequence.
回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-8-8 22:18:28 | 显示全部楼层
QQQQ 发表于 2017-6-18 22:53
错误原因是这样的:
  File "C:/Users/Administrator/小甲鱼/readPIC.py", line 48, in
    data=a
第一种方法:numpy进行操作,保存为4-D矩阵:

from PIL import Image
import numpy as np
import os

def load_Img(imgDir,imgFoldName):
    imgs = os.listdir(imgDir+imgFoldName)
    imgNum = len(imgs)
    data = np.empty((imgNum,28,28,1),dtype="float32")
    for i in range (imgNum):
        img = Image.open(imgDir+imgFoldName+"/"+imgs)
        arr = np.asarray(img,dtype="float32")
        arr = np.reshape(arr, [28,28,1] )
        data[i,:,:,:] = arr
    return data

imgDir = "./Test/"
imgFoldName = "test"
data = load_Img(imgDir,imgFoldName)
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-8-9 13:02:56 | 显示全部楼层
QQQQ 发表于 2017-6-18 22:51
import numpy as np
import tensorflow as tf
def get_image(image_path):  

第2种方法:tensorflow进行操作,保存为tensor:
# -*- coding: utf-8 -*-
"""
Created on Wed Aug  9 12:48:17 2017

@author: Administrator
"""

import tensorflow as tf
import numpy as np
import os
from PIL import Image

cur_dir = os.getcwd()
print("For Tensorflow to load images")
print("current directory:",cur_dir)

def modify_image(image):
    resized = tf.image.resize_images(image, [80, 80], 1)
    resized.set_shape([80,80,3])
    flipped_images = tf.image.flip_up_down(resized)
    return flipped_images

def read_image(filename_queue):
    reader = tf.WholeFileReader()
    key,value = reader.read(filename_queue)
    image = tf.image.decode_png(value)
    return key,image

def inputs():
    # 手写数字--灰度图像
    filenames = ['./Test/test/1.png', './Test/test/2.png' ]
#    加载所有的图像
    FullImageList = [];
    filenames = os.listdir('./Test/test/')
    for fn in filenames:
        FullImage = os.path.join( cur_dir, 'Test/test', fn )
        FullImageList.append(FullImage)

    filename_queue = tf.train.string_input_producer(filenames)
    filename,read_input = read_image(filename_queue)
    reshaped_image = modify_image(read_input)
    return filename,reshaped_image

with tf.Graph().as_default():
    image = inputs()
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)
    tf.train.start_queue_runners(sess=sess)
    for i in range(3):
        filename,img = sess.run(image)
        print (filename)
        # 手写数字--灰度图像
        img = Image.fromarray(img[:,:,0], "L")
        img.save(os.path.join(cur_dir+'/Test/',"ysw"+str(i)+".bmp"))

参考连接:https://stackoverflow.com/questions/34783030/saving-image-files-in-tensorflow


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-25 23:14 , Processed in 0.206890 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表