Tôi sử dụng Python để làm việc với xử lý hình ảnh. Tôi đã quen với việc cắt, vẽ và những thứ khác, nhưng đối với một hình ảnh. Giống như trong tập lệnh dưới đây, làm thế nào tôi có thể đặt một vòng lặp trong tập lệnh để làm cho một số hình ảnh?
import PIL
import Image
im=Image.open['test.tif']
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
Jacob
40,5k6 huy hiệu vàng76 Huy hiệu bạc81 Huy hiệu đồng6 gold badges76 silver badges81 bronze badges
Hỏi ngày 9 tháng 8 năm 2011 lúc 14:04Aug 9, 2011 at 14:04
Bạn cần phải bọc nó trong vòng lặp for
và cung cấp cho vòng lặp đó một danh sách các tệp.
Một cách rất dễ dàng để có được danh sách tất cả các tệp TIF trong thư mục hiện tại là với glob
, như thế này:
import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
Katriel
117K19 Huy hiệu vàng133 Huy hiệu bạc165 Huy hiệu Đồng19 gold badges133 silver badges165 bronze badges
Đã trả lời ngày 9 tháng 8 năm 2011 lúc 14:08Aug 9, 2011 at 14:08
RichiehindlerichiehindleRichieHindle
264K46 Huy hiệu vàng354 Huy hiệu bạc395 Huy hiệu Đồng46 gold badges354 silver badges395 bronze badges
0
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
Chỉ cần thêm tên tệp vào danh sách filelist
. Vòng lặp for
lặp lại trên từng phần tử của danh sách và gán giá trị hiện tại cho imagefile
. Bạn có thể sử dụng imagefile
trong phần thân của vòng lặp của bạn để xử lý hình ảnh.
Đã trả lời ngày 9 tháng 8 năm 2011 lúc 14:07Aug 9, 2011 at 14:07
1
Một cái gì đó như thế này, có lẽ?
import PIL
import Image
images = ["test.tif", "test2.tif", "test3.tif"]
for imgName in images:
im=Image.open[imgName]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
Đã trả lời ngày 9 tháng 8 năm 2011 lúc 14:07Aug 9, 2011 at 14:07
Một cái gì đó như thế này, có lẽ?combatdave
Combatdavecombatdave7 silver badges17 bronze badges
OpenCV: Nhập CV2# Để đọc hình ảnh là colorcv2_img = cv2. Imread [.
Nhập khẩu Pydicom Pydicom dưới dạng DICOM ..
Skimage từ IOIO nhập khẩu này. Imsave ["write_brain_image. PNG", IMG].
OpenCV: Nhập CV2# Để đọc hình ảnh là colorcv2_img = cv2. Imread [.
Nhập khẩu Pydicom Pydicom dưới dạng DICOM ..
Skimage từ IOIO nhập khẩu này. Imsave ["write_brain_image. PNG", IMG].
Làm cách nào để lưu một hình ảnh trong một thư mục cụ thể trong Python?
Ở đây, chúng ta có thể thấy cách lưu một hình ảnh trong Python ..Using os.listdir
Trong ví dụ này, tôi đã nhập một mô -đun có tên Image từ PIL và đã khai báo một hình ảnh biến và hình ảnh được gán. Mở [r'DOWDLOADS \ 3. JPG '] Đường dẫn và tên của hình ảnh cùng với phần mở rộng ..
- Và tuyên bố một biến khác và hình ảnh được gán. Tiết kiệm [búp bê của người Viking. JPG] ..os module to interact with the operating system.
- Xem thảo luậnlistdir[] function from os to get access to the folders given in quotes.
- Cải thiện bài viếtos.listdir[] function, we iterate through the images and printed the names in order.
- Lưu bài viết.png files to be loaded using the endswith[] function.
Python3
Đọc
Bàn luận
Trong bài viết này, chúng ta sẽ học cách lặp lại thông qua các hình ảnh trong một thư mục trong Python. & NBSP;
Phương pháp 1: Sử dụng Os.ListDir
for
for
4
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
1 for
6
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
8import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
9import PIL
import Image
images = ["test.tif", "test2.tif", "test3.tif"]
for imgName in images:
im=Image.open[imgName]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0Output::
Phương pháp 3: Sử dụngglob.iglob []
Lúc đầu, chúng tôi đã nhập mô -đun GLOB..png, .jpg, .jpeg files to be loaded using the endswith[] function.
Python3
Đọc
Bàn luận
Trong bài viết này, chúng ta sẽ học cách lặp lại thông qua các hình ảnh trong một thư mục trong Python. & NBSP;
Phương pháp 1: Sử dụng Os.ListDir
Ví dụ 1: Chỉ lặp qua .png chỉ
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
8for
9 glob
0glob
6import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
7import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
8import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
9import PIL
import Image
images = ["test.tif", "test2.tif", "test3.tif"]
for imgName in images:
im=Image.open[imgName]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0Output:
Phương pháp 2: Sử dụng & NBSP; Mô -đun Pathlibpathlib module
- Lúc đầu, chúng tôi đã nhập mô -đun PATHLIB từ đường dẫn.pathlib module from Path.
- Sau đó, chúng tôi vượt qua hàm thư mục/thư mục bên trong đường dẫn [] và sử dụng chức năng .glob [‘*. PNG,] để lặp qua tất cả các hình ảnh có trong thư mục này.Path[] function and used it .glob[‘*.png’] function to iterate through all the images present in this folder.
Python3
import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
2 filelist
2import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0 filelist
4import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
6import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
7 filelist
7import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
7 for
0for
1for
2for
for
4
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
1 for
6import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
3import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
9for
9Output:
Phương pháp 3: Sử dụngglob.iglob [] glob.iglob[]
- Lúc đầu, chúng tôi đã nhập mô -đun GLOB.glob module.
- Sau đó, với sự trợ giúp của hàm glob.iglob [], chúng tôi lặp lại thông qua các hình ảnh và in tên theo thứ tự.glob.iglob[] function we iterate through the images and print the names in order.
- Ở đây chúng tôi đã đề cập đến các tệp .png sẽ được tải bằng hàm endswith []..png files to be loaded using the endswith[] function.
Python3
import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0 glob
import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
6import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
7 filelist
7import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0import PIL
import Image
import glob
for filename in glob.glob["*.tif"]:
im=Image.open[filename]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
7 for
0for
1for
2for
for
4
import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
1 for
6import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
8import PIL
import Image
filelist = ['test.tif','test2.tif']
for imagefile in filelist:
im=Image.open[imagefile]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
9import PIL
import Image
images = ["test.tif", "test2.tif", "test3.tif"]
for imgName in images:
im=Image.open[imgName]
box=[50, 50, 200, 200]
im_crop=im.crop[box]
im_crop.show[]
0Output::