Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

Gối là một cái nĩa của Pil. Bạn nên sử dụng gối những ngày này.

Cài đặt gối

Trước khi bạn có thể sử dụng nó, bạn cần cài đặt thư viện gối. Đọc tài liệu của Gối về cách cài đặt nó trên hệ điều hành của bạn.

Vẽ một hình ảnh đơn giản với một màu

from PIL import Image, ImageDraw
img = Image.new(mode, size, color)
img.save(filename)

Có nhiều giá trị khác nhau cho chế độ được liệt kê trong tài liệu của Gối. Ví dụ RGB và RGBA có thể là chế độ. Kích thước là một tuple ở dạng (chiều rộng, chiều cao) tính bằng pixel. Màu sắc có thể là một từ như 'màu đỏ' hoặc bộ ba cho màu RGB của 3 giá trị trong khoảng 0-255.mode listed in the documentation of Pillow. For example RGB and RGBA can be modes. The size is a tuple in the form of (width, height) in pixels. The color can be a word such as 'red', or a triplet for RGB colors of 3 values between 0-255.

Kịch bản này:

examples/python/pil_new_red_image.py

from PIL import Image

img = Image.new('RGB', (60, 30), color = 'red')
img.save('pil_red.png')

sẽ tạo hình ảnh này:

Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

Chúng tôi cũng có thể sử dụng các giá trị RGB riêng lẻ ở dạng thập phân được lấy từ một số bánh xe màu hoặc ứng dụng chọn màu khác.

examples/python/pil_new_color_image.py

from PIL import Image

img = Image.new('RGB', (60, 30), color = (73, 109, 137))
img.save('pil_color.png')

Kết quả là:

Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

Viết văn bản trên hình ảnh

Đối với điều này, chúng tôi cũng cần nhập hình ảnh. Chúng tôi vượt qua vị trí của góc trên cùng bên trái của văn bản, chính văn bản và màu của văn bản. Có số lượng các tham số khác mà bạn có thể chuyển cho phương thức này.ImageDraw. We pass the location of the top-left corner of the text, the text itself, and the color of the text. There are number of other parameters you can pass to this method.

examples/python/pil_write_text_on_image.py

from PIL import Image, ImageDraw

img = Image.new('RGB', (100, 30), color = (73, 109, 137))

d = ImageDraw.Draw(img)
d.text((10,10), "Hello World", fill=(255,255,0))

img.save('pil_text.png')

Kết quả là:

Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

Viết văn bản trên hình ảnh

Đối với điều này, chúng tôi cũng cần nhập hình ảnh. Chúng tôi vượt qua vị trí của góc trên cùng bên trái của văn bản, chính văn bản và màu của văn bản. Có số lượng các tham số khác mà bạn có thể chuyển cho phương thức này.ImageFont to load a TrueType font. Mac OSX supplies a bunch of fonts that are located in the /Library/Fonts/. On other platforms you'll need to locate the files yourself and then pass the full path to the function. Alternatively you could include the font-file in your application and then you can know where is the font-file relative to your code.

Chọn phông chữtruetype method of the ImageFont passing to it the path to the fonts and the size of the fonts to be loaded.

examples/python/pil_write_text_on_image_select_font.py

from PIL import Image, ImageDraw, ImageFont

img = Image.new('RGB', (100, 30), color = (73, 109, 137))

fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
d = ImageDraw.Draw(img)
d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0))

img.save('pil_text_font.png')

