本篇文章给大家谈谈在Python中调整图像大小而不会丢失EXIF数据,以及python设置图像大小的知识点,同时本文还将给你拓展Android:上传图片而不会丢失Exif数据、c–查找浮点类型可以处理
本篇文章给大家谈谈在Python中调整图像大小而不会丢失EXIF数据,以及python设置图像大小的知识点,同时本文还将给你拓展Android:上传图片而不会丢失Exif数据、c – 查找浮点类型可以处理的最大整数大小而不会丢失精度、C#中调整图像大小的步骤详解、html – 在’a href’标记链接中调整图像大小等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 在Python中调整图像大小而不会丢失EXIF数据(python设置图像大小)
- Android:上传图片而不会丢失Exif数据
- c – 查找浮点类型可以处理的最大整数大小而不会丢失精度
- C#中调整图像大小的步骤详解
- html – 在’a href’标记链接中调整图像大小
在Python中调整图像大小而不会丢失EXIF数据(python设置图像大小)
我需要使用Python调整jpg图像的大小,而又不丢失原始图像的EXIF数据(有关拍摄日期,相机型号等的元数据)。所有有关python和图像的google搜索都指向我当前正在使用的PIL库,但似乎无法保留元数据。我到目前为止(使用PIL)的代码是这样的:
img = Image.open(''foo.jpg'')width,height = 800,600if img.size[0] < img.size[1]: width,height = height,widthresized_img = img.resize((width, height), Image.ANTIALIAS) # best down-sizing filterresized_img.save(''foo-resized.jpg'')
有任何想法吗?还是我可能正在使用的其他库?
答案1
小编典典import jpegjpeg.setExif(jpeg.getExif(''foo.jpg''), ''foo-resized.jpg'')
http://www.emilas.com/jpeg/
Android:上传图片而不会丢失Exif数据
在我们的应用程序中,用户多年来一直使用(大约)以下代码上传数百万个图像:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(postFilePath, bmOptions);
Bitmap roughBitmap = BitmapFactory.decodeFile(postFilePath, bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
roughBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
InputStream fis = new ByteArrayInputStream(stream.toByteArray());
int fileSize = stream.toByteArray().length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);
...
if (fis != null) {
byte[] buf = new byte[10240];
int read;
while ((read = fis.read(buf)) > 0) {
os.write(buf, 0, read);
totalBytesRead += read;
if (uploadProgressListener != null) {
try {
uploadProgressListener.onBytesuploaded(read);
} catch (Exception e) {
Log.e(e);
}
}
}
fis.close();
}
最近,我们看到有必要保留上传图像的Exif数据.问题是压缩位图时图像Exif数据丢失.我想到了使用ExifInterface从原始文件中提取此数据:
ExifInterface oldExif = new ExifInterface(postFilePath);
String value = oldExif.getAttribute(ExifInterface.TAG_DATETIME);
..,然后将其添加到InputStream fis,然后继续上传文件.问题在于ExifInterface无法将Exif数据保存到InputStream.
将Exif数据上传到服务器后如何保留在图像中?
它不是重复的:
为了更深入地说明,我尝试通过以下方法使用建议的重复问题:
public static void copyExif(String originalPath, InputStream newStream) throws IOException {
String[] attributes = new String[]
{
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_DATETIME_DIGITIZED,
ExifInterface.TAG_EXPOSURE_TIME,
ExifInterface.TAG_FLASH,
ExifInterface.TAG_FOCAL_LENGTH,
ExifInterface.TAG_GPS_ALTITUDE,
ExifInterface.TAG_GPS_ALTITUDE_REF,
ExifInterface.TAG_GPS_DATESTAMP,
ExifInterface.TAG_GPS_LATITUDE,
ExifInterface.TAG_GPS_LATITUDE_REF,
ExifInterface.TAG_GPS_LONGITUDE,
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_PROCESSING_METHOD,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_MAKE,
ExifInterface.TAG_MODEL,
ExifInterface.TAG_ORIENTATION,
ExifInterface.TAG_SUBSEC_TIME,
ExifInterface.TAG_WHITE_BALANCE
};
ExifInterface oldExif = new ExifInterface(originalPath);
ExifInterface newExif = new ExifInterface(newStream);
if (attributes.length > 0) {
for (int i = 0; i < attributes.length; i++) {
String value = oldExif.getAttribute(attributes[i]);
if (value != null)
newExif.setAttribute(attributes[i], value);
}
newExif.saveAttributes();
}
}
..但出现异常java.io.IOException:ExifInterface不支持保存当前输入的属性.在newExif.saveAttributes()之后;因为我正在尝试将属性保存到InputStream.我还能怎么做?
解决方法:
The problem is that the image Exif data is lost when compressing the bitmap
读取位图时,EXIF数据会丢失.位图没有EXIF标记.
How can Exif data be retained in the images when they’er uploaded to the server?
停止阅读位图.只需按原样上传postFilePath的内容.它将包含它包含的任何EXIF标签.
我的假设是,您正在阅读位图,希望再次以70%JPEG品质保存它会节省大量带宽.我怀疑您没有节省太多,并且在某些情况下可能会增加带宽(例如,postFilePath指向PNG).您的成本是大量的cpu时间,增加的OutOfMemoryError风险以及丢失的EXIF标签.
相反,如果将convert-to-70%-JPEG转换为某种数据规范化方法,则可以在服务器上使用该功能,因为在该服务器上您具有更多的cpu功能,更多的磁盘空间,更多的RAM和连续的功能.
c – 查找浮点类型可以处理的最大整数大小而不会丢失精度
#include <boost/cstdint.hpp>#include <limits>template<typename T,typename TFloat>voidmaxint_to_double(){ T i = std::numeric_limits<T>::max(); TFloat d = i; std::cout << std::fixed << i << std::endl << d << std::endl;}intmain(){ maxint_to_double<int,double>(); maxint_to_double<boost::intmax_t,double>(); maxint_to_double<int,float>(); return 0;}
这打印:
21474836472147483647.00000092233720368547758079223372036854775800.00000021474836472147483648.000000
注意max int如何在不损失精度的情况下适合双精度而boost :: intmax_t(在这种情况下为64位)不能. float甚至不能持有int.
现在,问题是:在C中是否有一种方法可以检查给定整数类型的整个范围是否可以适应一个浮动点类型而不会损失精度?
最好,
>这将是一个可以在静态断言中使用的编译时检查,
>并且不会涉及枚举编译器应该知道或可以计算的常量.
解决方法
#include <limits>template <typename T,typename U>struct can_fit{ static const bool value = std::numeric_limits<T>::digits <= std::numeric_limits<U>::digits;};#include <iostream>int main(void){ std::cout << std::boolalpha; std::cout << can_fit<short,float>::value << std::endl; std::cout << can_fit<int,float>::value << std::endl; std::cout << can_fit<int,double>::value << std::endl; std::cout << can_fit<long long,double>::value << std::endl; std::cout << can_fit<short,int>::value << std::endl; std::cout << can_fit<int,short>::value << std::endl;}
测试一个T中可用的二进制精度是否存在于U.适用于所有类型.
“Boostified”:
// this is just stuff I use#include <boost/type_traits/integral_constant.hpp>template <bool B>struct bool_type : boost::integral_constant<bool,B>{ static const bool value = B;};typedef const boost::true_type& true_tag;typedef const boost::false_type& false_tag;// can_fit type traits#include <limits>namespace detail{ template <typename T,typename U> struct can_fit { static const bool value = std::numeric_limits<T>::digits <= std::numeric_limits<U>::digits; };}template <typename T,typename U>struct can_fit : bool_type<detail::can_fit<T,U>::value>{ typedef T type1; typedef U type2; static const bool value = detail::can_fit<T,U>::value;};// test#include <iostream>namespace detail{ void foo(true_tag) { std::cout << "T fits in U" << std::endl; } void foo(false_tag) { std::cout << "T does not fit in U" << std::endl; }}// just an exampletemplate <typename T,typename U>void foo(void){ detail::foo(can_fit<T,U>());}int main(void){ foo<int,double>();}
C#中调整图像大小的步骤详解
在本篇文章中,我将介绍如何在C#中来调整你想要的图像大小。要实现这一目标,我们可以采取以下几个步骤:
1.首先要获取你想要调整大小的图像:
string path = Server.MapPath("~/Images"); System.Drawing.Image img = System.Drawing.Image.FromFile(string.Concat(path,"/3904.jpg"));
2.将图像转换为Bitmap:
Bitmap b = new Bitmap(img);
3.创建一个调整图像大小的方法:
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { //获取图片宽度 int sourceWidth = imgToResize.Width; //获取图片高度 int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //计算宽度的缩放比例 nPercentW = ((float)size.Width / (float)sourceWidth); //计算高度的缩放比例 nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; //期望的宽度 int destWidth = (int)(sourceWidth * nPercent); //期望的高度 int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制图像 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (System.Drawing.Image)b; }
在上面的方法中,我们获取了位图图像,然后绘制了不同尺寸的图像(这里绘制出的图像是基于指定的纵横比)
4.调用上述方法,得到调整大小之后的图片:
System.Drawing. Image i = resizeImage(b, new Size(100, 100));
输出结果:
到此这篇关于C#中调整图像大小的步骤详解的文章就介绍到这了,更多相关C#图像大小内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
- C#加载嵌入到资源的非托管dll
- C#基于Mongo的官方驱动手撸一个Super简易版MongoDB-ORM框架
- 使用C#实现一个PPT遥控器
- C#读写文本文件(.txt)的方法实例
- C#读取txt文件数据的方法实例
- 详解C# 线程的挂起与唤醒
- c#基于Redis实现轻量级消息组件的步骤
- 详解搭建基于C#和Appium的Android自动测试环境
html – 在’a href’标记链接中调整图像大小
<a href="images/image.jpg"><img src="images/image.jpg" width="300" height="214" border="0"></a>
但是,如果我希望href链接显示更小的图像,该怎么办呢?所以,让我们说原始的image.jpg是1500×1200,在链接中我想要显示800×600.
无论如何要做这样的事情:
<a href="images/image.jpg" width="800" height="600"><img src="images/image.jpg" width="300" height="214" border="0"></a>
解决方法
你不能这样做.您最好的选择(用于控制链接源的大小)是使用服务器端脚本为您的图像提供服务,该脚本接受查询字符串中的大小参数.像image.PHP?img = image.jpg& w = 800& h = 600.
但这是另一个问题,我不能在这里详细介绍.
当然,如果您只想将图像更改为特定大小,只需调整实际图像大小并再次上传:)
今天关于在Python中调整图像大小而不会丢失EXIF数据和python设置图像大小的介绍到此结束,谢谢您的阅读,有关Android:上传图片而不会丢失Exif数据、c – 查找浮点类型可以处理的最大整数大小而不会丢失精度、C#中调整图像大小的步骤详解、html – 在’a href’标记链接中调整图像大小等更多相关知识的信息可以在本站进行查询。
本文标签: