12 năm trước
Tôi muốn tạo một hình ảnh JPG liên kết một số đồ họa mà tôi vẽ bằng tiện ích Tkinter Canvas. Điều này vẽ hình ảnh lên màn hình và sau đó tôi sử dụng chức năng thư viện PIL ImageGrab [] để lấy nó ra khỏi màn hình vào một tệp. Nó sẽ gọn gàng hơn để vẽ bằng cách vẽ nó trên màn hình bằng cách vẽ nó trong bộ nhớ và sau đó chụp nó ở định dạng mong muốn trong một tệp - điều này có thể không?
Câu trả lời được đề xuất
Đây là một cách để làm điều đó ...
# with the Tkinter canvas, drawings can only be saved as .ps files # use PIL to draw simultaneuosly to memory and then save to file # PIL allows .png .jpg .gif or .bmp file formats import Tkinter as …
Nhảy để đăng
Tất cả 2 câu trả lời
Vegaseat1.735 đồng nghiệp của Đội đạo đức giả của Daniweb 1,735 DaniWeb's Hypocrite
Team Colleague Đây là một cách để làm điều đó ...# with the Tkinter canvas, drawings can only be saved as .ps files
# use PIL to draw simultaneuosly to memory and then save to file
# PIL allows .png .jpg .gif or .bmp file formats
import Tkinter as tk
from PIL import Image, ImageDraw
root = tk.Tk[]
root.title["drawing lines"]
# some color constants for PIL
white = [255, 255, 255]
black = [0, 0, 0]
blue = [0, 0, 255]
red = [255, 0, 0]
green = [0,128,0]
width = 450
height = 450
# create the drawing canvas
cv = tk.Canvas[root, width=width, height=height, bg='white']
cv.pack[]
# create empty PIL image and draw objects to draw on
# PIL draws in memory only, but the image can be saved
image = Image.new["RGB", [width, height], white]
draw = ImageDraw.Draw[image]
# draw horizontal lines
x1 = 0
x2 = 450
for k in range[0, 500, 50]:
y1 = k
y2 = k
# Tkinter [visible]
cv.create_line[x1, y1, x2, y2]
# PIL [to memory for saving to file]
draw.line[[x1, y1, x2, y2], black]
# draw vertical lines
y1 = 0
y2 = 450
for k in range[0, 500, 50]:
x1 = k
x2 = k
# Tkinter
cv.create_line[x1, y1, x2, y2]
# PIL
draw.line[[x1, y1, x2, y2], black]
# Tkinter canvas object can only be saved as a postscipt file
# which is actually a postscript printer language text file
cv.postscript[file="mylines.ps", colormode='color']
# PIL image can be saved as .png .jpg .gif or .bmp file
filename = "mylines.jpg"
image.save[filename]
root.mainloop[]
12 năm trước
Nhảy để đăng
I am new to Python [ex-Visual Basic] and am excited by the power and elegance of the language. Also I value the simple and transparent build process. In this case I need to understand the 2 lines
image = Image.new["RGB", [width, height], white]
draw = ImageDraw.Draw[image][icode]which are the key that I was missing.
Tất cả 2 câu trả lời
Vegaseat1.735 đồng nghiệp của Đội đạo đức giả của Daniweb
Rất gọn gàng - cảm ơn bạn rất nhiều - đó chỉ là những gì tôi đang tìm kiếm [tôi đã thử nghiệm nó ở đây và nó hoạt động!]. Tôi chưa quen với Python [ex-Visual Basic] và bị kích thích bởi sức mạnh và sự thanh lịch của ngôn ngữ. Ngoài ra tôi đánh giá quá trình xây dựng đơn giản và minh bạch. Trong trường hợp này, tôi cần hiểu 2 dòng
đó là chìa khóa. Tôi đang làm đây ...!
Được chỉnh sửa 9 năm trước bởi Reverend Jim vì: Định dạng cố định
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:
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à:
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ốiImageDraw. 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à:
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àuImageFont 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.
Trong ví dụ này, chúng tôi tải phông chữ bằng phương thức trueType của ImageFont chuyển cho nó đường dẫn đến phông chữ và kích thước của các phông chữ được tải.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à: