Hướng dẫn how do i save a binary image in python? - làm cách nào để lưu hình ảnh nhị phân trong python?

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')

Hướng dẫn how do i save a binary image in python? - làm cách nào để lưu hình ảnh nhị phân trong python?

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:

  1. Đọc hình ảnh từ vị trí.
  2. 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.
  3. 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:

Hướng dẫn how do i save a binary image in python? - làm cách nào để lưu hình ảnh nhị phân trong python?

Ảnh gốc

Hướng dẫn how do i save a binary image in python? - làm cách nào để lưu hình ảnh nhị phân trong python?

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

Hình ảnh có thể được chuyển đổi thành nhị phân không?

Hình ảnh cũng cần được chuyển đổi thành nhị phân để máy tính xử lý chúng để có thể nhìn thấy chúng trên màn hình của chúng tôi.Hình ảnh kỹ thuật số được tạo thành từ pixel.Mỗi pixel trong một hình ảnh được tạo thành từ các số nhị phân. in order for a computer to process them so that they can be seen on our screen. Digital images are made up of pixels . Each pixel in an image is made up of binary numbers.

Tệp Python có phải là một tệp nhị phân không?

Python có các công cụ để làm việc với các tệp nhị phân.Tệp nhị phân sử dụng chuỗi loại byte.Điều này có nghĩa là khi đọc dữ liệu nhị phân từ một tệp, một đối tượng loại byte được trả về.Tệp nhị phân được mở bằng hàm Open (), có tham số chế độ chứa ký tự 'B'.. Binary files use strings of type bytes. This means when reading binary data from a file, an object of type bytes is returned. The binary file is opened using the open() function, whose mode parameter contains the character 'b'.

Python có nhị phân không?

Trong Python, bạn chỉ có thể sử dụng hàm bin () để chuyển đổi từ giá trị thập phân sang giá trị nhị phân tương ứng của nó.Và tương tự, hàm int () để chuyển đổi một nhị phân thành giá trị thập phân của nó.Hàm int () lấy đối số thứ hai là cơ sở của số sẽ được chuyển đổi, đó là 2 trong trường hợp số nhị phân.you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.