Python 如何向JPEG文件添加文本或合并图像
我有一个web应用程序,用户将上传带有布局的JPEG文件作为我的示例,我需要动态地将文本添加到“标题”区域,并将其他JPEG文件合并到“底部”区域。 是否可以使用任何一种语言进行此操作,最好使用.Net。 使用以下c#代码从文本创建jpeg图像:Python 如何向JPEG文件添加文本或合并图像,python,.net,image-processing,machine-learning,pattern-matching,Python,.net,Image Processing,Machine Learning,Pattern Matching,我有一个web应用程序,用户将上传带有布局的JPEG文件作为我的示例,我需要动态地将文本添加到“标题”区域,并将其他JPEG文件合并到“底部”区域。 是否可以使用任何一种语言进行此操作,最好使用.Net。 使用以下c#代码从文本创建jpeg图像: /// <summary> /// Creates a Jpeg image file drawing the given text and saves the file on give disk location.
/// <summary>
/// Creates a Jpeg image file drawing the given text and saves the file on give disk location.
/// </summary>
/// <param name="text">text which is to be draw in image</param>
/// <param name="font">font is to be used for the text</param>
/// <param name="textColor">color to be used for the text</param>
/// <param name="backColor">background color to be used for the image</param>
/// <param name="filename">filename with complete path where you want to save the output jpeg file.</param>
private static void DrawText(String text, Font font, Color textColor, Color backColor, string filename)
{
//first, create a dummy bitmap just to get a graphics object
Image image = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(image);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
image.Dispose();
drawing.Dispose();
//create a new image of the right size
image = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(image);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
image.Save(filename, ImageFormat.Jpeg);
}
您可以使用图像库来完成此操作,例如imagemagick。是的,两种语言都可以。也许你应该问一个更具体的问题。我需要将文本添加到现有JPEG文件的“标题”区域,并且我需要将JPEG文件2合并到文件1的“底部”区域。@Simon-我已经更新了我的帖子,以便将添加文本的代码附加到现有JPEG图像中。请参阅我文章中的最后一个代码块。我需要找到“标题”和“底部”区域,它们都是矩形区域。谢谢!你的代码对我有用。如果你能帮我检测图像上的矩形区域,那将是完美的~如果你知道图像的尺寸、标题区域和底部区域,你可以裁剪图像并将其与你自己的图像连接起来。
/// <summary>
/// Merges two Jpeg images vertically
/// </summary>
/// <param name="inputJpeg1">filename with complete path of the first jpeg file.</param>
/// <param name="inputJpeg2">filname with complete path of the second jpeg file.</param>
/// <param name="outputJpeg">filename with complete path where you want to save the output jpeg file.</param>
private void MergeJpeg(string inputJpeg1, string inputJpeg2, string outputJpeg)
{
Image image1 = Image.FromFile(inputJpeg1);
Image image2 = Image.FromFile(inputJpeg2);
int width = Math.Max(image1.Width, image2.Width);
int height = image1.Height + image2.Height;
Bitmap outputImage = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(outputImage);
graphics.Clear(Color.Black);
graphics.DrawImage(image1, new Point(0, 0));
graphics.DrawImage(image2, new Point(0, image1.Height));
graphics.Dispose();
image1.Dispose();
image2.Dispose();
outputImage.Save(outputJpeg, System.Drawing.Imaging.ImageFormat.Jpeg);
outputImage.Dispose();
}
var inputFile = @"c:\inputImage.jpg";
var outputFile = @"c:\outputImage.jpg";
Bitmap bitmap = null;
//Open file in read moad and create a stream and using that stream create a bitmap.
using (var stream = File.OpenRead(inputFile))
{
bitmap = (Bitmap)Bitmap.FromStream(stream);
}
using (bitmap)
using (var graphics = Graphics.FromImage(bitmap))
using (var font = new Font("Arial", 20, FontStyle.Regular))
{
//Draw Title in the existing jpeg file at coordinates 0,0 these coordinates can be changed as per your need.
graphics.DrawString("Title ", font, Brushes.Red, 0, 0);
//Save the updated file.
bitmap.Save(outputFile);
}