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. Show Cài đặt gốiTrướ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àufrom 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:
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à:
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à:
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à:
Xem thảo luận Cải thiện bài viết Lưu bài viết 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
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. 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 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: Ở đâ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 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 = 'red') img.save('pil_red.png')4 PIL 6PIL 7from PIL import Image img = Image.new('RGB', (60, 30), color = (73, 109, 137)) img.save('pil_color.png')5 PIL 9from 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')3 import 2from PIL import Image img = Image.new('RGB', (60, 30), color = (73, 109, 137)) img.save('pil_color.png')5 import 2from PIL import Image img = Image.new('RGB', (60, 30), color = (73, 109, 137)) img.save('pil_color.png')7 import 6import 7from PIL import Image img = Image.new('RGB', (60, 30), color = 'red') img.save('pil_red.png')4 import 9from 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: 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 .. |