Kết quả là:

Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcPython Imaging Library (PIL). Using this PIL we can do so many operations on images like create a new Image, edit an existing image, rotate an image, etc. For adding text we have to follow the given approach.

    Bàn luận

    • Trong Python để mở một hình ảnh, chỉnh sửa hình ảnh, lưu hình ảnh đó ở các định dạng khác nhau, một thư viện bổ sung gọi là Thư viện hình ảnh Python (PIL). Sử dụng PIL này, chúng tôi có thể thực hiện rất nhiều hoạt động trên các hình ảnh như tạo một hình ảnh mới, chỉnh sửa hình ảnh hiện có, xoay hình ảnh, v.v. để thêm văn bản, chúng tôi phải tuân theo cách tiếp cận đã cho.
    • Cách tiếp cận
    • Nhập mô -đun
    • Mở hình ảnh được nhắm mục tiêu
    • Thêm thuộc tính văn bản bằng cách sử dụng đối tượng hình ảnh

    Hiển thị hình ảnh đã chỉnh sửaobj.text( (x,y), Text, font, fill)

    Parameters: 

    • Lưu hình ảnh đóThis X and Y denotes the starting position(in pixels)/coordinate of adding the text on an image.
    • Cú pháp: obj.text ((x, y), văn bản, phông chữ, điền)A Text or message that we want to add to the Image.
    • .specificfont type and font size that you want to give to the text.
    • Văn bản: Một văn bản hoặc tin nhắn mà chúng tôi muốn thêm vào hình ảnh.Fill is for to give the Font color to your text.

    Phông chữ: Loại cụ thể và kích thước phông chữ mà bạn muốn cung cấp cho văn bản.

    Fill: Fill là để cung cấp màu phông chữ cho văn bản của bạn.

    Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

    Khác với những điều này, chúng tôi yêu cầu một số mô -đun từ PIL để thực hiện nhiệm vụ này. Chúng ta cần hình ảnh có thể thêm đồ họa 2D (hình dạng, văn bản) vào một hình ảnh. Ngoài ra, chúng tôi yêu cầu mô -đun ImageFont để thêm kiểu phông chữ tùy chỉnh và kích thước phông chữ. Đưa ra dưới đây là việc triển khai thêm văn bản vào một hình ảnh. & NBSP; Add a simple text to an image. ( without custom Font style)

    Python3

    from PIL import Image

    from PIL import

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    2

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    3
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    5
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    6
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    7
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    8
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    9

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    0
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    2

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    3
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    6
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    7
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    8
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    9
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    7
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    2
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    4
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    7

    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    8

    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    9
    from PIL import Image, ImageDraw, ImageFont
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0))
    
    img.save('pil_text_font.png')
    0
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    9

    Output:

    Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python

    Ở đây bạn có thể thấy rằng chúng tôi thành công thêm văn bản vào một hình ảnh nhưng nó không thể nhìn thấy đúng để chúng tôi có thể thêm tham số phông chữ để đưa ra một kiểu tùy chỉnh.

    Ví dụ 2: Thêm một văn bản đơn giản vào một hình ảnh. (Với kiểu phông chữ tùy chỉnh) Add a simple text to an image. ( With custom Font style)

    Python3

    from PIL import Image

    from PIL import

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    2

    from PIL import from3

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    3
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    5
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    6
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    7
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    8
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    9

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    0
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    2

    PIL 4

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4 PIL 6PIL 7
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5PIL 9
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    9

    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    3import2
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5import2
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    7import6import7
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4import9
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    7
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    2
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    4
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = (73, 109, 137))
    img.save('pil_color.png')
    
    5
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    4
    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    7

    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    8

    from PIL import Image, ImageDraw
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello World", fill=(255,255,0))
    
    img.save('pil_text.png')
    9
    from PIL import Image, ImageDraw, ImageFont
    
    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
    
    fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
    d = ImageDraw.Draw(img)
    d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0))
    
    img.save('pil_text_font.png')
    0
    from PIL import Image
    
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')
    
    9

    Output:

    Hướng dẫn how to write a image in python - làm thế nào để viết một hình ảnh trong python


    Làm thế nào để bạn đọc và viết một hình ảnh trong Python?

    Trong Python và OpenCV, bạn có thể đọc (tải) và ghi (lưu) các tệp hình ảnh với cv2.imread () và cv2.imwrite ().Hình ảnh được đọc dưới dạng mảng numpy ndarray.cv2. imread() and cv2. imwrite() . Images are read as NumPy array ndarray .

    Làm cách nào để hiển thị một hình ảnh trong Python?

    Trên Windows, nó lưu hình ảnh vào tệp BMP tạm thời và sử dụng tiện ích hiển thị BMP tiêu chuẩn để hiển thị nó (thường là sơn) ...
    Cú pháp: Image.Show (title = none, lệnh = none).
    Parameters:.
    Tiêu đề - Tiêu đề tùy chọn để sử dụng cho cửa sổ hình ảnh, nếu có thể ..
    Lệnh - lệnh được sử dụng để hiển thị hình ảnh ..