Bạn có thể sử dụng
Populating the interactive namespace from numpy and matplotlib 3 với chế độ
Populating the interactive namespace from numpy and matplotlib 4 và đặt mỗi số nguyên làm pixel trong hình ảnh ban đầu của bạn:
>>> from PIL import Image >>> import random >>> data = [random.choice((0, 1)) for _ in range(2500)] >>> data[:] = [data[i:i + 50] for i in range(0, 2500, 50)] >>> print data [[0, 1, 0, 0, 1, ...], [0, 1, 1, 0, 1, ...], [1, 1, 0, 1, ...], ...] >>> img = Image.new('1', (50, 50)) >>> pixels = img.load() >>> for i in range(img.size[0]): ... for j in range(img.size[1]): ... pixels[i, j] = data[i][j] >>> img.show() >>> img.save('/tmp/image.bmp')Cải thiện bài viết
Lưu bài viết
Trong bài viết này, chúng tôi sẽ chuyển đổi hình ảnh thành dạng nhị phân của nó. Một hình ảnh nhị phân là một hình ảnh đơn sắc bao gồm các pixel có thể có một trong hai màu chính xác, thường là đen và trắng. Hình ảnh nhị phân cũng được gọi là cấp độ hai cấp hoặc hai cấp. Điều này có nghĩa là mỗi pixel được lưu trữ dưới dạng một bit duy nhất, tức là 0 hoặc 1.
Thư viện quan trọng nhất cần thiết để xử lý hình ảnh trong Python là OpenCV. Hãy chắc chắn rằng bạn đã cài đặt thư viện vào Python của bạn. Đối với các bước để cài đặt OpenCV đề cập đến bài viết này: Thiết lập OpenCV với môi trường AnacondaOpenCV. Make sure you have installed the library into your Python. For steps for installing OpenCV refers to this article: Set up Opencv with anaconda environment
Approach:
- Đọc hình ảnh từ vị trí.
- Là một hình ảnh màu có các lớp RGB trong đó và phức tạp hơn, hãy chuyển đổi nó sang dạng thang độ xám của nó trước tiên.
- Thiết lập một dấu ngưỡng, các pixel phía trên dấu đã cho sẽ chuyển màu trắng và bên dưới điểm sẽ chuyển sang màu đen.
Dưới đây là việc thực hiện:
Python3
Populating the interactive namespace from numpy and matplotlib 5
Populating the interactive namespace from numpy and matplotlib 6
Populating the interactive namespace from numpy and matplotlib 7
Populating the interactive namespace from numpy and matplotlib 8
Populating the interactive namespace from numpy and matplotlib 9<matplotlib.figure.Figure at 0x79b2b00>0<matplotlib.figure.Figure at 0x79b2b00>1<matplotlib.figure.Figure at 0x79b2b00>2<matplotlib.figure.Figure at 0x79b2b00>3
<matplotlib.figure.Figure at 0x79b2b00>4
Populating the interactive namespace from numpy and matplotlib 8 <matplotlib.figure.Figure at 0x79b2b00>6<matplotlib.figure.Figure at 0x79b2b00>7<matplotlib.figure.Figure at 0x79b2b00>1<matplotlib.figure.Figure at 0x79b2b00>9
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool') 0
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool') 1
Populating the interactive namespace from numpy and matplotlib 8 <matplotlib.figure.Figure at 0x79b2b00>6<matplotlib.figure.Figure at 0x79b2b00>7<matplotlib.figure.Figure at 0x79b2b00>1<matplotlib.figure.Figure at 0x79b2b00>9
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool') 0
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool') 8
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool') 9
In [3]:
matshow(checkerboard) 0
In [3]:
matshow(checkerboard) 1
In [3]:
matshow(checkerboard) 2<matplotlib.figure.Figure at 0x79b2b00>3
In [3]:
matshow(checkerboard) 4
Output:
Ảnh gốc
Mẫu nhị phân
Viết hình ảnh nhị phân bằng gối
Gối là một cái nĩa của Pil.
Đó là một công cụ thao tác hình ảnh hữu ích, nhưng dường như nó có một số lỗi khi đọc hoặc viết dữ liệu nhị phân.
Sổ ghi chép Python này là để phục vụ như một hướng dẫn để làm việc xung quanh các vấn đề trong Gối.
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys')
Populating the interactive namespace from numpy and matplotlib
<matplotlib.figure.Figure at 0x79b2b00>
In [2]:
checkerboard = np.mod(np.arange(49).reshape(7,7),2).astype('bool')
In [3]:
matshow(checkerboard)
Out[3]:
<matplotlib.image.AxesImage at 0x7d41c50>
In [4]:
#whats the dtype? checkerboard.dtype
Out[4]:
dtype('bool')
In [5]:
#create a PIL image with binary mode cb_img = Image.fromarray(checkerboard,mode='1')
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 0
Đây là hình ảnh, thừa nhận là nhỏ của nó: nhưng bạn có thể thấy không có màu trắng!
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 1
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 2
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 3
Đây là hình ảnh mới: Bây giờ nó là một bảng kiểm tra!
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 4
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 5
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 6
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 7
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 8
Để viết một tệp nhị phân, trước tiên bạn cần chuyển đổi dữ liệu nhị phân thành kiểu dữ liệu
In [3]:
matshow(checkerboard) 5 và chuyển sang kiểu dữ liệu đó.
Sau đó, bạn cần chuyển đổi bằng phương pháp
In [3]:
matshow(checkerboard) 6 sang nhị phân trước khi lưu.
In [1]:
#For data manipulations %pylab inline from IPython.display import set_matplotlib_formats from io import BytesIO import numpy as np #to compare to scipy's builtin conversions from scipy.misc import imsave, toimage #import pillow from PIL import Image set_cmap('Greys') 9
Populating the interactive namespace from numpy and matplotlib 0
Viết trực tiếp vào Zipfiles
Populating the interactive namespace from numpy and matplotlib 1
Populating the interactive namespace from numpy and matplotlib 2