7

I want to download the MNIST images to my computer as PNG files.

I found this page: http://yann.lecun.com/exdb/mnist/

After I pressed: train-images-idx3-ubyte.gz: training set images (9912422 bytes)

Please let me know if you have any ideas or suggestions. Thank you!

  • This is far too broad/vague, and possibly off-topic. Please see [ask], [help/on-topic]. – AMC Mar 10 '20 at 19:31

1 Answers1

7

You need to unzip these particular files in order to use them. A better way of doing it would be:

Download via:

curl -O http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz

Download to a particular path:

curl -O target/path/filename URL

Unzip the downloaded gzip archives:

gunzip t*-ubyte.gz

For further processing of data see the documentation

import gzip
f = gzip.open('train-images-idx3-ubyte.gz','r')

image_size = 28
num_images = 5

import numpy as np
import matplotlib.pyplot as plt

f.read(16)
buf = f.read(image_size * image_size * num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size, 1)
image = np.asarray(data[2]).squeeze()
plt.imshow(image)

For extracting image see here

Update

Try this link to simply download and expand .gz files

skaul05
  • 1,994
  • 2
  • 15
  • 24
  • 1
    Thank you for the quick response. After I unzip the .gz file, how do I get image files. The link for the documentation only shows how to get them into a NumPY arrays and CSV files. –  Mar 07 '19 at 17:42
  • 1
    I will give it a try. What does the attached script do? –  Mar 07 '19 at 19:13
  • 1
    I ran the script, and got this error: Traceback (most recent call last): File "mnist.py", line 10, in new.read(16) NameError: name 'new' is not defined –  Mar 07 '19 at 19:18
  • Edited pls check now. The attached script will print the image fetched from gz file – skaul05 Mar 08 '19 at 02:31
  • 1
    Everything worked (just needed to add plt.show()) BUT I don't see how the link for downloading the image was helpful for downloading the image. Can you provide some code on how to download the image to a directory? I appreciate all of the help! –  Mar 08 '19 at 06:33
  • That calls for a different question @NikolasIoannou. However, you can use `curl -o target/path/filename URL` to download in a particular path. – skaul05 Mar 08 '19 at 06:44
  • The question is titled: How to download MNIST images locally. Does that not mean download images to a local directory? Anyway, do you have any tips on how to do that? –  Mar 08 '19 at 06:45
  • Updated my answer @NikolasIoannou – skaul05 Mar 08 '19 at 06:47
  • While I appreciate the help, I am not sure that we are talking about the same thing. What I want to do is download the images in the MNIST dataset onto my computer. I want to store all the png photos of integers in the MNIST dataset on my computer. –  Mar 08 '19 at 06:50
  • Because you have contributed so much, just edit your answer (because I already posted that), and I will give you the credit! –  Mar 08 '19 at 15:50
  • Thanks for the insight @NikolasIoannou. Updated my answer. Always happy to help contributors!! – skaul05 Mar 08 '19 at 16:34